在CAS Overlay中,如何发送用户属性

时间:2017-01-31 12:36:37

标签: cas

在CAS Overlay中,如何将名称以外的用户属性返回给JAVA中的客户端。我正在使用CAS Overlay项目并将用户详细信息存储在数据库中。

1 个答案:

答案 0 :(得分:0)

最后,我能够将登录用户的用户属性从CAS服务器提取到客户端。

我正在使用CAS Overlay项目版本5.0.0.RC1和Spring Security 4.1.3.RELEASE。

WebSecurityConfigurerAdapter中的Spring客户端配置:

 @Bean
public ServiceProperties serviceProperties() {
    ServiceProperties serviceProperties = new ServiceProperties();
    serviceProperties.setService(serviceUrl);
    serviceProperties.setSendRenew(false);
    return serviceProperties;
}

@Bean
public CasAuthenticationProvider casAuthenticationProvider() {
    CasAuthenticationProvider casAuthenticationProvider = new CasAuthenticationProvider();
    casAuthenticationProvider.setAuthenticationUserDetailsService(authenticationUserDetailsService());
    casAuthenticationProvider.setServiceProperties(serviceProperties());
    casAuthenticationProvider.setTicketValidator(cas30ServiceTicketValidator());
    casAuthenticationProvider.setKey("an_id_for_this_auth_provider_only");
    return casAuthenticationProvider;
}

@Bean
public Cas30ServiceTicketValidator cas30ServiceTicketValidator() {
    return new Cas30ServiceTicketValidator(casServer);
}


@Bean
public AuthenticationUserDetailsService authenticationUserDetailsService(){
    String[] role ={"user_role"};
    return new GrantedAuthorityFromAssertionAttributesUserDetailsService(role);
}



@Bean
public CasAuthenticationFilter casAuthenticationFilter() throws Exception {
    CasAuthenticationFilter casAuthenticationFilter = new CasAuthenticationFilter();
    casAuthenticationFilter.setAuthenticationManager(authenticationManager());
    casAuthenticationFilter.setAuthenticationSuccessHandler(new CustomAuthenticationSuccessHandler());
    casAuthenticationFilter.setAuthenticationFailureHandler(new CustomAuthenticationFailureHandler());
    return casAuthenticationFilter;
}

@Bean
public CasAuthenticationEntryPoint casAuthenticationEntryPoint() {
    CasAuthenticationEntryPoint casAuthenticationEntryPoint = new CasAuthenticationEntryPoint();
    casAuthenticationEntryPoint.setLoginUrl(casServerLogin);
    casAuthenticationEntryPoint.setServiceProperties(serviceProperties());
    return casAuthenticationEntryPoint;
}

@Bean
public LogoutFilter requestSingleLogoutFilter (){

    LogoutFilter logoutFilter = new LogoutFilter(casLogout,new SecurityContextLogoutHandler());
    logoutFilter.setFilterProcessesUrl("/j_spring_cas_security_logout");
    return logoutFilter;
}

@Bean
public SingleSignOutFilter singleSignOutFilter() {
    SingleSignOutFilter filter = new SingleSignOutFilter();
    filter.setCasServerUrlPrefix(casServer);
    filter.setIgnoreInitConfiguration(true);
    return filter;
}

在CAS服务器端配置数据库属性存储库,因为我将用户详细信息存储在数据库中。

<code>
cas.authn.attributeRepository.jdbc.singleRow=true
cas.authn.attributeRepository.jdbc.requireAllAttributes=true
cas.authn.attributeRepository.jdbc.caseCanonicalization=NONE
cas.authn.attributeRepository.jdbc.queryType=OR

cas.authn.attributeRepository.jdbc.sql=SELECT * FROM users  WHERE {0}
cas.authn.attributeRepository.jdbc.username=username
cas.authn.attributeRepository.jdbc.healthQuery=SELECT 1
cas.authn.attributeRepository.jdbc.isolateInternalQueries=false
cas.authn.attributeRepository.jdbc.url=jdbc:postgresql://localhost:5432/casdb
cas.authn.attributeRepository.jdbc.failFast=true
cas.authn.attributeRepository.jdbc.isolationLevelName=ISOLATION_READ_COMMITTED
cas.authn.attributeRepository.jdbc.dialect=org.hibernate.dialect.PostgreSQLDialect
cas.authn.attributeRepository.jdbc.leakThreshold=10
cas.authn.attributeRepository.jdbc.propagationBehaviorName=PROPAGATION_REQUIRED
cas.authn.attributeRepository.jdbc.batchSize=1
cas.authn.attributeRepository.jdbc.user=postgres
cas.authn.attributeRepository.jdbc.ddlAuto=update
cas.authn.attributeRepository.jdbc.password=postgres
cas.authn.attributeRepository.jdbc.autocommit=false
cas.authn.attributeRepository.jdbc.driverClass=org.postgresql.Driver
cas.authn.attributeRepository.jdbc.idleTimeout=5000
cas.authn.attributeRepository.jdbc.pool.suspension=false
cas.authn.attributeRepository.jdbc.pool.minSize=6
cas.authn.attributeRepository.jdbc.pool.maxSize=18
cas.authn.attributeRepository.jdbc.pool.maxIdleTime=1000
cas.authn.attributeRepository.jdbc.pool.maxWait=2000

cas.authn.attributeRepository.attributes.last_name=last_name
cas.authn.attributeRepository.attributes.first_name=first_name
cas.authn.attributeRepository.attributes.user_role=user_role 

</code>

在这些更改之后,我能够从CAS Server获取用户属性。