參考文章http://www.ibm.com/developerworks/cn/opensource/os-cn-liferay-cas/index.html html
發現使用該方法最後出現了空指針異常,修改了liferay部分源碼實現了Digester,特此記錄,給後來人方便。 java
在此咱們採用和 Liferay 一致的默認加密算法「SHA」,在調用 Liferay 提供的加密算法時, 咱們須要將 Liferay 的幾個 jar 包引入到咱們的 cas 工程中,自定義的加密算法也很簡單, 只須要調用 jar 包中的加密工具類提供的加密算法便可,同時自定義的類須要繼承 PasswordEncoder 接口:使用maven管理的話,代碼以下
<dependency> <groupId>com.liferay.portal</groupId> <artifactId>portal-service</artifactId> <version>6.2.1</version> </dependency>
其中CAS的加密配置中修改以下 mysql
<bean id="dbAuthHandler" class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler" p:dataSource-ref="dataSource" p:passwordEncoder-ref="myPasswordEncoder" p:sql="SELECT password_ FROM User_ WHERE screenName=?" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://127.0.0.1:3306/portal?useUnicode=true&characterEncoding=utf-8" p:username="root" p:password="" /> <bean id="myPasswordEncoder" class="passwordEncoder.MyPasswordEncoder"> <constructor-arg value="SHA"/> </bean> <bean id="digesterUtil" class="com.liferay.portal.kernel.util.DigesterUtil"> <property name="digester" ref="digester"></property> </bean> <bean id="digester" class="passwordEncoder.DigesterImpl"></bean>
DigesterImpl是Digester的實現。 web
import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.security.pacl.DoPrivileged; import com.liferay.portal.kernel.util.Base64; import com.liferay.portal.kernel.util.Digester; import com.liferay.portal.kernel.util.StreamUtil; import com.liferay.portal.kernel.util.StringBundler; import com.liferay.portal.kernel.util.StringPool; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.commons.codec.binary.Hex; /** * @author Brian Wing Shun Chan * @author Alexander Chow * @author Connor McKay */ @DoPrivileged public class DigesterImpl implements Digester { @Override public String digest(ByteBuffer byteBuffer) { return digest(Digester.DEFAULT_ALGORITHM, byteBuffer); } @Override public String digest(InputStream inputStream) { return digest(Digester.DEFAULT_ALGORITHM, inputStream); } @Override public String digest(String text) { return digest(Digester.DEFAULT_ALGORITHM, text); } @Override public String digest(String algorithm, ByteBuffer byteBuffer) { if (_BASE_64) { return digestBase64(algorithm, byteBuffer); } else { return digestHex(algorithm, byteBuffer); } } @Override public String digest(String algorithm, InputStream inputStream) { if (_BASE_64) { return digestBase64(algorithm, inputStream); } else { return digestHex(algorithm, inputStream); } } @Override public String digest(String algorithm, String... text) { if (_BASE_64) { return digestBase64(algorithm, text); } else { return digestHex(algorithm, text); } } @Override public String digestBase64(ByteBuffer byteBuffer) { return digestBase64(Digester.DEFAULT_ALGORITHM, byteBuffer); } @Override public String digestBase64(InputStream inputStream) { return digestBase64(Digester.DEFAULT_ALGORITHM, inputStream); } @Override public String digestBase64(String text) { return digestBase64(Digester.DEFAULT_ALGORITHM, text); } @Override public String digestBase64(String algorithm, ByteBuffer byteBuffer) { byte[] bytes = digestRaw(algorithm, byteBuffer); return Base64.encode(bytes); } @Override public String digestBase64(String algorithm, InputStream inputStream) { byte[] bytes = digestRaw(algorithm, inputStream); return Base64.encode(bytes); } @Override public String digestBase64(String algorithm, String... text) { byte[] bytes = digestRaw(algorithm, text); return Base64.encode(bytes); } @Override public String digestHex(ByteBuffer byteBuffer) { return digestHex(Digester.DEFAULT_ALGORITHM, byteBuffer); } @Override public String digestHex(InputStream inputStream) { return digestHex(Digester.DEFAULT_ALGORITHM, inputStream); } @Override public String digestHex(String text) { return digestHex(Digester.DEFAULT_ALGORITHM, text); } @Override public String digestHex(String algorithm, ByteBuffer byteBuffer) { byte[] bytes = digestRaw(algorithm, byteBuffer); return Hex.encodeHexString(bytes); } @Override public String digestHex(String algorithm, InputStream inputStream) { byte[] bytes = digestRaw(algorithm, inputStream); return Hex.encodeHexString(bytes); } @Override public String digestHex(String algorithm, String... text) { byte[] bytes = digestRaw(algorithm, text); return Hex.encodeHexString(bytes); } @Override public byte[] digestRaw(ByteBuffer byteBuffer) { return digestRaw(Digester.DEFAULT_ALGORITHM, byteBuffer); } @Override public byte[] digestRaw(String text) { return digestRaw(Digester.DEFAULT_ALGORITHM, text); } @Override public byte[] digestRaw(String algorithm, ByteBuffer byteBuffer) { MessageDigest messageDigest = null; try { messageDigest = MessageDigest.getInstance(algorithm); messageDigest.update(byteBuffer); } catch (NoSuchAlgorithmException nsae) { _log.error(nsae, nsae); } return messageDigest.digest(); } @Override public byte[] digestRaw(String algorithm, InputStream inputStream) { MessageDigest messageDigest = null; try { messageDigest = MessageDigest.getInstance(algorithm); byte[] buffer = new byte[StreamUtil.BUFFER_SIZE]; int read = 0; while ((read = inputStream.read(buffer)) != -1) { if (read > 0) { messageDigest.update(buffer, 0, read); } } } catch (IOException ioe) { _log.error(ioe, ioe); } catch (NoSuchAlgorithmException nsae) { _log.error(nsae, nsae); } finally { StreamUtil.cleanUp(inputStream); } return messageDigest.digest(); } @Override public byte[] digestRaw(String algorithm, String... text) { MessageDigest messageDigest = null; try { messageDigest = MessageDigest.getInstance(algorithm); StringBundler sb = new StringBundler(text.length * 2 - 1); for (String t : text) { if (sb.length() > 0) { sb.append(StringPool.COLON); } sb.append(t); } String s = sb.toString(); messageDigest.update(s.getBytes(Digester.ENCODING)); } catch (NoSuchAlgorithmException nsae) { _log.error(nsae, nsae); } catch (UnsupportedEncodingException uee) { _log.error(uee, uee); } return messageDigest.digest(); } private static final boolean _BASE_64 = true; private static Log _log = LogFactoryUtil.getLog(DigesterImpl.class); }
最後一點,Liferay6.2.1最新版中,默認的加密方法不是SHA加密,須要配置文件 算法
passwords.encryption.algorithm=SHA
Liferay中Portal.properties文件保存着Liferay不少的平臺級的配置參數,經過在這裏調整一些參數的配置信息,能夠知足許多不一樣的需求。通常咱們定義Portal.properties裏面的配置文件是不直接修改的,按配置liferay能夠加載如下路徑下面的配置參數來覆蓋默認的 spring
也就是這些配置參數在上面的任一文件裏面修改均可以。咱們通常將自定義的配置參數放在下面的兩個配置文件裏面。 sql
portal-setup-wizard.properties(在Liferay第一次運行的時候生成,默認是在liferay-portal-6.1.1-ce-ga2目錄下面) apache
portal-ext.properties(在tomcat/webapps/ROOT/WEB-INF/classess下面,不存在能夠本身建立) tomcat