我正在学习春天。执行登录/注销功能。这是我的控制器的样子:
@RequestMapping(value="/successfulLoginAuth", method=RequestMethod.GET)
public ModelAndView postHttpLogin(HttpSession session, Authentication authInfo)
{
ModelAndView mav = new ModelAndView();
mav.setViewName("redirect:/index.html");
session.setAttribute("authInfo", authInfo);
return mav;
}
登录是通过 Spring Security 使用我实现的 dao 服务执行的。这很好用。
这是 index.jsp 的内容:
<%
HttpSession session1 = request.getSession(false);
Authentication authInfo;
if( (session1 != null) &&
( (authInfo = (Authentication)session1.getAttribute("authInfo")) != null)
)
{
out.print(" yo " + authInfo.getName() + " " + authInfo.getAuthorities().iterator().next().getAuthority());
}
else
{
%>
<a href="${pageContext.request.contextPath}/registration">New? Sign Up!</a><br/>
<a href="${pageContext.request.contextPath}/login">Existing? Sign In!</a><br/>
<%} %>
当我登录并重新启动服务器时,我仍然处于登录状态。服务器重新启动后会话信息不应该丢失吗?如果我重新启动浏览器,它会正常工作(即会话信息丢失)。
这是我的 Spring Security 配置:
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/logout" access="permitAll" />
<intercept-url pattern="/accessdenied" access="permitAll" />
<form-login login-page="/login" default-target-url="/successfulLoginAuth" authentication-failure-url="/accessdenied" />
<logout logout-success-url="/logout" />
</http>
<authentication-manager>
<authentication-provider user-service-ref="myUserDetailsService"></authentication-provider>
</authentication-manager>