Spring boot 如何使用Spring Security检索CAS服务器发送的属性和用户名
我有一个spring引导应用程序,本质上是MVC。此应用程序的所有页面都由CAS SSO进行身份验证。 我使用了spring security cas,如中所述 一切正常。但是,我有一个问题——我无法检索属性 以及CAS服务器在以下@Bean中发送的用户名。我需要做什么来检索所有属性 和CAS服务器发送的用户名Spring boot 如何使用Spring Security检索CAS服务器发送的属性和用户名,spring-boot,spring-mvc,spring-security,cas,Spring Boot,Spring Mvc,Spring Security,Cas,我有一个spring引导应用程序,本质上是MVC。此应用程序的所有页面都由CAS SSO进行身份验证。 我使用了spring security cas,如中所述 一切正常。但是,我有一个问题——我无法检索属性 以及CAS服务器在以下@Bean中发送的用户名。我需要做什么来检索所有属性 和CAS服务器发送的用户名 @Bean public CasAuthenticationProvider casAuthenticationProvider() { CasAuthenticationPr
@Bean
public CasAuthenticationProvider casAuthenticationProvider() {
CasAuthenticationProvider provider = new CasAuthenticationProvider();
provider.setServiceProperties(serviceProperties());
provider.setTicketValidator(ticketValidator());
provider.setUserDetailsService(
s -> new User("casuser", "Mellon", true, true, true, true,
AuthorityUtils.createAuthorityList("ROLE_ADMIN")));
provider.setKey("CAS_PROVIDER_LOCALHOST_9000");
return provider;
}
首先,您需要在CAS server的attributeRepository部分中配置要检索的源和属性,如:
cas.authn.attributeRepository.jdbc[0].singleRow=false
cas.authn.attributeRepository.jdbc[0].sql=SELECT * FROM USERATTRS WHERE {0}
cas.authn.attributeRepository.jdbc[0].username=username
cas.authn.attributeRepository.jdbc[0].role=role
cas.authn.attributeRepository.jdbc[0].email=email
cas.authn.attributeRepository.jdbc[0].url=jdbc:hsqldb:hsql://localhost:9001/xdb
cas.authn.attributeRepository.jdbc[0].columnMappings.attrname=attrvalue
cas.authn.attributeRepository.defaultAttributesToRelease=username,email,role
检查CAS博客中的示例
然后,您需要在服务中实现AuthenticationUserDetailsService,以读取从CAS身份验证返回的属性,例如:
@Component
public class CasUserDetailService implements AuthenticationUserDetailsService {
@Override
public UserDetails loadUserDetails(Authentication authentication) throws UsernameNotFoundException {
CasAssertionAuthenticationToken casAssertionAuthenticationToken = (CasAssertionAuthenticationToken) authentication;
AttributePrincipal principal = casAssertionAuthenticationToken.getAssertion().getPrincipal();
Map attributes = principal.getAttributes();
String uname = (String) attributes.get("username");
String email = (String) attributes.get("email");
String role = (String) attributes.get("role");
String username = authentication.getName();
Collection<SimpleGrantedAuthority> collection = new ArrayList<SimpleGrantedAuthority>();
collection.add(new SimpleGrantedAuthority(role));
return new User(username, "", collection);
}
}
然后,使用provider.setAuthenticationUserDetailsServicecasUserDetailService调整authenticationProvider 如果用户已成功通过身份验证,则可以使用获取主体。请检查以下以/secured path开头的示例部分是否映射到文档SharedHanks中的索引,以获取答复!您是对的,但首先我必须实现AuthenticationUserDetailsService,如接受答案中所述。