美文网首页
SpringBoot通过拦截器拦截非登录用户

SpringBoot通过拦截器拦截非登录用户

作者: 2019Say10 | 来源:发表于2019-07-10 22:31 被阅读0次

    使用springmvc中的intercepter进行页面的拦截,拦截除了登录界面的所有页面,当用户登录过后产生保存用户信息的session。在拦截器通过判断session进行页面跳转。
    1.登录页面html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form th:action="@{/}">
    
        <input type="text" name="username">用户名
        <br>
        <input type="password" name="password">密码
        <input type="submit" value="登录">
        <br>
        <p style="color:red;" th:text="${msg}"></p>
    </form>
    </body>
    </html>
    

    2.在自动配置类中配置需要拦截的页面和请求映射

    @Configuration
    public class MvcConfig implements WebMvcConfigurer {
    
        @Bean
         public WebMvcConfigurer  webMvcConfigurer(){
           return new WebMvcConfigurer(){
                @Override
                public void addInterceptors(InterceptorRegistry registry) {
                    InterceptorRegistration interceptorRegistration = registry.addInterceptor(new InterceptLogin());
                    /*拦截除了/ 和/index.html之外的全部请求*/
                    interceptorRegistration.addPathPatterns("/**");
                    interceptorRegistration.excludePathPatterns("/","/index.html");
                }
    
               @Override
               public void addViewControllers(ViewControllerRegistry registry) {
                   registry.addViewController("/index.html").setViewName("index");
               }
           };
         }
    }
    

    3.拦截请求

    public class InterceptLogin implements HandlerInterceptor  {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            HttpSession session = request.getSession();
            /*获取用户信息*/
            Object user = session.getAttribute("user");
            /*如果用户信息为空跳转到登录页面*/
            if(user==null){
                request.setAttribute("msg","请重新登录");
                request.getRequestDispatcher("index.html").forward(request, response);
                return false;
            }
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    
        }
    }
    

    4.业务逻辑层

    @Controller
    public class UserController {
    
        @RequestMapping("/")
        public String userLogin(User user,HttpSession session){
            User user1 = (User) session.getAttribute("user");
            /*如果信息从登陆页面传入,session中存入用户信息*/
            if(user.getUsername()!=null){
                session.setAttribute("user", user);
                return "redirect:/user";
            }
            if(user1!=null){
                return "redirect:/user";
            }
            return "index";
        }
        @ResponseBody
        @RequestMapping("user")
        public String user(HttpSession session){
            User user = (User) session.getAttribute("user");
            return user.toString();
        }
    }
    

    user

    public class User {
        private String username;
        private String password;
        getter..setter....
    }
    

    5.效果图
    直接访问其他的url


    用户登录后

    相关文章

      网友评论

          本文标题:SpringBoot通过拦截器拦截非登录用户

          本文链接:https://www.haomeiwen.com/subject/lbakkctx.html