【问题标题】:Anonymous access of method from Jersey webservice which is secured with spring security and oAuth2来自 Jersey Web 服务的匿名访问方法,该服务由 Spring Security 和 oAuth2 保护
【发布时间】:2015-02-06 20:15:37
【问题描述】:

我有一个 Jersey Rest Web 服务来处理个人帐户 CRUD。
我有 spring security+ oAuth2 来保护这个 api,我无法配置的是,我想让 Account create 方法匿名。我试图配置拦截 url,但它不起作用方法级别。所以我需要为此目的编写单独的类,否则我可以不用它来实现。

示例类代码

public class AccountResource{

createAccount() --- I want this method to be accessed by Anonymous uers so they can create account without generating tokens.
updateAccount() --
findAccount() --
deleteAccont()--

} 

使所有以'/services/rest/**'开始的调用安全的配置代码

<http pattern="/services/rest/**" create-session="never"
        entry-point-ref="oauthAuthenticationEntryPoint"
        xmlns="http://www.springframework.org/schema/security">
        <anonymous enabled="false" />
        <intercept-url pattern="/services/rest/**" method="GET" access="ROLE_USER" />
        <intercept-url pattern="/services/rest/**" method="POST" access="ROLE_USER" />
        <intercept-url pattern="/services/rest/**" method="PUT" access="ROLE_USER" />
        <intercept-url pattern="/services/rest/**" method="DELETE" access="ROLE_USER" />
        <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
        <http-basic entry-point-ref="oauthAuthenticationEntryPoint" />
        <access-denied-handler ref="oauthAccessDeniedHandler" />
    </http>

【问题讨论】:

  • “拦截 url ... 方法级别不起作用”是什么意思?你的 Jersey 资源是 Spring Bean 吗?
  • 请同时添加您的 spring 安全配置。
  • 我的意思是添加新帐户的方法不应该是安全的。
  • @FritzDuchardt,我的 Spring + oAuth2 安全工作正常,我只想允许用户在没有任何安全/令牌的情况下创建新帐户。是的,泽西资源是春豆。
  • @DaveSyer,是的 Jersey 资源是 spring bean。

标签: spring spring-security jersey-2.0 jersey-client spring-security-oauth2


【解决方案1】:

如何将安全配置中的 POST 请求配置更改为:

<intercept-url pattern="/services/rest/**" access="permitAll" method="POST" />

【讨论】:

  • 我已添加为 并且当我发出发布请求时,它会引发异常: by: { error: "unauthorized" error_description: "An Authentication object was not found in the SecurityContext" }
  • 你试过不带匿名标签吗?
  • 你的意思是 吗?
  • 谢谢,它使用匿名但它与其他 http 方法抛出异常:org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'ROLE_USER' cannot be found on 'org.springframework.security.web.access.expression.WebSecurityExpressionRoot' 类型的对象 - 可能不公开?
  • 尝试 access="isFullyAuthenticated()" 而不是 access="ROLE_USER"
猜你喜欢
  • 2018-06-13
  • 2021-01-01
  • 2014-09-13
  • 2015-03-23
  • 2014-12-27
  • 2018-12-08
  • 2011-07-19
  • 2011-04-06
  • 2012-09-10
相关资源
最近更新 更多