2014-05-20 86 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