【问题标题】:Spring Security OAuth2 - Anonymous-only endpointSpring Security OAuth2 - 匿名端点
【发布时间】:2015-02-21 19:09:06
【问题描述】:

我怎样才能使端点只对匿名用户可访问?我试过了

http
    .authorizeRequests()
    .regexMatchers(HttpMethod.POST, "^/users$")
    .anonymous()

http
    .authorizeRequests()
    .regexMatchers(HttpMethod.POST, "^/users$")
    .hasAnyAuthority("ROLE_ANONYMOUS");

但我总是收到以下消息:

An Authentication object was not found in the SecurityContext

如果这很重要,我的最后一条规则是

http
    .authorizeRequests()
    .anyRequest()
    .denyAll();

以便默认禁止访问每个未配置的端点。

更新:按照 Dave Syer 的建议,我将每个规则合并到同一个链中,但我一直收到相同的消息。这些是我现在的规则:

http.authorizeRequests()
    .regexMatchers(HttpMethod.POST, "^/users$")
    .anonymous()
    .regexMatchers(HttpMethod.GET, "^/me$")
    .hasAnyAuthority("ROLE_ADMIN", "ROLE_USER")
    .anyRequest()
    .denyAll();

有什么建议吗?

【问题讨论】:

    标签: spring-security-oauth2


    【解决方案1】:

    当您调用http.authorizeRequests() 时,您会重置规则,因此通常您只会在每个配置中调用一次。我认为您只需要将您的匿名规则与anyRequest() 合并到authorizeRequests() 的同一链上。

    【讨论】:

    • 谢谢戴夫,但我一直收到同样的信息。你能看看更新的帖子吗?
    • 也许您还必须明确添加匹配器,例如http.antMatchers("/**").
    猜你喜欢
    • 2016-08-12
    • 2015-01-01
    • 2018-12-08
    • 2018-09-07
    • 2019-02-21
    • 2012-10-22
    • 2021-05-19
    • 2019-04-06
    • 2015-08-26
    相关资源
    最近更新 更多