Spring安全授权截止日期

Spring安全授权截止日期,spring,security,spring-mvc,spring-security,Spring,Security,Spring Mvc,Spring Security,我需要创建一个系统,根据日期授予访问网站的权限 涉及两个角色:管理员和用户 有两个日期(日期1

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

涉及两个角色:管理员和用户
有两个日期(日期1<日期2)

这些是要求:

  • 您不能在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>


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

您的要求似乎主要是限制人们是否可以基于日期登录(即验证),但您的问题也涉及基于日期的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之后不需要身份验证,为什么不在date2之后移除安全性重新部署呢?您可以在没有安全性的情况下重新部署。我以前没教过它。谢谢:)