您好我已经创建了简单的mvc登录应用程序我期待实现记住我的功能与时间约束,如果记住我被检查,会话应该等到7天,如果用户不检查 记住它应该在30分钟内销毁的会话,然后检查会话是否为null如果它是null然后它应该将其重定向到login.jsp
我使用了这个链接https://www.mkyong.com/spring-security/spring-security-remember-me-example/,因为我无法得到新的春天
的login.jsp
<form id="loginForm" role="form" action="login" method="post">
<fieldset>
<div class="form-group">
<div class=" col-xs-15 control-label">
<input class="form-control" placeholder="E-mail or Username"
name="userName" type="text" autofocus>
</div>
</div>
<div class="form-group">
<div class=" col-xs-15 control-label">
<input class="form-control" placeholder="Password"
name="password" type="password" value="">
</div>
</div>
<div class="checkbox">
<label> <input name="remember" type="checkbox"
value="Remember Me">Remember Me
</label>
</div>
<input type="submit" class="btn btn-lg btn-success btn-block"
value="login" /> <br>
<div class="dropdown">
<button class="btn btn-info dropdown-toggle" type="button"
data-toggle="dropdown">
Registration Yourself <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="StudentSelfRegister.jsp">Student</a></li>
<li><a href="TeacherSelfRegister.jsp">Teacher</a></li>
</ul>
<span><a href="javascript:void(0)"><u>Forgot your password ?</u></a></span>
</div>
</fieldset>
</form>
AppConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.spring")
public class AppConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
AppInitializer
public class AppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
控制器
@Controller
public class LoginController {
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String authenticateUserLogin(@ModelAttribute User loginDTO) {
if (loginDTO.getUserName().equals("admin") && loginDTO.getUserName().equals("123")) {
return "Home";
}
return "Login";
}
}
回到Home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
答案 0 :(得分:1)
我做过类似的事情
<强>控制器强>
public class AccountController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String loginRequestHandler(ModelMap modelMap, HttpSession session, HttpServletRequest request) {
Account acc = checkCookie(request);
if (acc == null) {
modelMap.put("accountForm", new Account());
return "login";
}
else {
AccountModel accountModel = new AccountModel();
if (accountModel.login(acc.getUsername(), acc.getPassword())) {
session.setAttribute("username", acc.getUsername());
return "welcome";
} else {
modelMap.put("errorMSg", "invalid login from cookie");
return "login";
}
}
}
@RequestMapping(value = "/authenticate", method = RequestMethod.POST)
public String loginRequestHandler(@ModelAttribute("accountForm") Account account,ModelMap modelMap,HttpSession session,HttpServletRequest request,HttpServletResponse response) {
AccountModel accountModel=new AccountModel();
if(accountModel.login(account.getUsername(), account.getPassword()))
{
session.setAttribute("username", account.getUsername());
if(request.getParameter("remember")!=null)
{
Cookie ckUsername=new Cookie("username",account.getUsername());
ckUsername.setMaxAge(30);
response.addCookie(ckUsername);
Cookie ckPassword=new Cookie("password",account.getPassword());
ckUsername.setMaxAge(30);
response.addCookie(ckPassword);
}
return "welcome";
}
else
{
modelMap.put("errorMSg", "invalid login not from cookie");
return "login";
}
}
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout(HttpSession session,HttpServletRequest request,HttpServletResponse response) {
session.removeAttribute("username");
for (Cookie ck:request.getCookies()) {
if(ck.getName().equalsIgnoreCase("username")){
ck.setMaxAge(0);
response.addCookie(ck);
}
if(ck.getName().equalsIgnoreCase("password")){
ck.setMaxAge(0);
response.addCookie(ck);
}
}
return "login";
}
public Account checkCookie(HttpServletRequest request) {
Cookie [] cookies=request.getCookies();
Account account=null;
String username="", password="";
for (Cookie ck:cookies) {
if(ck.getName().equalsIgnoreCase("username"))
username=ck.getValue();
if(ck.getName().equalsIgnoreCase("password"))
password=ck.getValue();
}
if(!username.isEmpty() && !password.isEmpty())
account=new Account(username,password);
return account;
}
}
在jsp中
<c:if test="${sessionScope.username != null}">
<c:redirect url = "http://localhost:8080/Test2/"/>
</c:if>
welcome ${sessionScope.username }
<br>
<a href="${pageContext.request.contextPath}/logout">Logout</a>