2014-05-20 88 views
0

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

这里涉及两个角色:管理员和用户
并有2个日期(DATE1 < DATE2)

这些要求:

  • DATE1之前,您无法登录根本
  • 只有管理员可以在日期后登录1
  • 有了date2之后YONE可以访问该页面,则无需申请批准

这是我的弹簧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-网址标签,但我不知道如何做到这一点。

+0

不,你不会。你只需要一个自定义的['UserDetailsChecker'](http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/core/userdetails/UserDetailsChecker.html)记录日期和角色。 –

回答

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; 
    } 
} 
+0

澄清:在第一次约会后,用户需要通过身份验证才能访问该网站。在第二次日期之后,每个人(也是非注册用户)都可以访问网站的所有内容而无需身份验证。 – SubChord

+1

这仍然不能完全清除它 - 你说“只有管理员可以在date1之后登录”,现在你说“在第一次约会之后用户需要验证自己”。你的意思是说你唯一的身份验证用户是管理员?如果您在date2之后不需要身份验证,为什么不在日期过后删除安全性的情况下重新部署? –

+0

您在那里得到了一个有效的重新部署,但没有安全性。我以前没有教过它。谢谢 :) – SubChord