2014-03-04 239 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可能会提供一个令牌,可以在不存储在服务器端的情况下对其进行真实性,完整性和到期检查。 –