本示例jfinal集成cas單點認證,採用獲取到登陸用戶session信息後,在本地站點備份一份session信息,主要作如下幾個步驟:java
一、站點引入響應jar包;web
二、在web.xml中配置對應過濾器;ajax
三、增長攔截handler,並在jfinal的config中配置。api
四、註銷操做session
這次示例在handler中獲取登陸用戶session,並設置本站點session。app
詳細介紹以下:jsp
一、引入jar包ide
須要引入兩個jar包:cas-client-core-3.2.2.jar和commons-logging-1.2.jar;this
將兩個jar包放入站點WEB-INFO/lib下便可;url
二、在web.xml中配置對應過濾器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <context-param> 4 <param-name>getAuthMenuUrl</param-name> 5 <param-value>http://192.168.2.175:8082/hebswj_yw/api/modulePermis/</param-value> 6 </context-param> 7 <context-param> 8 <param-name>homePageUrl</param-name> 9 <param-value>http://192.168.2.175:8082/hebswj/index.jsp</param-value> 10 </context-param> 11 12 <!-- ========================集成CAS單點登陸模塊 開始=========================== --> 13 <!-- 1.CAS單點登出 --> 14 <listener> 15 <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class> 16 </listener> 17 <filter> 18 <filter-name>CAS Single Sign Out Filter</filter-name> 19 <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class> 20 </filter> 21 <filter-mapping> 22 <filter-name>CAS Single Sign Out Filter</filter-name> 23 <url-pattern>/*</url-pattern> 24 </filter-mapping> 25 26 <!-- 2.CAS單點登陸 --> 27 <filter> 28 <filter-name>CASFilter</filter-name> 29 <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class> 30 <init-param> 31 <param-name>casServerLoginUrl</param-name> 32 <param-value>http://192.168.2.175:8082/cas/login</param-value> 33 </init-param> 34 <init-param> 35 <param-name>serverName</param-name> 36 <param-value>http://127.0.0.1</param-value> 37 </init-param> 38 </filter> 39 <filter-mapping> 40 <filter-name>CASFilter</filter-name> 41 <url-pattern>/*</url-pattern> 42 </filter-mapping> 43 44 <!-- 3.CAS票據驗證 --> 45 <filter> 46 <filter-name>CAS Validation Filter</filter-name> 47 <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class> 48 <init-param> 49 <param-name>casServerUrlPrefix</param-name> 50 <param-value>http://192.168.2.175:8082/cas</param-value> 51 </init-param> 52 <init-param> 53 <param-name>serverName</param-name> 54 <param-value>http://127.0.0.1</param-value> 55 </init-param> 56 <init-param> 57 <param-name>encoding</param-name> 58 <param-value>UTF-8</param-value> 59 </init-param> 60 </filter> 61 <filter-mapping> 62 <filter-name>CAS Validation Filter</filter-name> 63 <url-pattern>/*</url-pattern> 64 </filter-mapping> 65 66 <!--4. CAS HttpServletRequest Wrapper Filter 這個是HttpServletRequet的包裹類,讓他支持getUserPrincipal,getRemoteUser方法來取得用戶信息--> 67 <filter> 68 <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> 69 <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class> 70 </filter> 71 <filter-mapping> 72 <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> 73 <url-pattern>/*</url-pattern> 74 </filter-mapping> 75 76 <!--5. CAS Assertion Thread Local Filter 這個類把Assertion信息放在ThreadLocal變量中,這樣應用程序不在web層也可以獲取到當前登陸信息--> 77 <filter> 78 <filter-name>CAS Assertion Thread Local Filter</filter-name> 79 <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class> 80 </filter> 81 <filter-mapping> 82 <filter-name>CAS Assertion Thread Local Filter</filter-name> 83 <url-pattern>/*</url-pattern> 84 </filter-mapping> 85 <!-- ========================集成CAS單點登陸模塊 結束=========================== --> 86 87 <filter> 88 <filter-name>jfinal</filter-name> 89 <filter-class>com.jfinal.core.JFinalFilter</filter-class> 90 <init-param> 91 <param-name>configClass</param-name> 92 <param-value>com.config.Config</param-value> 93 </init-param> 94 </filter> 95 96 <filter-mapping> 97 <filter-name>jfinal</filter-name> 98 <url-pattern>/*</url-pattern> 99 </filter-mapping> 100 101 <welcome-file-list> 102 <welcome-file>web/index.jsp</welcome-file> 103 </welcome-file-list> 104 </web-app>
注:cas的過濾器須要配置在jfinal過濾器前面,不然在handler中獲取不到傳入的登陸用戶session信息;
web.xml中配置的serverName當前地址必須爲ip地址,不可爲127.0.0.1,具體緣由還不清楚,待後續研究。
三、增長攔截handler,並在jfinal中配置
1 package com.handler; 2 3 import java.io.Console; 4 import java.util.Map; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import org.jasig.cas.client.authentication.AttributePrincipal; 8 import com.jfinal.handler.Handler; 9 import com.model.User; 10 11 /** 12 * 用於cas驗證輔助使用,並存儲用戶信息到session 13 * @author sunflower 14 * 15 */ 16 public class CasHelpHandler extends Handler{ 17 18 @Override 19 public void handle(String target, HttpServletRequest request, 20 HttpServletResponse response, boolean[] isHandled) { 21 22 int index = target.lastIndexOf(";jsessionid"); 23 24 if(index>-1){ 25 target = index==-1?target:target.substring(0, index); 26 } 27 28 AttributePrincipal principal1 = (AttributePrincipal) request.getUserPrincipal(); 29 Map<String, Object> userMap = principal1.getAttributes(); 30 31 if(userMap!=null){ 32 User user=new User(); 33 user.setUserId(userMap.get("UserId").toString()); 34 user.setUserName(userMap.get("RealName").toString()); 35 user.setDeptId(userMap.get("DeptId").toString()); 36 user.setRoleId(userMap.get("RoleId").toString()); 37 request.getSession().setAttribute("user",user); 38 } 39 40 nextHandler.handle(target, request, response, isHandled); 41 } 42 43 }
1 package com.config; 2 3 import com.util.*; 4 import com.handler.CasHelpHandler; 5 import com.jfinal.config.Constants; 6 import com.jfinal.config.Handlers; 7 import com.jfinal.config.Interceptors; 8 import com.jfinal.config.JFinalConfig; 9 import com.jfinal.config.Plugins; 10 import com.jfinal.config.Routes; 11 import com.jfinal.ext.handler.UrlSkipHandler; 12 import com.jfinal.ext.route.AutoBindRoutes; 13 import com.jfinal.kit.PropKit; 14 import com.util.ConfigDBPluginHelper; 15 16 /** 17 * API引導式配�? 18 */ 19 public class Config extends JFinalConfig { 20 21 /** 22 * 配置常量 23 */ 24 public void configConstant(Constants me) { 25 // 加載少許必要配置,隨後可用PropKit.get(...)獲取�? 26 PropKit.use("config.txt"); 27 me.setDevMode(PropKit.getBoolean("devMode", false)); 28 } 29 30 /** 31 * 配置路由 32 */ 33 public void configRoute(Routes me) { 34 me.add(new AutoBindRoutes()); 35 36 // 根據xml文件配置,自動配置路由 37 ConfigRoutesHelper.configRouteInXml(getClass().getResource("/")+"deploy.xml", me); 38 } 39 40 /** 41 * 配置插件 42 */ 43 public void configPlugin(Plugins me) { 44 ConfigDBPluginHelper.configSqlServerPlugin(me); 45 } 46 47 /** 48 * 配置全局攔截�? 49 */ 50 public void configInterceptor(Interceptors me) { 51 52 } 53 54 /** 55 * 配置處理�? 56 */ 57 public void configHandler(Handlers me) { 58 // 增長例外 59 me.add(new CasHelpHandler()); 60 me.add(new UrlSkipHandler("/web/",true)); 61 me.add(new UrlSkipHandler("/$",true)); 62 } 63 }
若是找不到jfinal的配置文件,在web.xml中查看jfinal對應的configclass便可找到
1 <filter> 2 <filter-name>jfinal</filter-name> 3 <filter-class>com.jfinal.core.JFinalFilter</filter-class> 4 <init-param> 5 <param-name>configClass</param-name> 6 <param-value>com.config.Config</param-value> 7 </init-param> 8 </filter>
四、註銷操做
在調用cas服務註銷登陸用戶的時候,不能使用ajax的方式請求,故可使用iframe來輔助註銷登陸。
在頁面增長一個隱形的iframe,註銷的時候,將iframe的url設置爲cas服務註銷登陸的服務,而後再iframe加載完成後,從新設置當前頁面的location.href,便可達到註銷的目的。
1 <iframe onReadystatechange ={this.directToLogin} onLoad={this.directToLogin} style={{position:"absolute",left:"-110px",width:"100px"}} ref="iframe"/>
1 loginOut:function(){ 2 var _url=CONFIGS["logOutUrl"]; 3 this.refs["iframe"].src=_url; 4 }, 5 directToLogin:function(){ 6 location.href=CTX; 7 },
配置完成後,即對接成功~