【问题标题】:Authenticated and UnAuthenticated views of the same resource in Spring SecuritySpring Security 中同一资源的 Authenticated 和 UnAuthenticated 视图
【发布时间】:2011-07-29 16:40:20
【问题描述】:

我正在尝试创建一个可供经过身份验证和未经身份验证的用户访问的资源。我也有记住我的身份验证。对于访问资源(页面)的用户,我希望记住我的身份验证来尝试登录该人。如果登录不成功,我希望页面仍然显示但行为不同。当我编写安全拦截 url 时,我可以选择“none”或“Role_User”。我可以让 spring 以尝试 ROLE_USER 的方式工作,如果它没有成功,那么它应该优雅地降级为“None”吗?这是我的 spring 配置的外观。

<security:http auto-config='true'>
 <security:intercept-url pattern="/dynamicPage" filters="none"/>
  <security:intercept-url pattern="/**" access="ROLE_USER" />
  <security:form-login login-page="/index"
        default-target-url="/home" always-use-default-target="true"
        authentication-success-handler-ref="AuthenticationSuccessHandler"        
        login-processing-url="/j_spring_security_check" 
        authentication-failure-url="/index?error=true"/>
   <security:remember-me key="myLongSecretCookieKey" token-validity-seconds="1296000" 
        data-source-ref="jdbcDataSource" user-service-ref="AppUserDetailsService" />
</security:http>

我希望动态页面尝试使用记住我身份验证的“Role_User”,等待失败然后回退到“无”。有推荐的方法吗?

我对 isRememberMe() 和链式表达式等表达式一无所知,但我仍然不知道如何解决这个问题。我想要的是这样的。

<security:intercept-url pattern="/dynamicPage" filters="ROLE_USER,none"/>

由于 getAuthorities 查询没有返回 none,我需要找到其他方法来执行此操作。

【问题讨论】:

  • 我只是阅读了表达式,但认为这不能解决我的问题。表达式在处理多个身份验证级别时看起来很有意义。

标签: java authentication spring-security remember-me spring-3


【解决方案1】:

我会使用“匿名身份验证”,即未经身份验证的用户。

添加

<security:anonymous key="anonymous-security" />

到你的配置和

<security:intercept-url pattern="/dynamicPage" access="IS_AUTHENTICATED_REMEMBERED,IS_AUTHENTICATED_ANONYMOUSLY"/>

希望有帮助(我没试过)

【讨论】:

  • 谢谢。这行得通。不过有一个小的变化。而不是 filters="IS...." 它应该是 access="IS_......"
猜你喜欢
  • 2019-10-02
  • 2020-09-18
  • 2018-01-18
  • 2016-06-18
  • 2012-12-26
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
  • 2011-05-23
相关资源
最近更新 更多