参考文章http://www.ibm.com/developerworks/cn/opensource/os-cn-liferay-cas/index.html
发现使用该方法最后出现了空指针异常,修改了liferay部分源码实现了Digester,特此记录,给后来人方便。
在此我们采用和 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的加密配置中修改如下
<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的实现。
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可以加载以下路径下面的配置参数来覆盖默认的
也就是这些配置参数在上面的任一文件里面修改都可以。我们一般将自定义的配置参数放在下面的两个配置文件里面。
portal-setup-wizard.properties(在Liferay第一次运行的时候生成,默认是在liferay-portal-6.1.1-ce-ga2目录下面)
portal-ext.properties(在tomcat/webapps/ROOT/WEB-INF/classess下面,不存在可以自己创建)