我有一个spring引导应用程序,它本质上是MVC。此应用程序的所有页面都由CAS SSO进行身份验证。我使用了在https://www.baeldung.com/spring-security-cas-sso中描述的"spring-security-cas“,一切都像预期的那样工作正常。然而,我有一个问题-那就是,我不能在下面的@Bean中检索CAS服务器发送的属性和用户名。要检索CAS服务器发送的所有属性和用户名,我需要做什么?
@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;
}发布于 2020-02-23 21:31:14
首先,您需要在attributeRepository服务器的attributeRepository部分配置CAS源和要检索的属性,如下所示:
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博客中的this示例。
然后,您需要在服务中实现一个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.setAuthenticationUserDetailsService(casUserDetailService);调整您的authenticationProvider
https://stackoverflow.com/questions/60326575
复制相似问题