【问题标题】:Spring security authorisation by date按日期的 Spring 安全授权
【发布时间】:2014-07-08 20:00:24
【问题描述】:

我需要创建一个系统,根据日期授予对网站的访问权限。

涉及 2 个角色:管理员和用户
并且有 2 个日期 (date1

这些是要求:

  • date1之前根本无法登录
  • 只有管理员可以在 date1 之后登录
  • date2 之后,每个人都可以访问该页面,无需授权

这是我的 spring-security.xml:

<beans:beans xmlns="http://www.springframework.org/schema/security"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">

<http auto-config="true">
    <intercept-url pattern="/overview**" access="ROLE_ADMIN" />
    <intercept-url pattern="/subscribe**" access="ROLE_ADMIN" />

    <form-login 
        login-page="/login" 
        default-target-url="/overview" 
        authentication-failure-url="/login?error" 
        username-parameter="username"
        password-parameter="password" />
    <logout logout-success-url="/login?logout"  />
    <!-- enable csrf protection -->
    <csrf/>
</http>

<authentication-manager>
    <authentication-provider ref="customAuthProvider">
    </authentication-provider>
</authentication-manager>

</beans:beans>

我的猜测是我需要编写一个自定义元素来替换拦截 URL 标记,但我不知道如何做到这一点。

【问题讨论】:

标签: spring security spring-mvc spring-security


【解决方案1】:

您的要求似乎主要是关于限制人们是否可以根据日期登录(即身份验证),但您的问题还涉及基于日期对 URL 的授权。这些不是一回事,您可能应该明确说明您的意思。例如,当您说第二次约会后每个人都可以访问该页面(哪个页面?)时,您是否也意味着不需要登录?还是您的意思是所有经过身份验证的用户 - 即整个站点仍需要身份验证?

如果您只是在谈论限制登录,那么您可以通过检查自定义 AuthenticationProvider 中的日期来最轻松地做到这一点。比如:

class MyAuthProvider extends SomeStandardAuthenticationProvider {

    public Authentication authenticate (Authentication a) {
        Authentication authenticated = super.authenticate(a);

        Date now = new Date();
        boolean isAdmin = // check authenticated.getAuthorities() for admin role

        if (now.isBefore(date1 || (isAdmin && now.isBefore(date2)) {
            throw new AuthenticationException("Too early");
        }

        return authenticated;
    }
}

【讨论】:

  • 澄清:在第一次约会之后,用户需要验证自己才能访问网站。第二次约会后,所有人,包括非注册用户,都可以访问网站的所有内容而无需认证。
  • 这仍然没有完全清除 - 你说“只有管理员可以在 date1 之后登录”,现在你说的是“在第一次约会之后用户需要验证自己”。您的意思是您可以拥有的唯一经过身份验证的用户是管理员吗?如果您在 date2 之后不需要身份验证,为什么不在日期过去后重新部署并删除安全性?
  • 您有一个有效点可以在没有安全性的情况下重新部署。我以前没教过。谢谢你:)
猜你喜欢
  • 2017-05-10
  • 2012-04-07
  • 2013-11-02
  • 1970-01-01
  • 2017-03-14
  • 2015-09-01
  • 2023-04-01
  • 2015-02-20
相关资源
最近更新 更多