當前位置: 首頁>>代碼示例>>Java>>正文


Java RequestContext.setResponseStatusCode方法代碼示例

本文整理匯總了Java中com.netflix.zuul.context.RequestContext.setResponseStatusCode方法的典型用法代碼示例。如果您正苦於以下問題:Java RequestContext.setResponseStatusCode方法的具體用法?Java RequestContext.setResponseStatusCode怎麽用?Java RequestContext.setResponseStatusCode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.netflix.zuul.context.RequestContext的用法示例。


在下文中一共展示了RequestContext.setResponseStatusCode方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: run

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();

    Object token = request.getHeaders("token");

    //校驗token
    if (token == null || "".equals(token)) {
        logger.info("token為空,禁止訪問!");
        ctx.setSendZuulResponse(false);
        ctx.setResponseStatusCode(401);
        return null;
    } else {
        ctx.setSendZuulResponse(true);
        ctx.setResponseStatusCode(200);
    }

    //添加Basic Auth認證信息
    ctx.addZuulRequestHeader("Authorization", "Basic " + getBase64Credentials("app01", "*****"));

    return null;
}
 
開發者ID:kenly333,項目名稱:service-hive,代碼行數:24,代碼來源:AccessTokenFilter.java

示例2: run

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
@Override
public Object run() {
    RequestContext ctx= RequestContext.getCurrentContext();
    HttpServletRequest req=ctx.getRequest();
    String ipAddr=this.getIpAddr(req);
    logger.info("請求IP地址為:[{}]",ipAddr);
   //配置本地IP白名單,生產環境可放入數據庫或者redis中
    List<String> ips=new ArrayList<String>();
    ips.add("0:0:0:0:0:0:0:1");
    ips.add("172.0.0.1");
    ips.add("10.17.30.1");


    if(!ips.contains(ipAddr)){
        logger.info("IP地址校驗不通過!!!");
        ctx.setResponseStatusCode(401);
        ctx.setSendZuulResponse(false);
        ctx.setResponseBody("IpAddr is forbidden!");
    }else{
        logger.info("IP地址校驗通過.");
    }
    return null;
}
 
開發者ID:gengkangkang,項目名稱:springcloud,代碼行數:24,代碼來源:IPFilter.java

示例3: run

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
@Override
public Object run() {
    RequestContext requestContext = RequestContext.getCurrentContext();
    try {
        InputStream in = requestContext.getResponseDataStream();
        StringBuffer result = new StringBuffer();
        byte[] b = new byte[1024];
        for (int n; (n = in.read(b)) != -1; ) {
            result.append(new String(b, 0, n));
        }
        LOGGER.info("接口返回結果:{}", result.toString());
        requestContext.setSendZuulResponse(true);
        requestContext.setResponseStatusCode(200);
        requestContext.setResponseBody(result.toString());
    } catch (Exception e) {
        LOGGER.error("解析返回值異常:{}", e);
    }
    return null;
}
 
開發者ID:hulvyou,項目名稱:spring-cloud-template,代碼行數:20,代碼來源:CustomZuulFilter.java

示例4: run

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
/**
 * filter 具體的邏輯
 */
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    String uri = request.getRequestURI();
    if (uri.equals("/hi-service/upload") || uri.equals("/zuul/hi-service/upload")) {
        return ctx;
    }
    String token = request.getHeader("x-auth-token");
    if (StringUtils.isEmpty(token)) {
        ctx.setSendZuulResponse(false);
        ctx.setResponseStatusCode(401);
        ctx.setResponseBody("no token");
        return null;
    }
    if (!TOKEN_AUTH.equals(token)) {
        ctx.setSendZuulResponse(false);
        ctx.setResponseStatusCode(401);
        ctx.setResponseBody("token auth fail");
        return null;
    }
    ctx.addZuulRequestHeader("userInfo", "{\"name\":\"Tom\",\"age\":18}");
    return ctx;
}
 
開發者ID:yangdd1205,項目名稱:spring-cloud-master,代碼行數:28,代碼來源:CustomAuthFilter.java

示例5: run

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
/**
 * 過濾邏輯
 * @return 過濾結果
 */
@Override
public Object run() {
    RequestContext requestContext = RequestContext.getCurrentContext();
    HttpServletRequest request = requestContext.getRequest();

    logger.info("send {} request to {}",request.getMethod(),request.getRequestURL().toString());

    Object accessToken = request.getHeader("Authorization");
    if (accessToken==null){
        logger.warn("Authorization token is empty");
        requestContext.setSendZuulResponse(false);
        requestContext.setResponseStatusCode(401);
        requestContext.setResponseBody("Authorization token is empty");
        return null;
    }
    logger.info("Authorization token is ok");


    return null;
}
 
開發者ID:lanshiqin,項目名稱:cloud-project,代碼行數:25,代碼來源:AccessFilter.java

示例6: run

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    logger.info("send {} request to {}",request.getMethod(), request.getRequestURL().toString());

    Object accessToken = request.getParameter("accessToken");
    if(accessToken == null){
        logger.warn("access token is empty");
        ctx.setSendZuulResponse(false);
        ctx.setResponseStatusCode(401);
        return null;
    }
    logger.info("access token ok");
    return null;
}
 
開發者ID:frank-jian,項目名稱:SpringCloudDemo,代碼行數:17,代碼來源:AccessFilter.java

示例7: run

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
@Override
public Object run() {
	RequestContext context = RequestContext.getCurrentContext();
	HttpServletRequest request = context.getRequest();

       Long remainingLimit = null;
	for (RateLimitCheck rl : filterConfig.getRateLimitChecks()) {
		ConsumptionProbe probe = rl.rateLimit(request);
		if (probe != null) {
			if (probe.isConsumed()) {
				remainingLimit = getRemainingLimit(remainingLimit, probe);
			} else {
				context.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
				context.setResponseBody(filterConfig.getHttpResponseBody());
				context.setSendZuulResponse(false);
			}
			if(filterConfig.getStrategy().equals(RateLimitConditionMatchingStrategy.FIRST)) {
				break;
			}
		}
	};

	return null;
}
 
開發者ID:MarcGiffing,項目名稱:bucket4j-spring-boot-starter,代碼行數:25,代碼來源:ServletRateLimitFilter.java

示例8: run

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
@Override
public Object run() {
	RequestContext context = getCurrentRequestContext();
	HttpServletRequest request = context.getRequest();

       Long remainingLimit = null;
	for (RateLimitCheck rl : filterConfig.getRateLimitChecks()) {
		ConsumptionProbe probe = rl.rateLimit(request);
		if (probe != null) {
			if (probe.isConsumed()) {
				remainingLimit = getRemainingLimit(remainingLimit, probe);
			} else {
				context.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
				context.setResponseBody(filterConfig.getHttpResponseBody());
				context.setSendZuulResponse(false);
				break;
			}
			if(filterConfig.getStrategy().equals(RateLimitConditionMatchingStrategy.FIRST)) {
				break;
			}
		}
	};

	return null;
}
 
開發者ID:MarcGiffing,項目名稱:bucket4j-spring-boot-starter,代碼行數:26,代碼來源:ZuulRateLimitFilter.java

示例9: run

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    String auth = request.getHeader("Authorization");
    String accessToken = auth.split(" ")[1];
    ctx.set("LimitAccess", Boolean.TRUE);
    try {
        Triple<Long, String, Long> clientTriple = databaseUserDetailService.loadClientByToken(accessToken);
        String user = clientTriple.getMiddle();
        Long intervalInMills = clientTriple.getLeft();
        Long limits = clientTriple.getRight();
        if (intervalInMills != null && intervalInMills != 0l && limits != null && limits != 0l) {
            if (!access(user, intervalInMills, limits)) {
                ctx.set("LimitAccess", Boolean.FALSE);
                ctx.setSendZuulResponse(false);
                ctx.setResponseStatusCode(HttpServletResponse.SC_BAD_REQUEST);
                ctx.setResponseBody("The times of usage is limited");
            }
        }
    } catch (Throwable e) {
    }
    return null;
}
 
開發者ID:venus-boot,項目名稱:saluki,代碼行數:25,代碼來源:LimitAccessFilter.java

示例10: run

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
@Override
public Object run() {

    RequestContext currentContext = RequestContext.getCurrentContext();
    HttpServletRequest request = currentContext.getRequest();
    String token = request.getParameter("token");
    if (StringUtil.isEmpty(token)){
        logger.warn("need token");
        //過濾請求
        currentContext.setSendZuulResponse(false);
        currentContext.setResponseStatusCode(401);
        return null ;
    }
    logger.info("token ={}",token) ;

    return null;
}
 
開發者ID:crossoverJie,項目名稱:springboot-cloud,代碼行數:18,代碼來源:RequestFilter.java

示例11: run

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
/**
 * 過濾器的具體邏輯。需要注意,這裏我們通過ctx.setSendZuulResponse(false)令zuul過濾該請求,不對其進行路由,
 * 然後通過ctx.setResponseStatusCode(401)設置了其返回的錯誤碼,當然我們也可以進一步優化我們的返回,
 * 比如,通過ctx.setResponseBody(body)對返回body內容進行編輯等。
 */
@Override
public Object run() {
	RequestContext ctx = RequestContext.getCurrentContext();
	HttpServletRequest request = ctx.getRequest();
	log.info(String.format("%s request to %s", request.getMethod(), request
			.getRequestURL().toString()));
	Object accessToken = request.getParameter("accessToken");
	if (accessToken == null) {
		log.warn("access token is empty");
		ctx.setSendZuulResponse(false);
		ctx.setResponseStatusCode(401);
		return null;
	}
	log.info("access token ok");
	return null;
}
 
開發者ID:hutou-workhouse,項目名稱:miscroServiceHello,代碼行數:22,代碼來源:AccessFilter.java

示例12: run

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
    if (ctx.getResponseBody() == null && !ctx.getResponseGZipped()) {
        ctx.setSendZuulResponse(false);
    }
    log.debug("Access Control: filtered unauthorized access on endpoint {}", ctx.getRequest().getRequestURI());
    return null;
}
 
開發者ID:IBM,項目名稱:Microservices-with-JHipster-and-Spring-Boot,代碼行數:11,代碼來源:AccessControlFilter.java

示例13: responseBodyOf

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
private String responseBodyOf(RequestContext context) {
  try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(MAX_COOKIE_SIZE)) {
    IOUtils.copy(context.getResponseDataStream(), outputStream);
    return outputStream.toString();
  } catch (IOException e) {
    context.setResponseStatusCode(SC_INTERNAL_SERVER_ERROR);
    throw new IllegalStateException("Failed to read response body", e);
  }
}
 
開發者ID:WillemJiang,項目名稱:acmeair,代碼行數:10,代碼來源:LoginCookieUpdateFilter.java

示例14: returnResultWithoutForwardingToZuul

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
private void returnResultWithoutForwardingToZuul(
    RequestContext context,
    String body,
    String contentType) {
  context.setResponseStatusCode(SC_OK);
  context.getResponse().setHeader(CONTENT_TYPE, contentType);
  context.setResponseBody(body);
  context.setSendZuulResponse(false);
}
 
開發者ID:ServiceComb,項目名稱:ServiceComb-Company-WorkShop,代碼行數:10,代碼來源:CacheFetchFilter.java

示例15: apiLimitExceeded

import com.netflix.zuul.context.RequestContext; //導入方法依賴的package包/類
/**
 * Create a Zuul response error when the API limit is exceeded.
 */
private void apiLimitExceeded() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
    if (ctx.getResponseBody() == null) {
        ctx.setResponseBody("API rate limit exceeded");
        ctx.setSendZuulResponse(false);
    }
}
 
開發者ID:deepu105,項目名稱:spring-io,代碼行數:12,代碼來源:RateLimitingFilter.java


注:本文中的com.netflix.zuul.context.RequestContext.setResponseStatusCode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。