2012-09-21 32 views
0

我在Spring Roo環境中使用Spring-Security 3.1.RELEASE。我已經更改了登錄機制,以便它可以與JSON-Response一起使用。對於我已經創建了兩個類(斯卡拉)即在使用自定義過濾器後持續記住我的身份驗證

class JsonSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { 
override def onAuthenticationSuccess(request: HttpServletRequest, response: HttpServletResponse, authentication: Authentication) = { 
    val responseWrapper = new HttpServletResponseWrapper(response); 
    val out = responseWrapper.getWriter 
    out.write("{success: true}") 
    out.close 
} 
} 

@Repository class JsonEntryPoint extends AuthenticationEntryPoint { 
def commence(request: HttpServletRequest, response: HttpServletResponse, ae: AuthenticationException) = { 
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized") 
} 
} 

在應用security.xml文件中的條目如下:

<http use-expressions="true" entry-point-ref="jsonAuthenticationEntryPoint"> 
    <custom-filter ref="myFilter" position="FORM_LOGIN_FILTER"/> 
    <custom-filter ref="rememberMeFilter" position="REMEMBER_ME_FILTER"/> 

    <logout logout-url="/resources/j_spring_security_logout"/> 

    <intercept-url pattern="/backend/**" access="isAuthenticated()" /> 
    <intercept-url pattern="/resources/**" access="permitAll" /> 
    <intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" /> 
    <intercept-url pattern="/**" access="permitAll" /> 
</http> 


<beans:bean id="rememberMeFilter" class="org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter"> 
    <beans:property name="rememberMeServices" ref="rememberMeServices"/> 
    <beans:property name="authenticationManager" ref="authenticationManager" /> 
</beans:bean> 

<beans:bean id="rememberMeServices" class="org.springframework.security.web.authentication.rememberme.JdbcTokenBasedRememberMeServices"> 
    <beans:property name="userDetailsService" ref="userServiceDb"/> 
    <beans:property name="tokenRepository" ref="tokenRepository"/> 
    <beans:property name="key" value="reservation" /> 
</beans:bean> 

<beans:bean id="tokenRepository" class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl"> 
<beans:property name="dataSource" ref="dataSource"/> 
</beans:bean> 


<beans:bean id="myFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> 
    <beans:property name="authenticationManager" ref="authenticationManager" /> 
    <beans:property name="authenticationSuccessHandler" ref="mySuccessHandler" /> 
    <beans:property name="rememberMeServices" ref="rememberMeServices" /> 
    <beans:property name="usernameParameter" value="email" /> 
    <beans:property name="passwordParameter" value="password" /> 
</beans:bean> 
<beans:bean id="mySuccessHandler" class="JsonSuccessHandler"/> 
<beans:bean id="jsonAuthenticationEntryPoint" class="JsonEntryPoint" /> 

認證工作正常,所以我可以成功登錄,也關於記住我的數據已成功存儲到數據庫(表persistent_logins)。但是,當我重新啓動服務器時,會話正如預期的那樣被刪除,記住我的功能應該可以完成它的工作。不幸的是,它跟隨日誌數據失敗:

[INFO] Started Jetty Server 
17:19:15.867 [qtp1943091306-38] DEBUG o.s.security.web.FilterChainProxy -/at position 1 of 10 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter' 
17:19:15.875 [qtp1943091306-38] DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository - No HttpSession currently exists 
17:19:15.875 [qtp1943091306-38] DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository - No SecurityContext was available from the HttpSession: null. A new one will be created. 
17:19:15.877 [qtp1943091306-38] DEBUG o.s.security.web.FilterChainProxy -/at position 2 of 10 in additional filter chain; firing Filter: 'LogoutFilter' 
17:19:15.878 [qtp1943091306-38] DEBUG o.s.security.web.FilterChainProxy -/at position 3 of 10 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter' 
17:19:15.878 [qtp1943091306-38] DEBUG o.s.security.web.FilterChainProxy -/at position 4 of 10 in additional filter chain; firing Filter: 'RequestCacheAwareFilter' 
17:19:15.878 [qtp1943091306-38] DEBUG o.s.security.web.FilterChainProxy -/at position 5 of 10 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter' 
17:19:15.879 [qtp1943091306-38] DEBUG o.s.security.web.FilterChainProxy -/at position 6 of 10 in additional filter chain; firing Filter: 'RememberMeAuthenticationFilter' 
17:19:15.879 [qtp1943091306-38] DEBUG o.s.s.w.a.r.PersistentTokenBasedRememberMeServices - Remember-me cookie detected 
17:19:15.895 [qtp1943091306-38] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query 
17:19:15.895 [qtp1943091306-38] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [select username,series,token,last_used from persistent_logins where series = ?] 
17:19:15.896 [qtp1943091306-38] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource 
17:19:15.922 [qtp1943091306-38] DEBUG o.s.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource 
17:19:15.938 [qtp1943091306-38] DEBUG o.s.s.w.a.r.PersistentTokenBasedRememberMeServices - Refreshing persistent login token for user '[email protected]', series 'oLmZMQbnFsfyTziANriMKw==' 
17:19:15.939 [qtp1943091306-38] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL update 
17:19:15.944 [qtp1943091306-38] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [update persistent_logins set token = ?, last_used = ? where series = ?] 
17:19:15.944 [qtp1943091306-38] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource 
17:19:15.984 [qtp1943091306-38] DEBUG o.s.jdbc.core.JdbcTemplate - SQL update affected 1 rows 
17:19:15.992 [qtp1943091306-38] DEBUG o.s.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource 
17:19:15.996 [qtp1943091306-38] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query 
17:19:15.996 [qtp1943091306-38] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [select email username, password, isactive enabled from principal where email = ?] 
17:19:15.996 [qtp1943091306-38] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource 
17:19:16.001 [qtp1943091306-38] DEBUG o.s.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource 
17:19:16.013 [qtp1943091306-38] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query 
17:19:16.013 [qtp1943091306-38] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [select p.email username, a.authority from principal p inner join principal_authority apa on p.id = apa.principal_id inner join authority a on a.id = apa.authorities_id where p.email = ?] 
17:19:16.013 [qtp1943091306-38] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource 
17:19:16.015 [qtp1943091306-38] DEBUG o.s.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource 
17:19:16.021 [qtp1943091306-38] DEBUG o.s.s.w.a.r.PersistentTokenBasedRememberMeServices - Remember-me cookie accepted 
17:19:16.036 [qtp1943091306-38] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalScheduledAnnotationProcessor' 
17:19:16.042 [qtp1943091306-38] DEBUG o.s.s.w.a.r.RememberMeAuthenticationFilter - SecurityContextHolder not populated with remember-me token, as AuthenticationManager rejected Authentication returned by RememberMeServices: 'org.spring[email protected]2fb6ff6b: Principal: [email protected]: Username: [email protected]; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_PHYSIOTHERAPEUT; Credentials: [PROTECTED]; Authenticated: true; Details: org.sprin[email protected]957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_PHYSIOTHERAPEUT'; invalidating remember-me token 
org.springframework.security.authentication.ProviderNotFoundException: No AuthenticationProvider found for org.springframework.security.authentication.RememberMeAuthenticationToken 
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:196) ~[spring-security-core-3.1.0.RELEASE.jar:3.1.0.RELEASE] 
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:102) ~[spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE] 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE] 

我錯過了什麼?我不明白爲什麼會引發ProviderNotFoundException。

+0

請打開調試上(添加一行'log4j.logger.org.springframework.security = DEBUG'到log4j.properties)和輸出後(不工作)記住我在你的問題中的要求。 – Xaerxess

+0

好吧,日誌輸出現在發佈 – rainerhahnekamp

+0

調試消息說記住我沒有使用,因爲找到了UsernamePasswordAuthenticationToken,所以這似乎不是記住我的問題。你能發佈**完整**調試輸出,從正常登錄,會話到期和記住我登錄嘗試開始?另外請更確切地定義你的意思是什麼「認證工作正常,但記憶我的功能根本沒有」。 – Xaerxess

回答

2

你差不多完成了,驗證工作正常,你忘了添加RememberMeAuthenticationProvider。如果您使用安全命名空間中創建的AuthenticationManager,只需添加獨立<authentication-provider>喜歡這裏:

<authentication-manager> 
    <authentication-provider ref="yourRegularAuthenticationProvider" /> 
    <authentication-provider ref="rememberMeAuthenticationProvider" /> 
</authentication-manager> 

<beans:bean id="rememberMeAuthenticationProvider" class="org.springframework.security.authentication.RememberMeAuthenticationProvider"> 
    <beans:property name="key" value="reservation" /> 
</beans:bean> 
+0

謝謝,那是缺失的鏈接。只是最後一個問題。這是否在某個地方有記錄或者是否需要通過查看源代碼來找到它? – rainerhahnekamp

+1

它被記錄在[在Remeber-Me章節](http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#remember-me-impls)和[在這裏關於'AuthenticationProvider's](http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#core-services-authentication-manager)的一章中說,每個提供者都能夠嘗試認證,或者通過簡單地返回空值來跳過認證。如果所有的實現都返回null,'ProviderManager'將會拋出一個'ProviderNotFoundException' – Xaerxess

相關問題