In my application I have an invalid-session-url
and I was wondering if it's possible to get the username in the invalid-session-url
? If so please advise how to do that.
1 に答える
はい、可能です。ユーザーが認証されると、ユーザーのユーザー名の値とともに Cookie をユーザーの Web ブラウザーに送信できます。セッションが期限切れになっても、その Cookie にアクセスできます。あなたがする必要があるのは、その寿命を十分に長く設定することだけです.
独自の を実装できますFilter
。延長することをお勧めしUsernamePasswordAuthenticationFilter
ます。オーバーライドAuthentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
すると、Cookie にアクセスできるようになりますHttpServletResponse.addCookie(Cookie cookie)
。
独自のフィルターを簡単に挿入できます。構成に関する詳細情報: http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#nsa-custom-filter
また、このような Cookie を送信することは安全ではない可能性があることも考慮してください。あなたが望むことを達成する他の方法はありません。ただし、構成することで、このソリューションのセキュリティを簡単に向上させることができますLogoutHandler
。このインターフェースの実装がありますCookieClearingLogoutHandler
。ユーザーが手動でログアウトすることを決定したときに、その Cookie をクリアするために使用できます。
<bean id="cookieClearingLogoutHandler" class="org.springframework.security.web.authentication.logout.CookieClearingLogoutHandler">
<constructor-arg>
<!-- Names of the cookies you want to remove when user logs out -->
<list>
<value>username</value>
</list>
</constructor-arg>
</bean>
<bean id="logoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
<constructor-arg value="/login"/>
<constructor-arg>
<array>
<ref local="securityContextLogoutHandler"/>
<!-- Inject it -->
<ref local="cookieClearingLogoutHandler"/>
</array>
</constructor-arg>
<property name="filterProcessesUrl" value="/logout"/>
</bean>