天天看點

SpringBoot 中 登入攔截器的 實作

這裡寫目錄标題

    • 效果展示
    • 參考目錄

LoginController

@Controller
public class LoginController {

    @PostMapping("/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password")String password,
                        Model model, HttpSession session){
        //具體業務
        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
            session.setAttribute("loginUser",username);
            return "redirect:/main.html";
        }else {
            //告訴使用者,你登入失敗了
            model.addAttribute("msg","使用者名或密碼錯誤");
            return "index";
        }
    }
}
           

攔截器

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.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }else{
            return true;
        }

    }
           

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");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) { 
        registry.addInterceptor(new LoginHandlerInterceptor())  //添加 攔截器
                  // 添加 過濾規則,全部過濾。登入請求、登入界面、靜态資源,排除過濾
                 .addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login","/static/**"); 
    }

    //    @Bean          //國際化
//    public LocaleResolver localeResolver(){
//        return new MyLocaleResolver();
//    }




}
           

index.html 登入頁面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/user/login"  method="post">

    <h3 th:text="#{login.tip}" >Please Sign in</h3>
<p th:text="${msg}"></p>
<input name="username" type="text"  th:placeholder="#{login.username}" > <br>
<input name="password" type="password" th:placeholder="#{login.password}"> <br>


<input type="checkbox" value="rember-me">[[#{login.remeber}]] <br>

<button type="submit" th:text="#{login.btn}" > Sign in</button>  <br>

<a th:href="@{/index.html(token punctuation">'zh_CN')}" >中文</a> &nbsp;&nbsp;&nbsp;
<a th:href="@{/index.html(token punctuation">'en_US')}">English</a>

</form>
</body>
</html>
           

dashboard.html 主界面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2 th:text="${session.loginUser}"></h2>

</body>
</html>
           

效果展示

SpringBoot 中 登入攔截器的 實作

參考目錄

B站 狂神

https://www.bilibili.com/video/BV1PE411i7CV?p=4