CAS客户端与SpringSecurity集成

4. CAS客户端与SpringSecurity集成

4.1 Spring Security测试工程搭建

1)建立Maven项目casclient_demo3 ,引入spring依赖和spring secrity 相关依赖 ,tomcat端口设置为9003

2)建立web.xml ,添加过滤器等配置

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

4)添加html页面

CAS客户端与SpringSecurity集成_第1张图片

4.2 Spring SecurityCAS集成

1)引入依赖

  

   org.springframework.security  

   spring-security-cas  

   4.1.0.RELEASE  

     

  

        org.jasig.cas.client  

        cas-client-core  

        3.3.3  

          

              

                org.slf4j  

                log4j-over-slf4j  

              

          

 

2)修改spring-security.xml

"1.0" encoding="UTF-8"?>

"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">

 

"false" entry-point-ref="casProcessingFilterEntryPoint">  

        "/**" access="ROLE_USER"/>   

        "true"/>  

                   

        "casAuthenticationFilter"  position="CAS_FILTER" />      

        "requestSingleLogoutFilter" before="LOGOUT_FILTER"/>  

        "singleLogoutFilter" before="CAS_FILTER"/>  

    

    

  

    "casProcessingFilterEntryPoint" class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">  

          

        "loginUrl" value="http://localhost:9100/cas/login"/>  

        "serviceProperties" ref="serviceProperties"/>  

          

    "serviceProperties" class="org.springframework.security.cas.ServiceProperties">  

          

        "service" value="http://localhost:9003/login/cas"/>

      

    

   

    

    "casAuthenticationFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">  

        "authenticationManager" ref="authenticationManager"/>  

      

"authenticationManager">

"casAuthenticationProvider">

"casAuthenticationProvider"     class="org.springframework.security.cas.authentication.CasAuthenticationProvider">  

        "authenticationUserDetailsService">  

            "org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">  

                "userDetailsService" />  

              

          

        "serviceProperties" ref="serviceProperties"/>  

        

        "ticketValidator">  

            "org.jasig.cas.client.validation.Cas20ServiceTicketValidator">  

                "0" value="http://localhost:9100/cas"/>  

              

          

        "key" value="an_id_for_this_auth_provider_only"/> 

            

     

"userDetailsService" class="cn.itcast.demo.service.UserDetailServiceImpl"/>  

 

     

    "singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>          

    "requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">  

        "http://localhost:9100/cas/logout?service=http://www.baidu.com"/>  

          

            "org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/>  

          

        "filterProcessesUrl" value="/logout/cas"/>  

      

      

3)创建UserDetailsServiceImpl  

/**

 * 认证类

 */

public class UserDetailServiceImpl implements UserDetailsService {

@Override

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

//构建角色集合

List authorities=new ArrayList();

authorities.add(new SimpleGrantedAuthority("ROLE_USER"));

return new User(username, ""  , authorities);

}

}

这个类的主要作用是在登陆后得到用户名,可以根据用户名查询角色或执行一些逻辑。

4.3获取登录名

我们在处理后端逻辑需要获得登录名,那么如何获取单点登录的用户名呢? 其实和我们之前获得用户名的方式是完全相同的,我们下面来做个测试。

1web.xml 添加springmvc

  

   springmvc

   org.springframework.web.servlet.DispatcherServlet

  

  

   contextConfigLocation

   classpath:springmvc.xml

  

  

  

  

   springmvc

   *.do

  

2)创建springmvc.xml

"cn.itcast.demo" />

3)创建UserController  

@RestController

public class UserController {

@RequestMapping("/findLoginUser")

public void  findLoginUser(){

String name = SecurityContextHolder.getContext().getAuthentication().getName();

System.out.println(name);

}

}

地址栏输入http://localhost:9003/findLoginUser.do 即可在控制台看到输出的登录名。

4.4退出登录

修改spring-security.xml  

"requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">  

        "http://localhost:9100/cas/logout?service=http://localhost:9003/index2.html"/>  

          

            "org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/>  

          

        "filterProcessesUrl" value="/logout/cas"/>  

在页面上添加链接

"/logout/cas">退出登录

创建index2.html,index2.html设置为可匿名访问

"/index2.html" security="none">

 

5.品优购用户中心

5.1需求分析

用户中心实现单点登录。

5.2代码实现

5.2.1用户中心实现单点登录

(1)将用户中心相关的页面(home-开头的)拷贝至  pinnyougou-user-web  

 CAS客户端与SpringSecurity集成_第2张图片

2pom.xml 引入springSecuritycas客户端和springSecurity Cas整合包依赖(参照casclient_demo3)。

3web.xml  添加spring-security过滤器(参照参照casclient_demo3)设置首页为home-index.html

home-index.html

4)构建UserDetailsServiceImpl.java  (参照casclient_demo3

 CAS客户端与SpringSecurity集成_第3张图片

(5)添加spring-security.xml(参照casclient_demo3),并做以下修改

配置匿名访问资源

"/css/**" security="none">

"/js/**" security="none">

"/image/**" security="none">

"/plugins/**" security="none">

"/register.html" security="none">

"/user/add.do" security="none">

"/user/sendCode.do" security="none">

设置服务地址属性

"serviceProperties" class="org.springframework.security.cas.ServiceProperties">    

     "service" value="http://localhost:9106/login/cas"/>

 

设置认证类

id="userDetailsService" class="com.pinyougou.user.service.UserDetailServiceImpl"/>  

5.2.2页面显示用户名

1pinyougou-user-web创建LoginController.java

@RestController

@RequestMapping("/login")

public class LoginController {

@RequestMapping("/name")

public Map showName(){

String name = SecurityContextHolder.getContext().getAuthentication().getName();//得到登陆人账号

Map map=new HashMap<>();

map.put("loginName", name);

return map;

}

}

2)创建loginService.js

//服务层

app.service('loginService',function($http){

//读取列表数据绑定到表单中

this.showName=function(){

return $http.get('../login/name.do');

}

});

3)创建indexController.js

//首页控制器

app.controller('indexController',function($scope,loginService){

$scope.showName=function(){

loginService.showName().success(

function(response){

$scope.loginName=response.loginName;

}

);

}

});

5)修改home-index.html  引入js

    "text/javascript" src="plugins/angularjs/angular.min.js">

"text/javascript" src="js/base.js">

"text/javascript" src="js/service/loginService.js">

"text/javascript" src="js/controller/indexController.js">

指令,调用方法查询登陆名

ng-app="pinyougou" ng-controller="indexController" ng-init="showName()">

显示用户名

 "name">{{loginName}}

5.2.3退出登录

设置退出登录后的跳转地址

"requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">  

        "http://localhost:9100/cas/logout?service=http://localhost:9103"/>  

       ........  

 

退出登录后,跳转到网站首页

"safe"> "/logout/cas">退出登录 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

附录A. Spring Security 内置过滤器表

别名

Filter

CHANNEL_FILTER

ChannelProcessingFilter

SECURITY_CONTEXT_FILTER

SecurityContextPersistenceFilter

CONCURRENT_SESSION_FILTER

ConcurrentSessionFilter

LOGOUT_FILTER

LogoutFilter

X509_FILTER

X509AuthenticationFilter

PRE_AUTH_FILTER

AstractPreAuthenticatedProcessingFilter 的子类

CAS_FILTER

CasAuthenticationFilter

FORM_LOGIN_FILTER

UsernamePasswordAuthenticationFilter

BASIC_AUTH_FILTER

BasicAuthenticationFilter

SERVLET_API_SUPPORT_FILTER

SecurityContextHolderAwareRequestFilter

JAAS_API_SUPPORT_FILTER

JaasApiIntegrationFilter

REMEMBER_ME_FILTER

RememberMeAuthenticationFilter

ANONYMOUS_FILTER

AnonymousAuthenticationFilter

SESSION_MANAGEMENT_FILTER

SessionManagementFilter

EXCEPTION_TRANSLATION_FILTER

ExceptionTranslationFilter

FILTER_SECURITY_INTERCEPTOR

FilterSecurityInterceptor

SWITCH_USER_FILTER

SwitchUserFilter

 

 

完毕,good luck!

 

你可能感兴趣的:(CAS客户端与SpringSecurity集成)