【问题标题】:Spring Security SAML Extension and @PreAuthorizeSpring Security SAML 扩展和@PreAuthorize
【发布时间】:2015-07-30 16:07:54
【问题描述】:

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

  1. 使用 Spring SAML 扩展为 SP 配置应用程序。 [完成]
  2. 检索断言并分配角色 [完成]
  3. 创建休息端点。 [完成]
  4. 基于角色的安全休息端点和服务。 [不工作]

我已经实现了 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 没有触发吗?我是否缺少一些配置?

【问题讨论】:

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


    【解决方案1】:

    &lt;security:global-method-security pre-post-annotations="enabled" /&gt; 需要进入其余控制器的 servlet xml,而不是安全上下文 xml。

    参考: http://docs.spring.io/spring-security/site/faq/faq.html#faq-method-security-in-web-context

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。我想使用 SAML 身份验证执行 API 授权。要使其工作,您需要使用带有注释的hasAuthority 参数而不是hasRole 参数。 以下对我有用:

      @PreAuthorize(value="hasAuthority('Admin')")
      

      【讨论】:

        猜你喜欢
        • 2020-05-31
        • 2015-08-27
        • 2014-10-14
        • 2014-12-17
        • 1970-01-01
        • 2013-03-02
        • 2018-03-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多