本文整理汇总了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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
}
示例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);
}
示例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);
}
}