当前位置:   article > 正文

杨校老师课堂之SpringBoot整合Thymeleaf-登录拦截器_springboot thymeleaf数据拦截器

springboot thymeleaf数据拦截器
拦截
不拦截
直接访问操作页面
未登录
跳转到登录页面
D到达原定访问的页面
已登录
@Controller
@RequestMapping("index")
public class IndexController {

    /**
     * 去后台首页
     * @param request
     * @return
     */
    @RequestMapping("")
    public  String  toIndex(HttpServletRequest request){
        return "index";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
/**
 *登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor{
    
    @Override
    public boolean preHandler(HttpServletRequest request,HttpServletResponse response,Object handler)
        	throws Exception{
        
        Logger log = LoggerFactory.getLogger(LoginInterceptor.class);
        
        //1. 获取请求地址
        String uri = request.getRequestUri("adminlogin");
        
        // 2. 该地址若存在 则放行
        if(uri.indexOf("adminlogin") >= 0 || uri.indexOf("login") >= 0){
            retrun true;
        }
        
        // 3. 获取会话对象session
        HttpSession session = request.getSession();
        
        // 4.通过会话对象session获取标记ADMIN
        User user = (User)session.setAttribute("ADMIN");
        
        // 5. 通过ADMIN获取到的User对象、进行判断是否存在
        if(user != null){
            //表示已登录、不再拦截
            log.info("表示已登录、不再拦截");
            return true;
        }
        
        // 6. 已拦截
        request.setAttribute("msg","尚未进行登录");
        log.info("表示未登录、已拦截");
        response.sendRedirect(request.getConetxtPath + "/adminlogin");
        return false;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
@Controller
public class UserController {
    @RequestMapping("login")
    public String login(User user, HttpServletRequest request){

        User u = userService.userLogin(user);

        System.out.println("u=" + u);

        // 判断登录是不是管理员
        if (u.getType() == 1){
            //  确定是管理员
            request.getSession().setAttribute("ADMIN",u);
            //  重定向到IndexContrller中的index方法。进入到后台首页
            return "redirect:/index";
        }else  if (u.getType() == 0){
            //  确定是 普通用户
            request.getSession().setAttribute("USER",u);
            //  重定向到IndexContrller中的index/index方法。进入到前台首页
            return "redirect:/index/index";
        }else{
         //  确定是 普通用户
            request.setAttribute("msg","您的账户或密码有误,请检查后登录!");
            return "redirect:/adminlogin";
        }
	}
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
配置:
@Configuration
public class LoginConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
//  拦截路径: 项目下所有拦截
registration.addPathPatterns("/**");
//  不拦截路径: 
registration.excludePathPatterns("/login,/**/*.html","/**/*.css","/**/*.png","/**/*.js","/**/*.jpg");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("index.html").setViewName("index");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LOGIN</title>
    <link rel="stylesheet" th:href="@{/static/component/style/components.css}" href="../static/component/style/components.css">
    <link rel="stylesheet" th:href="@{/static/css/bootstrap.css}" href="../static/css/bootstrap.css">
    <link rel="stylesheet" th:href="@{/static/css/plugins.css}" href="../static/css/plugins.css">
    <link rel="stylesheet" th:href="@{/static/css/main.css}" href="../static/css/main.css">
    <link rel="stylesheet" th:href="@{/static/css/themes.css}" href="../static/css/themes.css">
    <script th:src="@{/static/component/js/JQuery2.1.4.js}"></script>
    <script th:src="@{/static/component/plugins/layer/layer.js}"></script>
</head>
	<body>
	    <div id="login">
	        <form action="/login" method="post">
	        <div class="center">
	            <dl>
	                <dt><i class="gi gi-leaf"></i>Lover's · <span> BookHouse</span></dt>
	                <dd><span><i class="fa fa-fw fa-user"></i></span><input type="text" name="usercode" placeholder="请输入账号"></dd>
	                <dd><span><i class="fa fa-fw fa-lock"></i></span><input type="password" name="password" placeholder="请输入密码"></dd>
	                <dd><button type="submit">登录</button></dd>
	            </dl>
	        </div>
	   	 </form>
	    </div>
	</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/204909
推荐阅读
相关标签
  

闽ICP备14008679号