Java 会话超时后是否可以获取用户名/上次访问的页面
在我的应用程序中,我有一个Java 会话超时后是否可以获取用户名/上次访问的页面,java,spring,cookies,spring-security,httpresponse,Java,Spring,Cookies,Spring Security,Httpresponse,在我的应用程序中,我有一个无效会话url,我想知道是否可以在无效会话url中获取用户名?如果是这样,请建议如何做。是的,这是可能的。当用户通过身份验证时,您可以使用其用户名的值将cookie发送到用户的web浏览器。当会话过期时,您仍然可以访问该cookie。您所需要做的就是将其生命周期设置为足够长 您可以实现自己的过滤器。我建议扩展UsernamePasswordAuthenticationFilter。覆盖Authentication attemptAuthentication(HttpSe
无效会话url
,我想知道是否可以在无效会话url
中获取用户名?如果是这样,请建议如何做。是的,这是可能的。当用户通过身份验证时,您可以使用其用户名的值将cookie发送到用户的web浏览器。当会话过期时,您仍然可以访问该cookie。您所需要做的就是将其生命周期设置为足够长
您可以实现自己的过滤器
。我建议扩展UsernamePasswordAuthenticationFilter
。覆盖Authentication attemptAuthentication(HttpServletRequest,HttpServletResponse)
使您能够访问Cookie-您可以使用HttpServletResponse.addCookie(Cookie Cookie Cookie)
添加Cookie
您可以轻松地注入自己的过滤器。有关配置的更多信息:
还要考虑到发送这样的cookie可能不安全。我看不到任何其他方法来实现你想要的。
但您可以通过配置LogoutHandler
轻松提高此解决方案的安全性。此接口有一个实现CookieClearningLogoutHandler
。当用户决定手动注销时,您可以使用它清除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>
用户名
是的,这是可能的。当用户通过身份验证时,您可以使用其用户名的值将cookie发送到用户的web浏览器。当会话过期时,您仍然可以访问该cookie。您所需要做的就是将其生命周期设置为足够长
您可以实现自己的过滤器
。我建议扩展UsernamePasswordAuthenticationFilter
。覆盖Authentication attemptAuthentication(HttpServletRequest,HttpServletResponse)
使您能够访问Cookie-您可以使用HttpServletResponse.addCookie(Cookie Cookie Cookie)
添加Cookie
您可以轻松地注入自己的过滤器。有关配置的更多信息:
还要考虑到发送这样的cookie可能不安全。我看不到任何其他方法来实现你想要的。
但您可以通过配置LogoutHandler
轻松提高此解决方案的安全性。此接口有一个实现CookieClearningLogoutHandler
。当用户决定手动注销时,您可以使用它清除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>
用户名
我不这么认为。。没有任何方法可以从过期会话中获取值。我不这么认为。。没有任何方法可以从过期会话获取值如何在代码中获取该用户名?还有什么是FilterProcessURL?是否可以在会话超时之前获取最后一页?filterprocesseurl
指定在筛选器中激活操作的URL。对于logoutFilter
filterprocesseurl
指定负责从用户获取注销请求的路径。当用户“单击”URL时,他/她将注销。有关Spring MVC中Cookie的更多信息:在我的java类中如何获取本例中的用户名?检查我在注释中发布的最后一个URL。如何在代码中获取该用户名?还有什么是FilterProcessURL?是否可以在会话超时之前获取最后一页?filterprocesseurl
指定在筛选器中激活操作的URL。对于logoutFilter
filterprocesseurl
指定负责从用户获取注销请求的路径。当用户“单击”URL时,他/她将注销。有关Spring MVC中Cookie的更多信息:在我的java类中如何获取本例中的用户名?请查看我在评论中发布的最后一个URL。