我的要求是使用基于SAML的SSO。从SAML断言中检索用户组并保护其余的api端点。我正在使用Spring安全SAML扩展和Spring MVC。我采取的步骤是。
我已经实现了SAMLUserDetailsService,它返回带有权限的UserDetails对象。 'loadUserBySAML'在下面。
@Override
public Object loadUserBySAML(SAMLCredential credential) throws UsernameNotFoundException {
final String userId = credential.getNameID().getValue();
final String emailAddress = credential.getAttributeAsString("EmailAddress");
final String firstName = credential.getAttributeAsString("FirstName");
final String lastName = credential.getAttributeAsString("LastName");
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("ROLE_STUDENT"));
return new User(userId, emailAddress, firstName, lastName, authorities);
}
我已将<!-- Enable security annotations on methods -->
<security:global-method-security pre-post-annotations="enabled" />
添加到securityContext.xml。
在RestController和我使用@PreAuthorize的服务上,但这个注释似乎根本没有效果。
@PreAuthorize("hasRole('ROLE_PROGRAMLEAD')")
@RequestMapping(method = RequestMethod.GET)
public String hello() {
return "Hello.";}
有人可以帮我理解为什么PreAuthorize没有解雇?我错过了一些配置吗?
答案 0 :(得分:1)
我面临着同样的问题。我想通过SAML身份验证执行API授权。为了使其正常工作,您需要使用带有注释的hasAuthority
参数而不是hasRole
参数。
以下对我有用:
@PreAuthorize(value="hasAuthority('Admin')")
答案 1 :(得分:0)
<security:global-method-security pre-post-annotations="enabled" />
需要进入其余控制器的servlet xml,而不是安全上下文xml。
参考: http://docs.spring.io/spring-security/site/faq/faq.html#faq-method-security-in-web-context