• Springboot2.3实现登录拦截


    package com.pacmp.config.intercepters;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import com.pacmp.dto.User;
    import com.pacmp.vo.ResponseData;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    /**
     * @Author xxxx
     * @Date 2020/6/10 19:17
     * @Version 1.0
     * @Description 登录拦截器
     */
    @Slf4j
    @Component
    public class LoginInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            HttpSession session=request.getSession();
            User user = (User)session.getAttribute("User");
            if(user!=null){
                return true;
            }
            returnJson(response);
            return false;
        }
        @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 {
    
        }
        private void returnJson(HttpServletResponse response){
            PrintWriter writer = null;
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            try {
                writer = response.getWriter();
                ResponseData responseData = new ResponseData();
                responseData.setCode(500);
                responseData.setMsg("您已登出,请重新登录!");
                writer.print(responseData);
            } catch (IOException e){
                log.error("拦截器输出流异常",e);
            } finally {
                if(writer != null){
                    writer.close();
                }
            }
        }
    
    }
    package com.pacmp.config;
    
    import com.pacmp.config.intercepters.LoginInterceptor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    /**
     * @Author xxxxx
     * @Date 2020/6/10 19:30
     * @Version 1.0
     * @Description 注册登录拦截器
     */
    @Slf4j
    @Configuration
    public class InterceptorConfig implements WebMvcConfigurer{
        @Autowired
        private LoginInterceptor interceptor;
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(interceptor)
                    .addPathPatterns("/**")
                    .excludePathPatterns("/login/*");
            log.info("登录拦截器注册成功!");
        }
    }
    package com.pacmp.vo;
    
    import lombok.Data;
    
    import java.io.Serializable;
    
    /**
     * @Author xxxx
     * @Date 2020/6/5 10:41
     * @Version 1.0
     * @Description 返回前端数据
     */
    @Data
    public class ResponseData implements Serializable {
        private int code;
    
        private String msg;
    
        private Object results;
    }
    package com.pacmp.dto;
    
    import lombok.Data;
    
    import java.io.Serializable;
    
    /**
     * @Author xxxx
     * @Date 2020/6/11 11:55
     * @Version 1.0
     * @Description 用户实体类
     */
    @Data
    public class User implements Serializable {
    
        /**
         * 用户id
         */
        private Integer userId;
        /**
         * 用户账号
         */
        private String userName;
    
        private String sessionId;
    }
  • 相关阅读:
    二进制运算
    python魔法函数__dict__和__getattr__的妙用
    logging模块配置笔记
    一个python爬虫工具类
    和我一起学爬虫(一)
    不一样的谷歌搜索
    CentOS6.4安装辅助NIS的流程
    ROS6.16开始支持802.11ac了,扫盲下
    centos 安装 Splunk
    扫盲贴2.5寸移动硬盘的厚度有几种
  • 原文地址:https://www.cnblogs.com/penghq/p/13093978.html
Copyright © 2020-2023  润新知