spring boot使用thymeleaf配置登录页面、登录拦截器

1.创建登录页

在项目路径resources/templates下导入资源login.html

login.html例如:
    <form th:action="@{/login}" method="post">
        <input type="text" name="username" >
        <input type="password" name="password">
        <button type="submit">登录</button>
    </form>

2.controller层配置请求映射,使得登录页能够被访问

    @GetMapping("/login")
    public String loginPage(){
        return "login";
    }

3.登录页提交表单跳转到后台首页

    @PostMapping("/login")
    public String index(){
        return "index";
    }

但这样提交表单跳转到后台首页的方式,在我们刷新后台首页页面时会重复提交表单。所以修改如下:提交表单后发送index请求,重定向到index页面。

    @PostMapping("/login")
    public String index(){
        return "redirect:/index";
    }

    @GetMapping("/index")
    public String indexPage(){
        return "index";
    }

现在,解决了表单重复提交问题,但是仍然存在着一些问题,比如直接在地址输入localhost:8888/index就能够访问后台页面,跳过了登录。

为解决此问题,可以用以下逻辑步骤:

  1. 请求访问后台首页时,判断session是否含有用户信息(用户是否登录),如果没有登录则跳转到登录页,并提示登录。
  2. 登录时,把登录信息存在session中。
    @PostMapping("/login")
    public String index(String userName,String password,HttpSession session,Model model){

        if (!StringUtils.isEmpty(userName) && password.equals("123456")){
            session.setAttribute("loginUser",userName);
            return "redirect:/index";
        }else
            model.addAttribute("msg","账号或密码错误");
            return "login";

    }

    @GetMapping("/index")
    public String indexPage(HttpSession session,Model model){
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser!=null){
            return "index";
        }else{
            model.addAttribute("msg","请先登录");
            return "login";
        }
    }

在login.html中添加提示信息:
<label style="color: red" th:text="${msg}"/>

4.配置登录拦截器

后台页面有很多,如果在每个页面都添加以上这样的逻辑会很复杂,通常在开发时使用拦截器、过滤器的方式解决。

拦截器的类名为HandlerInterceptor
image
它有三个方法:

  1. preHandle 目标方法handler执行之前做哪些工作,写在此方法中
  2. postHandle 目标方法执行handler执行以后
  3. afterCompletion 页面渲染以后
1.配置登录拦截器

要访问后台时,在访问之前判断用户是否登录

@Slf4j	//打印日志
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String requestURI = request.getRequestURI();
        log.info("拦截的请求是{}",requestURI);

        HttpSession session = request.getSession();
        Object loginUser = session.getAttribute("loginUser");

        if (loginUser!=null){
            return true;	//true继续执行
        }
        request.setAttribute("msg","请登录后访问");
        request.getRequestDispatcher("/login").forward(request,response);

        return false;	//false拦截请求
    }
}
2.要拦截哪些请求,并把配置放在容器中
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")     //拦截所有请求,包括静态资源
                .excludePathPatterns("/","/login","/css/**","/font/**","/image/**","/js/**","/picture/**","/webfonts/**");     //放行的请求
    }
}

此时,先前后台首页的手动逻辑拦截的方法可以注释掉

    @GetMapping("/index")
    public String indexPage(HttpSession session,Model model){
        return "index";
    }
posted @ 2023-02-28 21:54  ︶ㄣ演戲ㄣ  阅读(920)  评论(0)    收藏  举报