Search in sources :

Example 11 with RequestContext

use of com.netflix.zuul.context.RequestContext in project fw-cloud-framework by liuweijw.

the class AccessFilter method run.

@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.set("startTime", System.currentTimeMillis());
    return null;
}
Also used : RequestContext(com.netflix.zuul.context.RequestContext)

Example 12 with RequestContext

use of com.netflix.zuul.context.RequestContext in project spring-boot by Linda-Tan.

the class AccessFilter method run.

@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    if (isIgnorePath(request.getRequestURI()))
        return null;
    String token = request.getHeader(jwtTokenHeader);
    if (StringUtils.isBlank(token)) {
        log.warn("access token is empty");
        ctx.setSendZuulResponse(false);
        ctx.setResponseStatusCode(401);
        return null;
    }
    try {
        // 校验token合法性
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(RSAHelper.getBase64PublicKey(pubKeyPath)).parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        UserInfo userInfo = new UserInfo();
        userInfo.setId(body.getId());
        userInfo.setName(body.getSubject());
        log.info("parser token : {}", body);
    } catch (Exception e) {
        ctx.setResponseBody(JSON.toJSONString(new BaseResponse(ResponseCode.EX_OTHER_CODE, "Token error or Token is Expired!")));
        log.error(e.getMessage());
    }
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BaseResponse(com.junliang.spring.pojo.vo.BaseResponse) Claims(io.jsonwebtoken.Claims) UserInfo(com.junliang.spring.pojo.bean.UserInfo) RequestContext(com.netflix.zuul.context.RequestContext)

Example 13 with RequestContext

use of com.netflix.zuul.context.RequestContext in project tutorials by eugenp.

the class CustomZuulFilter method run.

@Override
public Object run() {
    final RequestContext ctx = RequestContext.getCurrentContext();
    ctx.addZuulRequestHeader("Test", "TestSample");
    return null;
}
Also used : RequestContext(com.netflix.zuul.context.RequestContext)

Example 14 with RequestContext

use of com.netflix.zuul.context.RequestContext in project tutorials by eugenp.

the class SessionSavingZuulPreFilter method run.

@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();
    HttpSession httpSession = context.getRequest().getSession();
    Session session = repository.getSession(httpSession.getId());
    context.addZuulRequestHeader("Cookie", "SESSION=" + httpSession.getId());
    log.info("ZuulPreFilter session proxy: {}", session.getId());
    return null;
}
Also used : HttpSession(javax.servlet.http.HttpSession) RequestContext(com.netflix.zuul.context.RequestContext) HttpSession(javax.servlet.http.HttpSession) Session(org.springframework.session.Session)

Example 15 with RequestContext

use of com.netflix.zuul.context.RequestContext in project tutorials by eugenp.

the class SwaggerBasePathRewritingFilterIntegrationTest method run_on_valid_response.

@Test
public void run_on_valid_response() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);
    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(false);
    context.setResponse(response);
    InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
    context.setResponseDataStream(in);
    filter.run();
    assertEquals("UTF-8", response.getCharacterEncoding());
    assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) InputStream(java.io.InputStream) RequestContext(com.netflix.zuul.context.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

RequestContext (com.netflix.zuul.context.RequestContext)163 Test (org.junit.Test)59 HttpServletRequest (javax.servlet.http.HttpServletRequest)27 ZuulRoute (org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute)24 Before (org.junit.Before)23 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)22 Set (java.util.Set)10 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)9 ZuulException (com.netflix.zuul.exception.ZuulException)7 InputStream (java.io.InputStream)7 HashSet (java.util.HashSet)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Route (org.springframework.cloud.netflix.zuul.filters.Route)7 ZuulProperties (org.springframework.cloud.netflix.zuul.filters.ZuulProperties)7 IOException (java.io.IOException)6 HttpServletRequestWrapper (com.netflix.zuul.http.HttpServletRequestWrapper)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 List (java.util.List)4 HttpHeaders (org.springframework.http.HttpHeaders)4 RateLimitCheck (com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck)3