• springboot在filter中设置跨域


    
    package com.ee.consultation.filter;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.ee.consultation.common.ApiMessage;
    import com.ee.consultation.common.constants.Constants;
    import com.ee.consultation.common.enums.StatusCode;
    import com.ee.consultation.common.util.UserUtils;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.lang3.ObjectUtils;
    import org.springframework.core.annotation.Order;
    import org.springframework.http.HttpStatus;
    
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    /**
     */
    @Slf4j
    @WebFilter(urlPatterns = "/*")
    @Order(1)
    public class TokenFilter implements Filter {
    
        private final ObjectMapper mapper;
    
        public TokenFilter(ObjectMapper mapper) {
            this.mapper = mapper;
        }
    
        @Override
        public void destroy() {
            log.info("filter destroy");
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                             FilterChain chain) throws IOException, ServletException {
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
    
            String originHeader = httpServletRequest.getHeader("Origin");
            httpServletResponse.setHeader("Access-Control-Allow-Origin", originHeader);
            httpServletResponse.addHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With,requestid");
            httpServletResponse.addHeader("Access-Control-Allow-Credentials", "true");
            httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
    
            String method =httpServletRequest.getMethod();
            if ("OPTIONS".equals(method) || "PUT".equals(method)){
                chain.doFilter(request, response);
                return;
            }
    
    
    
            String path = httpServletRequest.getRequestURI();
            if(path.indexOf("index") != -1){
                chain.doFilter(request, response);
                return;
            }
            if (path != null) {
                if (path.contains(Constants.LOGIN)
                        || path.contains(Constants.DOC)
                        || path.contains(Constants.FAVICON)
                        || path.contains(Constants.SWAGGER)
                        || path.contains(Constants.API)
                        || path.contains(Constants.WEBJARS)
                        || path.contains(Constants.HAIRDETECTIONCONTROLLER)
                        || path.contains(Constants.DICTDATA)
                        || path.contains(Constants.DOCTORFILE_SCREEN)
                        || path.contains(Constants.DOCTORFILE_DOCTOR)
                ) {
                    chain.doFilter(request, response);
                    return;
                }
            }
    
            String token = httpServletRequest.getHeader(Constants.AUTHORIZATION);
            if (ObjectUtils.isEmpty(token) || ObjectUtils.isEmpty(UserUtils.getUserId())) {
                httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
                httpServletResponse.setContentType("application/json;charset=utf-8");
                ApiMessage apiMessage = ApiMessage.error(StatusCode.USER_IS_NOT_LOGIN);
                httpServletResponse.getWriter().write(mapper.writeValueAsString(apiMessage));
                return;
            }
            chain.doFilter(request, response);
        }
    
        @Override
        public void init(FilterConfig filterConfig) {
            log.info("filter init");
        }
    }
    
    
    
    • 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
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
  • 相关阅读:
    基于单片机预费电表控制系统(proteus仿真+源程序)
    小程序优点
    通过Excel,生成sql,将A表数据插入B表
    生物素偶联二硒化钨WSe2 (Biotin-WSe2)|羟基修饰PEG化二硒化钨WSe2纳米颗粒 (OH-WSe2)齐岳
    JAVA反射机制实现调用类的方法
    视觉Transformer中的位置编码方式
    1024程序员节过后,我才知道JVM有多重要
    WebDAV之π-Disk派盘 + 纸间书摘
    最小生成树(Prim算法与Kruskal算法)
    react——状态管理(redux)——手写redux的模拟状态管理——订阅和取消
  • 原文地址:https://blog.csdn.net/jerryyang_2017/article/details/132871934