中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用Filter攔截器怎么實現請求跨域轉發

發布時間:2021-08-30 09:31:46 來源:億速云 閱讀:155 作者:小新 欄目:開發技術

這篇文章將為大家詳細講解有關使用Filter攔截器怎么實現請求跨域轉發,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

Filter攔截器實現請求跨域轉發

因為公司項目需求,項目中前端請求需要通過一個類似中轉的服務轉發(請求在該服務中會重新包裝一些通用參數)

在使用Filter實現轉發后特做一次記錄

package com.unicloud.cce.Filter; 
import com.alibaba.fastjson.JSON;
import com.unicloud.cce.common.RestfulEntity;
import com.unicloud.cce.service.CloudosService;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate; 
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
 
/**
 * @author skyxt
 * Created 2019-06-11 18:46
 * Email skyxt.yang@gmail.com
 */
@WebFilter(filterName = "authFilter", urlPatterns = { "/*" })
@Component
public class RequestFilter implements Filter { 
 
    //該處配置需要轉發的路徑
    public static final Set<String> FILTER_URL = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
            "/config/*"
    )));
 
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private CloudosService cloudosService;  
    private final static Logger logger = LoggerFactory.getLogger(RequestFilter.class); 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException { 
    }
 
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
            return;
        }
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse rsp = (HttpServletResponse) response;
        AntPathMatcher matcher = new AntPathMatcher();
        Optional<String> cloudIp = Optional.empty();
//        for (String url : FILTER_URL) {
//            if (matcher.match(url, req.getRequestURI().substring(req.getContextPath().length()))) {
//                cloudIp = cloudosService.cloudosList("", "").stream().filter(cloudos ->
//                        cloudos.getId().equals(((HttpServletRequest) request).getHeader("cloudId"))
//                ).map(Cloudos::getCloudIp).findFirst();
//            }
//        }
        cloudIp = Optional.of("localhost");
        if (cloudIp.isPresent()) {
            switch (req.getMethod()) {
                case "GET": {
                    request(req, rsp, HttpMethod.GET, cloudIp.get());
                    break;
                }
                case "POST": {
                    request(req, rsp, HttpMethod.POST, cloudIp.get());
                    break;
                }
                case "PUT": {
                    request(req, rsp, HttpMethod.PUT, cloudIp.get());
                    break;
                }
                case "PATCH": {
                    request(req, rsp, HttpMethod.PATCH, cloudIp.get());
                    break;
                }
                case "DELETE": {
                    request(req, rsp, HttpMethod.DELETE, cloudIp.get());
                    break;
                }
                default:{
                    logger.error("unknow request method:" + req.getMethod());
                    rsp.setCharacterEncoding("UTF-8");
                    try (PrintWriter out = rsp.getWriter()) {
                        out.write("請求方法未知");
                    } catch (Exception e1) {
                        logger.error(e1.getMessage() + e1);
                    }
                } 
            }
        } else {
            chain.doFilter(request, response);
        }
    }
 
    @Override
    public void destroy() { 
    }
 
    private void request(HttpServletRequest req, HttpServletResponse rsp, HttpMethod method, String cloudIp) throws IOException {
        rsp.setCharacterEncoding("UTF-8");
        String requestBody = IOUtils.toString(req.getInputStream(), "UTF-8");
        Object body = null;
        if (StringUtils.hasText(requestBody)) {
            body = JSON.parse(requestBody);
        }
        HttpHeaders headers = new HttpHeaders();
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            headers.add(name, req.getHeader(name));
        }
        String url;
        if (StringUtils.hasText(req.getQueryString())) {
            url = String.format(
                    "http://%s:15120%s?%s",
                    cloudIp,
                    req.getRequestURI().substring(req.getContextPath().length()),
                    req.getQueryString()
                    );
        } else {
            url = String.format(
                    "http://%s:15120%s",
                    cloudIp,
                    req.getRequestURI().substring(req.getContextPath().length())
            );
        }
        HttpEntity<Object> httpEntity = new HttpEntity<>(body, headers);
        ResponseEntity<RestfulEntity> exchange = null;
        try {
            exchange = restTemplate.exchange(
                    url,
                    method,
                    httpEntity,
                    RestfulEntity.class
            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            try (PrintWriter out = rsp.getWriter()) {
                out.write("請求異常");
            } catch (Exception e1) {
                logger.error(e1.getMessage() + e1);
            }
        }
        if (exchange != null) {
            exchange.getStatusCode();
            rsp.setStatus(exchange.getStatusCodeValue());
            exchange.getHeaders().entrySet().stream().forEach(entry -> {
                String value = entry.getValue().toString();
                rsp.addHeader(entry.getKey(), value.substring(1, value.length()-1));
            });
            try (PrintWriter out = rsp.getWriter()) {
                out.write(JSON.toJSONString(exchange.getBody()));
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        } else {
            logger.info("error: URL:" + "http://" + cloudIp + ":15120" + req.getRequestURI().substring(req.getContextPath().length()));
            try (PrintWriter out = rsp.getWriter()) {
                out.write("請求異常");
            } catch (Exception e1) {
                logger.error(e1.getMessage() + e1);
            }
        } 
    }
}

使用filter解決跨域

在web.xml配置攔截器

 <filter>
  <filter-name>servletFilterTest</filter-name>
  <filter-class>cn.test.intercepter.ServletFilterTest</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>servletFilterTest</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

過濾器代碼

import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletFilterTest implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        httpServletRequest.getSession();
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
        httpResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpResponse.setHeader("Access-Control-Allow-Methods", "*");
        httpResponse.setHeader("Access-Control-Max-Age", "3600");
        httpResponse.setHeader("Access-Control-Allow-Headers",
                "Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie");
        httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
        httpResponse.setHeader("Content-type", "application/json");
        httpResponse.setHeader("Cache-Control", "no-cache, must-revalidate");
        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
            return ;
        }
        filterChain.doFilter(httpServletRequest, httpResponse);
    }
    @Override
    public void destroy() {
    }
}

關于“使用Filter攔截器怎么實現請求跨域轉發”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

苍溪县| 斗六市| 疏附县| 谢通门县| 平原县| 德钦县| 阳新县| 英德市| 博罗县| 长春市| 昌平区| 赞皇县| 上虞市| 临沭县| 潞西市| 建宁县| 华坪县| 崇礼县| 吉安市| 新和县| 行唐县| 蒲江县| 隆化县| 郁南县| 昭苏县| 石嘴山市| 中卫市| 邯郸县| 东城区| 同江市| 霸州市| 开远市| 开江县| 诸城市| 汝城县| 太康县| 乐业县| 仪征市| 曲麻莱县| 饶平县| 仪陇县|