在正常的开发过程中,一般会使用spring Security等安全框架来进行登录用户的拦截,因为使用安全框架,可以配置特定的角色访问特定的资源,这里简单说明Sping Security集成CAS的使用
Spring Security集成CAS
- 建立maven工程,引入spring和spring security的相关依赖,配置tomcat插件启动项目,端口为9090,并且引入Spring Security与 CAS集成的依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itcast.demo</groupId>
<artifactId>casclient_demo3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-cas</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>3.3.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>9090</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 建立web.xml ,加载springSecurity的配置文件和springMVC的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- 加載spring security的配置文件(其实就是spring配置文件) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-security.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring security的过滤器,过滤所有的请求 -->
<filter> <!-- filter-name是spring security内置好的过滤器名称,固定要是:springSecurityFilterChain -->
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
- 创建springMVC的配置文件,这里springMVC的作用是简单的创建一个controller,通过访问这个controller的一个方法,来获取当前登录的用户名,下面会详细讲到
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.dss.demo"/>
<mvc:annotation-driven/>
</beans>
- 创建配置文件spring-security.xml,这里主要是配置了Spring Security集成CAS的相关配置,内容很多,比较抽象,里面的注释都是我个人的理解,仅供参考
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<!-- use-expressions="false"表示spring security的配置角色时的关闭表达式 -->
<!-- entry-point-ref 入口点引用,因为原来的spring security有自己的登录入口,但是这里集成了cas,所以要使用cas的登录入口,在这里配置cas的入口点的引用 -->
<http use-expressions="false" entry-point-ref="casProcessingFilterEntryPoint">
<!-- 表示要访问根目录下的所有资源,需要是ROLE_USER这个角色 -->
<intercept-url pattern="/**" access="ROLE_USER"/>
<!-- 关闭csrg请求 -->
<csrf disabled="true"/>
<!-- custom-filter为Spring security过滤器 -->
<!-- 因为过滤器的配置有先后之分,而spring security中也有很多内置的过滤器,而且它给每个过滤器都定义了一个别名,可以直接用别名来标识某个过滤器,如下CAS_FILTER,LOGOUT_FILTER等都是内置过滤的别名 -->
<!-- 这里需要自定义几个过滤器,加入spring security中,position 表示将过滤器放在指定的位置上,before表示放在指定位置之前 ,after表示放在指定的位置之后 -->
<custom-filter ref="casAuthenticationFilter" position="CAS_FILTER" />
<custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER"/>
<custom-filter ref="singleLogoutFilter" before="CAS_FILTER"/>
</http>
<!-- CAS入口点 开始 ,这里就是定一个了一个CAS入口点-->
<beans:bean id="casProcessingFilterEntryPoint" class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">
<!-- 单点登录服务器登录URL,也就是cas登录页面的访问路径 -->
<beans:property name="loginUrl" value="http://localhost:9100/cas/login"/>
<!-- 这里需要引用一个服务参数 -->
<beans:property name="serviceProperties" ref="serviceProperties"/>
</beans:bean>
<beans:bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
<!--service 配置自身工程的根地址+/login/cas,表示是通过自身工程跳转到cas的登录页面,/login/cas 这个是security集成cas的固定写法 -->
<beans:property name="service" value="http://localhost:9090/login/cas"/>
</beans:bean>
<!-- CAS入口点 结束 -->
<!-- 认证过滤器 开始 -->
<beans:bean id="casAuthenticationFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">
<!-- 引入一个security的认证管理器 -->
<beans:property name="authenticationManager" ref="authenticationManager"/>
</beans:bean>
<!-- 认证管理器 -->
<authentication-manager alias="authenticationManager">
<!-- 引入一个 security的认证提供者-->
<authentication-provider ref="casAuthenticationProvider"/>
</authentication-manager>
<!-- 认证提供者 -->
<beans:bean id="casAuthenticationProvider" class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
<!-- 这里是引入一个自定义的认证类,是需要自己书写,但是认证的功能交给了cas去做,可以在这个类里面根据用户名,获取security中的角色列表,要获取角色列表,才能访问上面配置的资源 -->
<beans:property name="authenticationUserDetailsService">
<beans:bean class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
<beans:constructor-arg ref="userDetailsService" />
</beans:bean>
</beans:property>
<!-- 这里也需要引入一个服务参数,注入自身工程的路径,表示是从那个url跳到cas的登录页面 -->
<beans:property name="serviceProperties" ref="serviceProperties"/>
<!-- ticketValidator 为票据验证器 -->
<beans:property name="ticketValidator">
<beans:bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
<!-- 单点登录服务器cas的访问路径,用来校验票据 -->
<beans:constructor-arg index="0" value="http://localhost:9100/cas"/>
</beans:bean>
</beans:property>
<!-- 这个是一个固定参数 -->
<beans:property name="key" value="an_id_for_this_auth_provider_only"/>
</beans:bean>
<!-- 认证类 -->
<beans:bean id="userDetailsService" class="cn.itcast.demo.service.UserDetailServiceImpl"/>
<!-- 认证过滤器 结束 -->
<!-- 单点登出 开始 -->
<!-- 这个过滤器表示对cas服务端进行单点登出 -->
<beans:bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>
<!-- 这个过滤器表示,客户端进行单点登出 -->
<beans:bean id="requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
<!-- 这个配置的是cas登出的路径,后面service参数表示登出后,重定向的url,而且service参数名固定是这个 -->
<beans:constructor-arg value="http://localhost:9100/cas/logout?service=http://www.baidu.com"/>
<beans:constructor-arg>
<beans:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/>
</beans:constructor-arg>
<!-- 这个表示,在我们的系统中,只要请求/logout/cas,就相当于访问了上面配置的cas的登出路径 -->
<beans:property name="filterProcessesUrl" value="/logout/cas"/>
</beans:bean>
<!-- 单点登出 结束 -->
</beans:beans>
- 创建一个html页面,用于测试方便
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
登录成功...<br/>
<a href="/logout/cas">退出登录</a>
</body>
</html>
- 因为使用了security,所以要写一个自定的认证类,这个认证类在这里的主要作用就是返回当前认证成功的用户的角色列表(详细看spring-security.xml这个配置文件)
public class UserDetailServiceImpl implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
System.out.println("经过认证类..." + username);
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("ROLE_USER")); //这个角色名需要在security.xml文件中配置好
//这里只需要返回认证的username和角色类别,当单独使用security时,需要在这里根据用户名查询密码,再返回密码,通过security进行认证,但是集成了cas之后,认证的工作就由cas完成了
return new User(username, "", authorities);
}
}
至此,spring security集成cas就完成了,首先启动cas,我这里的端口是9100,然后启动这个测试项目,在浏览器输入http://localhost:9090/index.html后,因为没有登录,页面就会自动跳转到cas的登录页面,在那个登录页面登录成功之后,又会回到这个index.html页面
- 通过security获取当前登录的用户名
创建一个Controller类
@RestController
public class UserController {
@RequestMapping("/findLoginUser")
public void findLoginUser() {
String name = SecurityContextHolder.getContext().getAuthentication()
.getName();
System.out.println(name);
}
}
当登录成功之后,在浏览器访问http://localhost:9090/findLoginUser.do,就能在控制台看到输出当前登录的用户名,这个是spring security的知识。
- 单点登出
在index.html页面中有一个a标签,请求的路径是/logout/cas,当点击这个a标签后,就能退出登录,并且跳转到百度页面,详细原因,参考spring-security.xml文件中的相关配置,如下:
<!-- 单点登出 开始 -->
<!-- 这个过滤器表示对cas服务端进行单点登出 -->
<beans:bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>
<!-- 这个过滤器表示,客户端进行单点登出 -->
<beans:bean id="requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
<!-- 这个配置的是cas登出的路径,后面service参数表示登出后,重定向的url,而且service参数名固定是这个 -->
<beans:constructor-arg value="http://localhost:9100/cas/logout?service=http://www.baidu.com"/>
<beans:constructor-arg>
<beans:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/>
</beans:constructor-arg>
<!-- 这个表示,在我们的系统中,只要请求/logout/cas,就相当于访问了上面配置的cas的登出路径 -->
<beans:property name="filterProcessesUrl" value="/logout/cas"/>
</beans:bean>
<!-- 单点登出 结束 -->