springboot_简单用户登录和拦截器
1、为登录页面的form表单,添加th:action进入controller
<form class="form-signin" th:action="@{/user/login}">
2、input标签添加“name”属性
<input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
<input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
3、进入controller实现具体的业务代码
新建controller
@Controller
public class LoginController {
@RequestMapping("/user/login")
public String login(
@RequestParam("username")String username,
@RequestParam("password")String password,
Model model,
HttpSession session){
//具体业务:
if (!StringUtils.isEmpty(username) && "123456".equals(password)){
System.out.println(username+":"+password);
session.setAttribute("loginUser",username);
//重定向
return "redirect:/main.html";
}else {
model.addAttribute("msg","用户名或密码错误");
return "index";
}
}
}
如果密码错误页面显示错误信息:
<!--th:if thymeleaf模板语法,进行条件判断-->
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
img.png
- @RequestParam:获取登录页面的username和password参数
- Model:进行数据的回显
- HttpSession:用于设置session,用于后续拦截器的判断条件
4、在MyMvcConfig进行地址映射
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
}
映射前uri:
http://localhost:8080/user/login?username=123&password=123456
映射后uri:
http://localhost:8080/main.html
5、拦截器设置
创建LoginHandlerInterceptor实现HandlerInterceptor接口
重写方法:
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {}
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//登录成功之后。应该有用户的session
Object loginUser = request.getSession().getAttribute("loginUser");
//没有登录
if (loginUser==null){
request.setAttribute("msg","请先登录");
//返回主页,转发request和response
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}else {
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
6、在MyMvcConfig中配置拦截器
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
//配置拦截器
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/index.html",
"/",
"/user/login",
"/css/**",
"/js/**",
"/img/**");
}
}
如果没有登录提示用户登录
img_1.png