CAS单点登录示例

maven项目可以使用以下jar包,

maven项目可以通过pom.xml中增加:

 1         <!-- 集成cas -->
 2         <dependency>
 3             <groupId>soa</groupId>
 4             <artifactId>soa-casclient</artifactId>
 5             <version>1.0.0</version>
 6             <exclusions>
 7                 <exclusion>
 8                     <groupId>org.slf4j</groupId>
 9                     <artifactId>log4j-over-slf4j</artifactId>
10                 </exclusion>
11                 <exclusion>
12                     <groupId>org.slf4j</groupId>
13                     <artifactId>slf4j-api</artifactId>
14                 </exclusion>
15                 <exclusion>
16                     <groupId>org.slf4j</groupId>
17                     <artifactId>slf4j-log4j12</artifactId>
18                 </exclusion>
19             </exclusions>
20         </dependency>
21         <!-- 集成cas -->  
pom.xml

 

web.xml加入以下代码

 1     <!-- ======================== 单点登录开始 ======================== -->
 2     <!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置-->
 3     <listener>
 4         <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
 5     </listener>
 6  
 7     <!-- 该过滤器用于实现单点登出功能,可选配置。 -->
 8     <filter>
 9         <filter-name>CAS Single Sign Out Filter</filter-name>
10         <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
11     </filter>
12     <filter-mapping>
13         <filter-name>CAS Single Sign Out Filter</filter-name>
14         <url-pattern>/*</url-pattern>
15     </filter-mapping>
16  
17     <!-- 该过滤器负责用户的认证工作,必须启用它 -->
18     <filter>
19         <filter-name>CASFilter</filter-name>
20         <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>                                    
21         <init-param>
22             <param-name>casServerLoginUrl</param-name>
23 <!-- //单点登陆服务器访问地址 -->
24             <param-value>http://192.168.1.116:8888/cas/login</param-value>
25         </init-param>         
26         <init-param>
27             <param-name>service</param-name>         
28 <!-- //接入系统的访问地址 -->
29       <param-value>http://localhost:8083/zjhrssmis</param-value>
30         </init-param>
31     </filter>
32     <filter-mapping>
33         <filter-name>CASFilter</filter-name>
34         <url-pattern>/*</url-pattern>
35     </filter-mapping>
36  
37     <!-- 该过滤器负责对Ticket的校验工作,必须启用它 -->
38     <filter>
39         <filter-name>CAS Validation Filter</filter-name>
40         <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
41         <init-param>
42             <param-name>casServerUrlPrefix</param-name>
43 <!-- //单点登陆服务器访问地址 -->
44             <param-value>http://192.168.1.116:8888/cas</param-value>
45         </init-param>
46         <init-param>
47             <param-name>service</param-name>
48 <!-- //接入系统的访问地址 -->
49             <param-value>http://localhost:8083/zjhrssmis</param-value>
50         </init-param>
51     </filter>
52     <filter-mapping>
53         <filter-name>CAS Validation Filter</filter-name>
54         <url-pattern>/*</url-pattern>
55     </filter-mapping>
56  
57     <!-- 该过滤器负责实现HttpServletRequest请求的包裹,
58              比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。 -->
59     <filter>
60         <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
61         <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
62     </filter>
63     <filter-mapping>
64         <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
65         <url-pattern>/*</url-pattern>
66     </filter-mapping>
67  
68     <!-- 该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。
69             比如AssertionHolder.getAssertion().getPrincipal().getName()。 -->
70     <filter>
71         <filter-name>CAS Assertion Thread Local Filter</filter-name>
72         <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
73     </filter>
74     <filter-mapping>
75         <filter-name>CAS Assertion Thread Local Filter</filter-name>
76         <url-pattern>/*</url-pattern>
77     </filter-mapping>
78  
79     <!-- 根据单点登录的结果设置本系统的用户信息 ,过滤器的位置随意建,名称随意定,与下述类名一致即可 -->
80     <filter>
81         <filter-name>userAdapterFilter</filter-name>
82         <filter-class>com.cas.UserAdapterFilter</filter-class>
83     </filter>
84     <filter-mapping>
85         <filter-name>userAdapterFilter</filter-name>
86         <url-pattern>/*</url-pattern>
87     </filter-mapping>
88 <!-- ======================== 单点登录结束 ======================== -->
web.xml

UserAdapterFilter.java

UserAdapterFilter

跳转的登录action

userCasLogin

 

posted @ 2017-02-16 17:30  你若安好2017  阅读(189)  评论(0编辑  收藏  举报