springboot怎么配置拦截器(Springboot登录检查与拦截器设置)

springboot怎么配置拦截器(Springboot登录检查与拦截器设置)(1)

一、Model

package com.gl.model; import java.util.Date; import lombok.Data; @Data public class User { private Integer id; private String username; private String password; private Date createtime; }

二、Mapper映射

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.gl.mapper.LoginMapper"> <select id="findByUsername" resultType="com.gl.model.User"> SELECT * FROM user where username=#{username} </select> </mapper>

三、Mapper接口

package com.gl.mapper; import com.gl.model.User; public interface LoginMapper { public User findByUsername(String username); }

四、Service

package com.gl.service; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.gl.mapper.LoginMapper; @Service public class LoginService { @Resource LoginMapper loginMapper; // 判断用户是否存在 public boolean isUserExist(String username) { if (loginMapper.findByUsername(username) == null) { return false; } else { return true; } } // 根据提供的用户名取得密码 public String getPasswordByUsername(String username) { return loginMapper.findByUsername(username).getPassword(); } }

五、Controller

package com.gl.controller; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.requestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.gl.service.LoginService; @Controller public class LoginController { @Resource LoginService loginservice; // 定位登录页面 @RequestMapping({ "/", "/login.html" }) public String toLoginPage() { return "/login"; } // @RequestMapping(value = "/user/login", method = RequestMethod.POST) @PostMapping("/login") public String login(@RequestParam("username") String username, @RequestParam("password") String password, Map<String, Object> map, HttpSession session) { if (!loginservice.isUserExist(username)) { map.put("msg", "用户名不存在!"); } else { if (password.equals(loginservice.getPasswordByUsername(username))) { session.setAttribute("loginUserName", username); map.put("msg", "登录成功!"); } else { map.put("msg", "登录密码错误"); return "/login"; } } return "redirect:/index.html"; } }

六、Thymeleaf

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <h2>登录</h2> <form action="index.html" th:action="@{/login}" method="post"> <p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p> 用户名:<input type="text" name="username"><br /> 密码:<input type="text" name="password"><br /> <input id="btn-query" type="submit" value="登录" /> </form> </body> </html>

七、component

package com.gl.component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; /** * 登陆检查 */ public class LoginHandlerInterceptor implements HandlerInterceptor { // 目标方法执行之前 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Object user = request.getSession().getAttribute("loginUserName"); if (user == null) { // 未登陆,返回登陆页面 request.setAttribute("msg", "没有权限请先登陆"); request.getRequestDispatcher("/login.html").forward(request, response); return false; } else { // 已登陆,放行请求 return true; } } }

八、config

package com.gl.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import com.gl.component.LoginHandlerInterceptor; @Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { // SpringBoot已经做好了静态资源映射 registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/login.html", "/", "/login"); } }

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页