Cas

CAS客户端使用SpringSecurity访问CAS发布的属性

2020-09-25  本文已影响0人  飘逸峰

摘要

cas服务配置

{
  "@class" : "org.apereo.cas.services.RegexRegisteredService",
  "serviceId" : "^(https|http|imaps)://localhost:8081.*", #对应客户端的地址匹配路径
  "name" : "client1",  #与文件名称对应
  "id" : 10000005,     #与文件名称对应
  "description" : "This service definition authorizes all application urls that support HTTPS and IMAPS protocols.",
  "evaluationOrder" : 10, #顺序id不能重复
  "logoutType" : "BACK_CHANNEL", #固定写法
  "logoutUrl" : "http://localhost:8081/",  #客户端地址,用于单点登出时通知客户端使用
  "attributeReleasePolicy": { # 属性返回策略
    "@class": "org.apereo.cas.services.ReturnAllAttributeReleasePolicy"
  },
  "accessStrategy" : { #是否允许该客户端访问单点登录,肯定是要开启的啊
    "@class" : "org.apereo.cas.services.DefaultRegisteredServiceAccessStrategy",
    "enabled" : true,
    "ssoEnabled" : true
  }
}

cas多属性返回

# 返回全部配置属性
"attributeReleasePolicy": {
    "@class": "org.apereo.cas.services.ReturnAllAttributeReleasePolicy"
  }

# 返回指定配置属性,这里指定只返回username和email两个属性
  "attributeReleasePolicy" : {
    "@class" : "org.apereo.cas.services.ReturnAllowedAttributeReleasePolicy",
    "allowedAttributes" : [ "java.util.ArrayList", [ "username", "email" ] ]
  }

动态service

如果使用代码的方式维护service,则也要根据情况来创建返回策略,
参考代码chapter36/cas-overlay-template-5.3.14/src/main/java/com/cas/controller/ServiceController.java
# 动态services配置
################################################
# 开启识别Json文件,默认false
# 这一段表示从json文件里面初始化服务,如果我们配置了这个,就会将这写json里面的数据,都会自动导入到数据库中
cas.serviceRegistry.initFromJson=true
cas.serviceRegistry.watcherEnabled=true
#设置配置的服务,一直都有,不会给清除掉 , 第一次使用,需要配置为 create-drop
#create-drop 重启cas服务的时候,就会给干掉
#create  没有表就创建,有就不创建
#none 什么也不做
#update 更新
#Unrecognized legacy `hibernate.hbm2ddl.auto` value : create-drops
cas.serviceRegistry.jpa.ddlAuto=update
#配置将service配置到数据库中
cas.serviceRegistry.jpa.isolateInternalQueries=false
cas.serviceRegistry.jpa.url=${jdbc.url}
cas.serviceRegistry.jpa.user=${jdbc.username}
cas.serviceRegistry.jpa.password=${jdbc.password}
#这个必须是org.hibernate.dialect.MySQL5Dialect ,我就是这个问题导致表创建失败
cas.serviceRegistry.jpa.dialect=org.hibernate.dialect.MySQL5Dialect
cas.serviceRegistry.jpa.driverClass=${jdbc.driver}
cas.serviceRegistry.jpa.leakThreshold=10
cas.serviceRegistry.jpa.batchSize=1
cas.serviceRegistry.jpa.autocommit=true
cas.serviceRegistry.jpa.idleTimeout=5000
#配置结束
################################################
List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from regexregisteredservice where serviceId = '" + serviceId + "'");
if (list != null && list.size() > 0) {
    has_data = true;
    jdbcTemplate.update("delete from regexregisteredservice where serviceId = '" + serviceId + "'");
}

 //执行load生效
 servicesManager.load();

基于配置文件返回属性

#####################################################
## 属性返回
cas.authn.attributeRepository.jdbc[0].sql=select username,password,email from cas_user where username=?

# 格式cas.authn.attributeRepository.jdbc[0].attributes.key=value
# 返回上面查询结果的username,属性key名称也为username ,以下雷同
cas.authn.attributeRepository.jdbc[0].attributes.username=username
cas.authn.attributeRepository.jdbc[0].attributes.password=password
cas.authn.attributeRepository.jdbc[0].attributes.email=email

cas.authn.attributeRepository.jdbc[0].singleRow=true
cas.authn.attributeRepository.jdbc[0].order=0
cas.authn.attributeRepository.jdbc[0].requireAllAttributes=true
# cas.authn.attributeRepository.jdbc[0].caseCanonicalization=NONE|LOWER|UPPER
# cas.authn.attributeRepository.jdbc[0].queryType=OR|AND

cas.authn.attributeRepository.jdbc[0].username=username
#数据库连接
cas.authn.attributeRepository.jdbc[0].url=${jdbc.url}
#数据库dialect配置
cas.authn.attributeRepository.jdbc[0].dialect=${jdbc.dialect}
#数据库用户名
cas.authn.attributeRepository.jdbc[0].user=${jdbc.username}
#数据库用户密码
cas.authn.attributeRepository.jdbc[0].password=${jdbc.password}
#数据库事务自动提交
cas.authn.attributeRepository.jdbc[0].autocommit=true
#数据库驱动
cas.authn.attributeRepository.jdbc[0].driverClass=${jdbc.driver}
#超时配置
cas.authn.attributeRepository.jdbc[0].idleTimeout=5000
cas.authn.attributeRepository.jdbc[0].ddlAuto=none
cas.authn.attributeRepository.jdbc[0].leakThreshold=10
cas.authn.attributeRepository.jdbc[0].batchSize=1
cas.authn.attributeRepository.jdbc[0].dataSourceProxy=false

#####################################################

代码实现返回属性内容

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  org.apereo.cas.config.CasEmbeddedContainerTomcatConfiguration,\
  org.apereo.cas.config.CasEmbeddedContainerTomcatFiltersConfiguration,\
  com.cas.config.MyConfiguration,\ #自定义的其它需要加入spring上下文的bean
  com.cas.config.DataSourceConfig,\ #数据源配置,为了引入JdbcTemplate
  com.cas.config.CustomAuthenticationConfig,\  #自定义登录策略配置
  com.cas.config.CustomerAuthWebflowConfiguration #自定义登录流程配置

cas-client-springsecurity

Principal principal = request.getUserPrincipal();
if(principal instanceof AttributePrincipal){
    //cas传递过来的数据
    Map<String,Object> result =( (AttributePrincipal)principal).getAttributes();
    for(Map.Entry<String, Object> entry :result.entrySet()) {
        String key = entry.getKey();
        Object val = entry.getValue();
        System.out.printf("%s:%s\r\n",key,val);
    }
}
/**
     * cas 认证 Provider
     */
    @Bean
    public CasAuthenticationProvider casAuthenticationProvider() {
        //创建CAS授权认证器
        CasAuthenticationProvider casAuthenticationProvider = new CasAuthenticationProvider();

        //设置Cas授权认证器相关配置
        casAuthenticationProvider.setServiceProperties(serviceProperties());
        //设置票据校验器
        casAuthenticationProvider.setTicketValidator(cas30ServiceTicketValidator());
        casAuthenticationProvider.setKey("casAuthenticationProviderKey");


        //setUserDetailsService和setAuthenticationUserDetailsService只能设置一个,其目的都是为了初始化属性authenticationUserDetailsService
        //setUserDetailsService的类型为UserDetailsService
        //setAuthenticationUserDetailsService的类型为AuthenticationUserDetailsService<CasAssertionAuthenticationToken>
        // 如果使用setUserDetailsService,则其会对UserDetailsService进行封装,new UserDetailsByNameServiceWrapper(userDetailsService),
        // 将其转换为AuthenticationUserDetailsService<CasAssertionAuthenticationToken>类型
        //这里使用setAuthenticationUserDetailsService是为了接收cas服务端返回的属性,因为CasAssertionAuthenticationToken会接收到返回的属性
        casAuthenticationProvider.setAuthenticationUserDetailsService(userDetailsServiceImplByAttrs());

        return casAuthenticationProvider;
    }

    @Bean
    public UserDetailsServiceImplByAttrs userDetailsServiceImplByAttrs(){
        UserDetailsServiceImplByAttrs userDetailsServiceImplByAttrs = new UserDetailsServiceImplByAttrs();
        return userDetailsServiceImplByAttrs;
    }

//实际可以接收的属性
Map<String, Object> objectMap = assertion.getPrincipal().getAttributes();
CasAuthenticationToken principal = (CasAuthenticationToken) request.getUserPrincipal();
UserDetails userDetails = principal.getUserDetails();
Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) userDetails.getAuthorities();
authorities.stream().forEach(System.out::println);
#获取登录的用户名称
String username = request.getRemoteUser();
#获取cas返回属性
CasAuthenticationToken principal = (CasAuthenticationToken) request.getUserPrincipal();
UserDetails userDetails = principal.getUserDetails();
if(userDetails instanceof CustomerUser){
    Map<String, Object> map = ((CustomerUser) userDetails).getMap();
    for (String key : map.keySet()) {
        Object value = map.get(key);
        if (value != null) {
            if (value instanceof List) {
                List list = (List) value;
                Iterator iterator = list.iterator();
                int i = 0;
                while (iterator.hasNext()) {
                    Object object = iterator.next();
                    System.out.println("key:" + key + ",values[" + i + "]:" + object.toString());
                    i++;
                }
            } else {
                System.out.println("key:" + key + ",value:" + value.toString());
            }
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读