springboot跨域解决方案

自定义跨域过滤器

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Configuration
@Slf4j
public class CorsFilter implements Filter {

    private String allowOrigin;
    private String allowMethods;
    private String allowCredentials;
    private String allowHeaders;
    private String allowMaxAge;
    private String exposeHeaders;

    @Override
    public void init(FilterConfig filterConfig) {
        allowOrigin = "*";
        allowMethods = "*";
        allowCredentials = "true";
        allowHeaders = "accept, content-type, origin, userid, token";
        exposeHeaders = "*";
        allowMaxAge ="*";
    }


    /**
     * @param req
     * @param res
     * @param chain
     * @throws IOException
     * @throws ServletException
     * @description 通过CORS技术实现AJAX跨域访问, 只要将CORS响应头写入response对象中即可
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
                         FilterChain chain) throws IOException, ServletException {


        HttpServletResponse response = (HttpServletResponse) res;

        HttpServletRequest request = (HttpServletRequest) req;

        String clientOrigin = request.getHeader("Origin");

        if (clientOrigin != null && !ObjectUtils.isEmpty(allowOrigin)) {
            String[] strings = allowOrigin.split(",");
            List<String> serverAllowOrigin = Arrays.asList(strings);
            if("*".equals(allowOrigin)){
                response.setHeader("Access-Control-Allow-Origin", clientOrigin);
            }
            if (serverAllowOrigin.contains(clientOrigin)) {
                response.setHeader("Access-Control-Allow-Origin", clientOrigin);
            }
        }
        if (clientOrigin != null && !ObjectUtils.isEmpty(allowMethods)) {
            response.setHeader("Access-Control-Allow-Methods", allowMethods);
        }
        if (clientOrigin != null && !ObjectUtils.isEmpty(allowCredentials)) {
            response.setHeader("Access-Control-Allow-Credentials", allowCredentials);
        }
        if (clientOrigin != null && !ObjectUtils.isEmpty(allowHeaders)) {
            response.setHeader("Access-Control-Allow-Headers", allowHeaders);
        }
        if (clientOrigin != null && !ObjectUtils.isEmpty(allowMaxAge)) {
            response.setHeader("Access-Control-Max-Age", allowMaxAge);
        }

        if ("OPTIONS".equals(request.getMethod())) {
            response.setStatus(200);
            return;
        }

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值