cas 客户端搭建

客户端应用创建

新建一动态web工程cas-client1,这里使用的是maven来创建,在pom文件中增加对cas-client的依赖。

Java代码   收藏代码
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>org.dylan</groupId>  
  5.     <artifactId>cas-client1</artifactId>  
  6.     <version>1.0-SNAPSHOT</version>  
  7.     <packaging>war</packaging>  
  8.     <dependencies>  
  9.         <dependency>  
  10.             <groupId>org.springframework</groupId>  
  11.             <artifactId>spring-web</artifactId>  
  12.             <version>3.1.2.RELEASE</version>  
  13.         </dependency>  
  14.         <dependency>  
  15.             <groupId>org.jasig.cas</groupId>  
  16.             <artifactId>cas-client-core</artifactId>  
  17.             <version>3.1.10</version>  
  18.         </dependency>  
  19.   
  20.         <dependency>  
  21.             <groupId>javax.servlet</groupId>  
  22.             <artifactId>servlet-api</artifactId>  
  23.             <version>2.5</version>  
  24.             <scope>provided</scope>  
  25.         </dependency>  
  26.     </dependencies>  
  27.   
  28.     <build>  
  29.         <plugins>  
  30.             <plugin>  
  31.                 <groupId>org.codehaus.mojo</groupId>  
  32.                 <artifactId>tomcat-maven-plugin</artifactId>  
  33.                 <configuration>  
  34.                     <port>8081</port>  
  35.                 </configuration>  
  36.             </plugin>  
  37.         </plugins>  
  38.     </build>  
  39. </project>  

 

Web.xml文件

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="cas-client1" version="2.5">  
  6.     <context-param>  
  7.         <param-name>contextConfigLocation</param-name>  
  8.         <param-value>classpath*:applicationContext-cas.xml</param-value>  
  9.     </context-param>  
  10.     <!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置 -->  
  11.     <listener>  
  12.         <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>  
  13.     </listener>  
  14.   
  15.     <!-- 该过滤器用于实现单点登出功能,可选配置。 -->  
  16.     <filter>  
  17.         <filter-name>CAS Single Sign Out Filter</filter-name>  
  18.         <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>  
  19.     </filter>  
  20.     <filter-mapping>  
  21.         <filter-name>CAS Single Sign Out Filter</filter-name>  
  22.         <url-pattern>/*</url-pattern>  
  23.     </filter-mapping>  
  24.     <filter>  
  25.         <filter-name>CAS Authentication Filter</filter-name>  
  26.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  27.         <init-param>  
  28.             <param-name>targetBeanName</param-name>  
  29.             <param-value>authenticationFilter</param-value>  
  30.         </init-param>  
  31.     </filter>  
  32.     <filter>  
  33.         <filter-name>CAS Validation Filter</filter-name>  
  34.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  35.         <init-param>  
  36.             <param-name>targetBeanName</param-name>  
  37.             <param-value>ticketValidationFilter</param-value>  
  38.         </init-param>  
  39.     </filter>  
  40.   
  41.     <filter-mapping>  
  42.         <filter-name>CAS Authentication Filter</filter-name>  
  43.         <url-pattern>/*</url-pattern>  
  44.     </filter-mapping>  
  45.     <filter-mapping>  
  46.         <filter-name>CAS Validation Filter</filter-name>  
  47.         <url-pattern>/*</url-pattern>  
  48.     </filter-mapping>  
  49.   
  50.     <!-- 该过滤器负责实现HttpServletRequest请求的包裹, 比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。 -->  
  51.     <filter>  
  52.         <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
  53.         <filter-class>  
  54.             org.jasig.cas.client.util.HttpServletRequestWrapperFilter  
  55.         </filter-class>  
  56.     </filter>  
  57.     <filter-mapping>  
  58.         <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
  59.         <url-pattern>/*</url-pattern>  
  60.     </filter-mapping>  
  61.   
  62.     <!-- 该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。 比如AssertionHolder.getAssertion().getPrincipal().getName()。 -->  
  63.     <filter>  
  64.         <filter-name>CAS Assertion Thread Local Filter</filter-name>  
  65.         <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>  
  66.     </filter>  
  67.     <filter-mapping>  
  68.         <filter-name>CAS Assertion Thread Local Filter</filter-name>  
  69.         <url-pattern>/*</url-pattern>  
  70.     </filter-mapping>  
  71.     <!-- 自定义的filter,在用户登录成功之后进行处理 -->  
  72.     <filter>  
  73.         <filter-name>AutoSetUserAdapterFilter</filter-name>  
  74.         <filter-class>org.dylan.sso.filter.AutoSetUserAdapterFilter</filter-class>  
  75.     </filter>  
  76.     <filter-mapping>  
  77.         <filter-name>AutoSetUserAdapterFilter</filter-name>  
  78.         <url-pattern>/*</url-pattern>  
  79.     </filter-mapping>  
  80.       
  81.     <!--       - Loads the root application context of this web app at startup.   
  82.               - The application context is then available via       - WebApplicationContextUtils.getWebApplicationContext(servletContext).   
  83.              -->  
  84.     <listener>  
  85.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  86.     </listener>  
  87. </web-app>  

 

Spring配置文件

 

web.xml中可以看出,这里使用了springDelegatingFilterProxycasfilter进行代理,这样做的好处是将casspring集成起来,方便对配置信息的管理。也更符合主流的编程风格。所以这里还用到了一个spring的配置文件和属性的配置文件。

applicationContext-cas.xml

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
  7.        ">  
  8.     <context:property-placeholder location="classpath:cas-client.properties" />  
  9.     <bean name="authenticationFilter"  
  10.         class="org.jasig.cas.client.authentication.AuthenticationFilter">  
  11.         <property name="casServerLoginUrl" value="${cas.server.loginUrl}" />  
  12.         <property name="renew" value="${cas.server.renew}" />  
  13.         <property name="gateway" value="${cas.server.gateway}" />  
  14.         <property name="service" value="${cas.client.serverName}" />  
  15.     </bean>  
  16.   
  17.     <!-- 对认证ticket进行校验 -->  
  18.     <bean name="ticketValidationFilter"  
  19.         class="org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter">  
  20.         <property name="service" value="${cas.client.serverName}" />  
  21.         <property name="ticketValidator">  
  22.             <bean class="org.jasig.cas.client.validation.Cas10TicketValidator">  
  23.                 <constructor-arg index="0" value="${cas.server.url}" />  
  24.             </bean>  
  25.         </property>  
  26.     </bean>  
  27.   
  28. </beans>  

 

cas-client.properties

Java代码   收藏代码
  1. cas.server.url=http://localhost:8080/cas/  
  2. cas.server.loginUrl=http://localhost:8080/cas/login  
  3. cas.server.renew=false  
  4. cas.server.gateway=false  
  5. cas.client.serverName=http://localhost:8081/cas-client1/  

 

这里的配置信息根据实际的情况进行修改,我这里的cas服务器的地址是http://localhost:8080/cas,当前客户端的服务地址是http://localhost:8081/cas-client1(通过pom文件中可以看到使用的端口号)

AutoSetUserAdapterFilter

该类的doFilter方法如下(详细说明见后文中的参考资料):

Java代码   收藏代码
  1. /** 
  2.      * 过滤逻辑:首先判断单点登录的账户是否已经存在本系统中, 如果不存在使用用户查询接口查询出用户对象并设置在Session中 
  3.      *  
  4.      * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) 
  5.      */  
  6.     public void doFilter(ServletRequest request, ServletResponse response,  
  7.             FilterChain chain) throws IOException, ServletException {  
  8.         HttpServletRequest httpRequest = (HttpServletRequest) request;  
  9.   
  10.         // _const_cas_assertion_是CAS中存放登录用户名的session标志  
  11.         Object object = httpRequest.getSession().getAttribute(  
  12.                 AbstractCasFilter.CONST_CAS_ASSERTION);  
  13.         if (object != null) {  
  14.             Assertion assertion = (Assertion) object;  
  15.             String loginName = assertion.getPrincipal().getName();  
  16.             System.out.println("用户已经在SSO系统中登录,登录用户名为:" + loginName);  
  17.             String user = getCurrentUser(httpRequest);  
  18.   
  19.             // 第一次登录系统  
  20.             if (user == null) {  
  21.                 System.out.println("用户第一次登录系统,保存session信息.");  
  22.                 httpRequest.getSession().setAttribute("User_Info", loginName);  
  23.             } else {  
  24.                 System.out.println("当前Session中的用户信息为:" + user);  
  25.             }  
  26.   
  27.         }  
  28.         chain.doFilter(request, response);  
  29.     }  
  30.   
  31.     private String getCurrentUser(HttpServletRequest request) {  
  32.         return (String) request.getSession().getAttribute("User_Info");  
  33.     }  

 

客户端页面index.jsp

Java代码   收藏代码
  1. %@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6. <title>Insert title here</title>  
  7. </head>  
  8. <body>  
  9.     首页,当前登录用户是:<%=session.getAttribute("User_Info") %>  
  10. </body>  
  11. </html>  

 

由于在web.xml中对安全的拦截配置为/*,所以该页面是受保护的,需要用户输入用户名和密码才能使用。

 

为了测试单点登录在多个应用程序间是否生效,将上面创建的cas-client1复制一份,更名为cas-client2,并将pom文件中tomcat使用的端口配置为8082;并相应地修改cas-client.properties文件中的客户端配置。

 

现在,依次启动cas-servercas-client1cas-client2三个应用。

测试

 

在浏览器中输入地址:http://localhost:8081/cas-client1/index.jsp,可以看到,浏览器已经自动跳转到cas-server的登录页面,如下图:



 

 

输入用户名和密码,登录成功。浏览器又会返回到cas-client1的首页,如下图:



 

此时,在浏览器中打开一个选项卡(由于cas-server默认的cookie是当前窗口,所以不能是再打开一个新窗口测试),输入cas-client2的访问地址:http://localhost:8082/cas-client2/index.jsp。此时,看到用户已经登录,并获取到用户的登录名。如下图:



 

单点登录测试成功。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值