定制CAS登录验证,用Liferay的用户身份进行验证摘要本文以Liferay与CAS整合为例,将CAS登录验证从输入相同的用户名/密码定制为以Liferay的用户身份进行验证。环境MySQL5.0.5JRE1.6.0.7Ubuntu8.04Liferay5.1.1BundledwithTomcat6.0CASServer3.3.1YaleCASClient2.0.11NetBeansIDE6.1在进行本文示例前,请参考这里。新建工程打开NetBeansIDE,新建JavaClassLibrary工程:PortalAuthHandler。添加依赖包从CAS中的lib下找到如下jar:cas-server-core-3.3.jarinspektr-core-0.7.0.jar下载spring-core.jar(2.5.5),点这里。将这三个jar包添加到工程PortalAuthHandler下。编写测试用例在TestPackages下建立测试用例,代码如下:packagecom.激nfonet.developer.portal;importjunit.framework.TestCase;importorg.jasig.cas.authentication.handler.PasswordEncoder;/****@author88250<DL88250@gmail>*/publicfinalclassBase64PasswordEncoderTestsextendsTestCase{privatefinalPasswordEncoderpasswordEncoder=newBase64PasswordEncoder("SHA1");publicvoidtestHashBase64Encoded(){assertEquals("qUqP5cyxm6YcTAhz05Hph5gvu9M=",this.passwordEncoder.encode("test"));}publicvoidtestNullPassword(){assertEquals(null,this.passwordEncoder.encode(null));}publicvoidtestInvalidEncodingType(){finalPasswordEncoderpe=newBase64PasswordEncoder("invalidencoding");try{pe.encode("test");fail("exceptionexpected.");}catch(finalExceptione){return;}}}这个测试用例有三个测试方法,其中HashBase64Encoded最为重要。因为在Liferay的帐户表User_中的password_字段默认是以SHA1进行加密,然后再以Base64进行编码存放的。而CAS中自带的PasswordEncoder只有用加密算法进行加密的步骤,没有Base64编码的步骤,所以我们要写一个带有Base64编码功能的Encoder,且必须是实现org.jasig.cas.authentication.handler.PasswordEncoder接口的。编写实现代码注:代码已测试。packagecom.激nfonet.developer.portal;importjava.io.UnsupportedEncodingException;importjava.security.MessageDigest;importjava.security.NoSuchAlgorithmException;//importorg.inspektrmon.ioc.annotation.NotNull;importorg.jasig.cas.authentication.handler.PasswordEncoder;importorg.springframework.util.StringUtils;importsun.misc.BASE64Encoder;/****@author88250<DL88250@gmail>*/publicclassBase64PasswordEncoderimplementsPasswordEncoder{privatestaticfinalchar[]HEX_DIGITS={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};//@NotNullprivatefinalStringencodingAlgorithm;privateStringcharacterEncoding;publicBase64PasswordEncoder(finalStringencodingAlgorithm){this.encodingAlgorithm=encodingAlgorithm;}publicStringencode(finalStringpassword){if(password==null){returnnull;}try{MessageDigestmessageDigest=MessageDigest.getInstance(this.encodingAlgorithm);if(StringUtils.hasText(this.characterEncoding)){messageDigest.update(password.getBytes(this.characterEncoding));}else{messageDigest.update(password.getBytes());}finalbyte[]digest=messageDigest.digest();returngetFormattedText(digest);}catch(finalNoSuchAlgorithmExceptione){thrownewSecurityException(e);}catch(finalUnsupportedEncodingExceptione){thrownewRuntimeException(e);}}/***Takestherawbytesfromthedigestandformatsthemcorrect.**@parambytestherawbytesfromthedigest.*@returntheformattedbytes.*/privateStringgetFormattedText(byte[]bytes){finalStringBuilderbuf=newStringBuilder(bytes.length*2);sun.misc.BASE64Encodere=newBASE64Encoder();finalStringbuf2=e.encode(bytes);for(intj=0;j<bytes.length;j++){buf.append(HEX_DIGITS[(bytes[j]>>4)0x0f]);buf.append(HEX_DIGITS[bytes[j]0x0f]);}System.out.println("Final:"+buf2);System...