【问题标题】:Spring security - allowing anonymous accessSpring security - 允许匿名访问
【发布时间】:2015-10-09 00:24:57
【问题描述】:

我已经在我的 spring-boot 应用程序中实现了 Oauth2。在我的 security-context.xml 中,我有这些行 -

<sec:intercept-url pattern="/trusted/**" access="isAnonymous()" />
<sec:intercept-url pattern="/**" access="isFullyAuthenticated()" />

我希望 /trusted 下的所有内容无需身份验证即可使用。但是,当我尝试访问 /trusted 资源(这些是 RESTful 资源)时,仍然会提示我进行身份验证。

我是不是错过了什么?

[编辑:]我正在使用“提供”的 tomcat 实例运行这个应用程序。

【问题讨论】:

  • isAnonymous() 表示您已配置匿名用户。我会做这样的事情:
  • Stackoverflow 将向某人奖励赏金,但我没有得到有效的答案。

标签: java spring spring-security spring-boot spring-security-oauth2


【解决方案1】:

您只需替换受信任的拦截表达式access 属性,它应该可以工作:

<sec:intercept-url pattern="/trusted/**" filters="none" />
<sec:intercept-url pattern="/**" access="isFullyAuthenticated()" />

虽然 Spring Security 3.1 已经弃用了filters,你应该使用http 标签来达到同样的效果:

<http pattern="/trusted/**" security="none"/>

<http auto-config='true'>
  <intercept-url pattern="/**" access="isFullyAuthenticated()" />
  <form-login login-page='/login.jsp'/>
</http>

You can read more about this here.

【讨论】:

  • 现在我得到 org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
  • 听起来您的身份验证设置有其他问题。
  • 我以 github.com/skate056/spring-security-oauth2-google 为模型,您知道其他一些使用 google oath provider 和 spring boot 的工作示例吗?
【解决方案2】:

你的配置是错误的。现在想象一下正在发生的事情,您告诉 Spring 安全性允许匿名访问 /trusted/** 下的所有内容,这没问题,但随后您再次告诉它限制 /** 下的所有匿名访问 - 这是您应用程序中的每条路径,显然也限制了对/trusted/** 的访问。

你需要把你的配置改成这样:

<sec:intercept-url pattern="/trusted/**" access="isAnonymous()" />
<sec:intercept-url pattern="/secure/**" access="isFullyAuthenticated()" />

它会起作用的。

【讨论】:

  • 你的回答是有道理的,但后来我得到了这个异常 -2015-08-17 20:10:30.760 ERROR 18078 --- [io-8080-exec-10] osboot.context。 web.ErrorPageFilter:由于异常[在SecurityContext中找不到身份验证对象],从请求[/monitor/123]转发到错误页面 org.springframework.security.authentication.AuthenticationCredentialsNotFoundException:在SecurityContext中找不到身份验证对象
【解决方案3】:
<http>
<intercept-url pattern="/trusted/**" access="ROLE_USER,ROLE_GUEST" />
<intercept-url pattern="/messagePost.htm*" access="ROLE_USER" />
<intercept-url pattern="/messageDelete.htm*" access="ROLE_ADMIN" />
<anonymous username="guest" granted-authority="ROLE_GUEST" />
<remember-me />
</http>

&lt;anonymous username="guest" granted-authority="ROLE_GUEST" /&gt;

您可以定义像 ROLE_GUEST 这样的角色,并像上面的代码一样提及。任何匿名成员都可以访问 ROLE_GUEST

下的 url 模式

【讨论】:

    猜你喜欢
    • 2014-09-02
    • 2020-05-10
    • 2018-12-10
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2019-07-21
    相关资源
    最近更新 更多