一.體系結構
1.須要注意的幾個配置文件
WEB-INF/deployerConfigContext.xml,WEB-INF/login-webflow.xmlcss
2.AuthenticationManager
配置位置 deployerConfigContext.xmlhtml
主要涉及的方法是Authentication authenticate(final Credential... credentials),以及在authenticate方法中調用的AuthenticationBuilder authenticateInternal(final Credential... credentials)。java
身份認證管理器,經過指定認證憑證的認證處理程序來定義認證的安全策略。3.5的時候實現類是 org.jasig.cas.authentication.AuthenticationManagerImpl , 4.0變爲org.jasig.cas.authentication.PolicyBasedAuthenticationManager,這是3.5和4.0改變最大的緣由。沒有改造的必要,若是debug看代碼從PolicyBasedAuthenticationManager的Authentication authenticate(final Credential... credentials) 方法開始。web
3.Credential
配置位置 login-webflow.xmlspring
由登錄提交的信息封裝成的認證憑證,主要須要定製的內容,注意修改時同時須要修改login-webflow 中 viewLoginForm 的 binder安全
4.AuthenticationHandler
配置位置 deployerConfigContext.xml服務器
主要涉及方法 String getName();session
boolean supports(Credential credential) 判斷是否由該AuthenticationHandler處理app
HandlerResult authenticate(Credential credential) 傳入封裝好的jsp
AuthenticationManager經過委託AuthenticationHandler(處理器)處理Credential(認證憑證)。認證該組件提供身份認證在您的環境中使用的各種證件。
注意:(1)每一個處理器都須要一個惟一的名稱。
(2)不一樣於3.5的AuthenticationHandler(包路徑改變)中authenticate方法返回boolean值,4.0版本AuthenticationHandler中返回的是HandlerResult。
5.PrincipalResolver
配置位置 deployerConfigContext.xml
主要涉及方法 Principal resolve(Credential credential);
經過解析AuthenticationHandler(處理器)處理認證經過的Credential(認證憑證),構建Principal(認證結果)。
在4.0中能夠不配置AuthenticationHandler對應的PrincipalResolver,這樣,程序會調用AuthenticationHandler處理結果中HandlerResult的
6.Principal
一個通用概念,表明一個通過認證的東西。這裏是認證結果的封裝。
在這裏是一個接口,SimplePrincipal是官方提供的一個實現。有兩個方法 getId() 方法用於返回惟一標識,Map<String, Object> getAttributes()能夠在重寫後返回其它的屬性,可是須要修改casServiceValidationSuccess.jsp,修改方法以前的文章中有過講解,這裏再也不贅述。
結果屬性中Map<String, Object> Object儘可能不要放集合,casServiceValidationSuccess.jsp很差修改.
二.輸入參數和返回值的訂製
在 yale-cas服務器端深度定製 文章中已經介紹過3.5如何進行訂製,這裏主要說一下4.0不同的地方
1.deployerConfigContext.xml
PolicyBasedAuthenticationManager中配置AuthenticationHandler的方式發生改變,如今能夠向下面這樣配置一個自定義的AuthenticationHandler。
上面說過能夠不配置PrincipalResolver
<bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager"> <constructor-arg> <map> <entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver"/> <entry key-ref="myAuthenticationHandler"> <null/> </entry> </map> </constructor-arg> <property name="authenticationPolicy"> <bean class="org.jasig.cas.authentication.AnyAuthenticationPolicy"/> </property> </bean>
2.AuthenticationHandler
主要的認證工做和服務器端返回值都在AuthenticationHandler的authenticate方法中完成,注意 getName和 supports 兩個方法也須要重寫
@Override public HandlerResult authenticate(Credential credential) throws GeneralSecurityException, PreventedException { MyCredential myCredential = (MyCredential)credential; if (credential == null) { throw new FailedLoginException(); } else if (StringUtils.isBlank(credential.getId())) { logger.debug("{} was not found in the map.", credential); throw new AccountNotFoundException(credential + " not found in backing map."); } Map<String, Object> attributes = Maps.newHashMap(); attributes.put("中文KEY", "中文value"); attributes.put("空值key", ""); attributes.put("custom", myCredential.getCustom()); Principal principal = new SimplePrincipal(credential.getId(), attributes); return new HandlerResult(this, new BasicCredentialMetaData(credential), principal); }
3.login-webflow.xml
設置自定義Credential(認證憑證),MyCredential是一個只有有三個String屬性username,password,custom的pojo
<var name="credential" class="com.gqshao.sso.credential.MyCredential" />