Spring Security SAML Extension和@PreAuthorize

时间:2015-05-19 01:27:05

标签: spring-mvc spring-security spring-saml

我的要求是使用基于SAML的SSO。从SAML断言中检索用户组并保护其余的api端点。我正在使用Spring安全SAML扩展和Spring MVC。我采取的步骤是。

  1. 使用Spring SAML扩展配置SP的应用程序。 [完成]
  2. 检索断言并分配角色[完成]
  3. 创建休息端点。 [完成]
  4. 根据角色保护休息端点和服务。 [不工作]
  5. 我已经实现了SAMLUserDetailsS​​ervice,它返回带有权限的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没有解雇?我错过了一些配置吗?

2 个答案:

答案 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