Zuul modification request header, response header (Sike)

Crazy maker culture circle Java [one hundred million concurrent high flow chat room] actual combat series [ blog park entrance total ]

Architect interview growth + high concurrency essential foundation books [ Netty Zookeeper Redis high concurrency combat ]


Crazy maker culture circle high concurrency environment video, one after another on-line:

  • Windows Redis installed (with video)
  • Linux Redis installed (with video)
  • Windows Zookeeper installation (with video)
  • Linux Zookeeper installation (with video)
  • RabbitMQ offline installation (video tape)
  • Nacos installation (video tape)
  • ElasticSearch installation with video **

Small video and tools needed to Baidu network disk link , see the crazy maker culture circle high concurrency community blog

Foreword

Since SpingSecurity + SpringSession integration scenario involves modifying Zuul request header problem.

Therefore, comb a more comprehensive Zuul modify the request headers, response head of the article .

1 SpingSecurity + SpringSession integrate Scene II

This scenario is a scenario microcells + Unified Gateway : Gateway SpingSecurity achieve authentication token, into the sessionID after transfection, micro monomer routed to the service, SpringSession monomers micro Session sharing service implementation. SessionID held in the token.

Here Insert Picture Description

2 Features:

Zuul gateway SpingSecurity implement token authentication token extracted from sessionID, placed header head, then pass the background microService

Here Insert Picture Description

Reference 3: Zuul filter using modification request and response headers header

A: Modify the request parameters, see Bowen

RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
     Map<String, List<String>> requestQueryParams = ctx.getRequestQueryParams();

        if (requestQueryParams==null) {
            requestQueryParams=new HashMap<>();
        }

        //将要新增的参数添加进去,被调用的微服务可以直接 去取,就想普通的一样,框架会直接注入进去
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("1");
        requestQueryParams.put("test", arrayList);

        ctx.setRequestQueryParams(requestQueryParams);

Two: Modify the request header, see Bowen

RequestContext ctx = RequestContext.getCurrentContext();  
ctx.addZuulRequestHeader("original_requestURL",request.getRequestURL().toString());

Three: to modify the response header, see Bowen

RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletResponse response = ctx.getResponse();
        String info = response.getHeader("info");
        String info_size = response.getHeader("info_size");
        /**
         * 设置响应头,使请求变为文件下载
         */
        ctx.addZuulResponseHeader("Content-Type", "application/octet-stream");
        ctx.addZuulResponseHeader("Content-Disposition", "attachment;fileName=" + info);
        ctx.addZuulResponseHeader("Content-Length", ""+info_size);

4 ZuulFilter modification request header

One pair of header request containing the token intercepts

2 springsecurity responsible sessionid attribute on the request of the

Head 3 filter is responsible for requesting modification request header, the session is added to the micro and services in

package com.crazymaker.springcloud.cloud.center.zuul.config;

import com.crazymaker.springcloud.common.constants.SessionConstants;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class ModifyRequestHeaderFilter extends ZuulFilter {

    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        String uri = request.getRequestURI();
        /**
         * 根据条件去判断是否需要路由,是否需要执行该过滤器
         */
        String token = request.getHeader(SessionConstants.AUTHORIZATION);
        log.info("token=" + token);
        if (!StringUtils.isEmpty(token) ) {
            return true;
        }
        return false;
    }

    /**
     * 修改请求头
     *
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

        String sessionSeed = (String) request.getAttribute(SessionConstants.SESSION_SEED);
        log.info("sessionSeed=" + sessionSeed);

        ctx.addZuulRequestHeader(SessionConstants.SESSION_SEED,sessionSeed);

//        response.addHeader(SessionConstants.SESSION_SEED, sessionId);
        return null;

    }

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 1;
    }

}

5 token到 sessionid

Springsecurity responsible for the authentication token, sessionid then extracted from the token, and placed in attribute request.

In particular, please pay attention to Java concurrency high learning community [ blog park entrance total ]


Finally, tell us about the crazy maker culture circle: Crazy maker culture circle, a highly concurrent Java learning community [ blog park entrance total ]

Crazy maker culture circle, a commitment to delivering: interview + interview must-must-must-interview + base + actual principle books " Netty Zookeeper Redis combat high concurrency "

img


Crazy maker culture circles Java Sike series

  • Java (Netty) chat program [traffic] one hundred million real open source project combat

  • Netty source, principle, JAVA NIO principle
  • Java questions face clean sweep
  • Crazy maker culture circle [blog park entrance total]


Crazy maker culture circles Java Sike series

  • Java (Netty) chat program [traffic] one hundred million real open source project combat

  • Netty source, principle, JAVA NIO principle
  • Java questions face clean sweep
  • Crazy maker culture circle [blog park entrance total]


Guess you like

Origin www.cnblogs.com/crazymakercircle/p/12037587.html