Spring security集成CAS

什么是Spring security?

Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IOC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

Spring Security既可以做登陆验证功能,也可以负责判断用户具有的权限功能,但其主要是用来判断用户权限。

Spring Security和cas集成后,Spring Security主要用来负责权限判断,而是否登陆的判断就交给啦cas来负责。

Spring security集成CAS单点登录系统

  1. 创建项目导入spring、 spring security和CAS依赖
  2. 建立web.xml ,添加过滤器等配置

  3. 创建配置文件spring-security.xml

    1. <?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">
      //配置用户不需要登陆即可访问的资源
      <http pattern="/css/**" security="none"></http>
      	<http pattern="/img/**" security="none"></http>
      	<http pattern="/js/**" security="none"></http>
      	<http pattern="/plugins/**" security="none"></http>
      	.....
      <!--   entry-point-ref  入口点引用 -->
      <http use-expressions="false" entry-point-ref="casProcessingFilterEntryPoint">  
             <intercept-url pattern="/**" access="ROLE_USER"/>   
             <csrf disabled="true"/>  
             <!-- custom-filter为过滤器, 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入口点 开始 -->
         <beans:bean id="casProcessingFilterEntryPoint"class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">  
             <!-- 单点登录服务器登录URL -->  
             <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   -->  
             <beans:property name="service" value="http://localhost:9003/login/cas"/>
         </beans:bean>  
         <!-- CAS入口点 结束 -->
         
         <!-- 认证过滤器 开始 -->
         <beans:bean id="casAuthenticationFilter"class="org.springframework.security.cas.web.CasAuthenticationFilter">  
             <beans:property name="authenticationManager" ref="authenticationManager"/>  
         </beans:bean>  
      	<!-- 认证管理器 -->
      <authentication-manager alias="authenticationManager">
      	<authentication-provider  ref="casAuthenticationProvider">
      	</authentication-provider>
      </authentication-manager>
      	<!-- 认证提供者 -->
      <beans:bean id="casAuthenticationProvider"   class="org.springframework.security.cas.authentication.CasAuthenticationProvider">  
             <beans:property name="authenticationUserDetailsService">  
                 <beans:bean class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">  
                     <beans:constructor-arg ref="userDetailsService" />  
                 </beans:bean>  
             </beans:property>  
             <beans:property name="serviceProperties" ref="serviceProperties"/>  
             <!-- ticketValidator 为票据验证器 -->
             <beans:property name="ticketValidator">  
                 <beans:bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">  
                     <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="认证类的全路径.UserDetailServiceImpl"/>  
      	
      <!-- 认证过滤器 结束 -->
      <!-- 单点登出  开始  -->     
         <beans:bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>          
         <beans:bean id="requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">  
             <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>  
             <beans:property name="filterProcessesUrl" value="/logout/cas"/>  
         </beans:bean>  
         <!-- 单点登出  结束 -->  
      </beans:beans>
      

      认证类:

    2. /**
       * 认证类
       */
      public class UserDetailServiceImpl implements UserDetailsService {
      	@Override
      	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
      		//构建角色集合
      		List<GrantedAuthority> authorities=new ArrayList();
      		authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
      		return new User(username, ""  , authorities);		
      	}
      }
      

      web.xml

    3. <?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">	
         <!-- 解决post乱码 -->
      	<filter>
      		<filter-name>CharacterEncodingFilter</filter-name>
      		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      		<init-param>
      			<param-name>encoding</param-name>
      			<param-value>utf-8</param-value>
      		</init-param>
      		<init-param>  
                  <param-name>forceEncoding</param-name>  
                  <param-value>true</param-value>  
              </init-param>  
      	</filter>
      	<filter-mapping>
      		<filter-name>CharacterEncodingFilter</filter-name>
      		<url-pattern>/*</url-pattern>
      	</filter-mapping>	
      
      	 <context-param>
      		<param-name>contextConfigLocation</param-name>
      		<param-value>classpath:spring/spring-security.xml</param-value>
      	 </context-param>
      	 <listener>
      		<listener-class>
      			org.springframework.web.context.ContextLoaderListener
      		</listener-class>
      	 </listener>
      	
      	 <filter>  
      		<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:spring/springmvc.xml</param-value>
        	</init-param>
        </servlet>
        
        <servlet-mapping>
        	<servlet-name>springmvc</servlet-name>
        	<url-pattern>*.do</url-pattern>
        </servlet-mapping>
        
        <welcome-file-list>
        	<welcome-file>默认访问的页面</welcome-file>
        </welcome-file-list>
      	
      </web-app>

       

  4. 添加html页面

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值