2014-03-04 235 views
1

我想保護REST URL。爲此,我決定採用基於令牌的身份驗證。那麼,我該如何創建帶有過期時間的令牌,以及我可以在哪裏存儲它以供日後驗證令牌檢查?在RESTful API中存儲身份驗證令牌的位置?

在此先感謝。

This is my security.xml 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:beans="http://www.springframework.org/schema/beans" 
      xmlns:sec="http://www.springframework.org/schema/security" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
           http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 
    <beans:import resource="applicationContext.xml"/> 
    <http pattern="/jaxrs/employess/**" create-session="stateless" entry-point-ref="myAuthenticationEntryPoint"> 
     <intercept-url pattern='/jaxrs/employess/**' /> 
      <custom-filter position="PRE_AUTH_FILTER" ref="myAuthenticationFilter" /> 
    </http> 
    <beans:bean id="myAuthenticationEntryPoint" class="restservice.security.MyAuthenticationEntryPoint"/> 
    <global-method-security secured-annotations="enabled" /> 
    <beans:bean id="myAuthenticationFilter" class="restservice.security.MyAuthenticationFilter"> 
     <beans:property name="authenticationManager" ref="authenticationManager"/> 
    </beans:bean> 
    <authentication-manager alias="authenticationManager"> 
     <authentication-provider ref="myAuthenticationProvider"/> 
    </authentication-manager> 
    <!--<beans:bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>--> 
    <beans:bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> 
     <!-- <beans:property name="errorHandler" ref="customErrorHandler" /> --> 
    </beans:bean> 
    <beans:bean id="myAuthenticationProvider" class="restservice.security.MyAuthenticationProvider" > 
     <beans:property name="restTemplate" ref="restTemplate"/> 
    </beans:bean> 
    <!-- <beans:bean id="customErrorHandler" class="com.AuthenticationResponseErrorHandler"/> --> 
</beans:beans>* 
+0

你不知道。 Spring安全性爲你處理。 –

+0

你可以張貼它如何處理彈簧的例子? – sridhar

回答

1

你不應該把它存儲在任何地方,因爲那意味着在服務器上存儲一些會話狀態。

相反,令牌本身應該是一個帶符號的編碼字符串,其中包含您需要識別用戶的信息。您通過檢查簽名來驗證其真實性。如果您需要過期,請在簽名前添加一個時間戳,然後根據當前時間計算令牌年齡。

+0

oauth是否適合此要求? – sridhar

+0

如果我們不應該在REST API數據庫中保存令牌(例如JSON Web令牌),我們如何在SPA - > REST API體系結構中實現「記住我」功能?我能想到的唯一方法是使用SPA - > App server - > REST api體系結構,這樣我們就可以使用會話(保存在應用程序服務器的數據庫中),以便能夠在單頁面應用程序中添加「記住我」功能。 –

+0

該API可能會提供一個令牌,可以在不存儲在服務器端的情況下對其進行真實性,完整性和到期檢查。 –