[AppleScript] 纯文本查看 复制代码
@Component public class LoginFilterTest extends ZuulFilter { private static final Logger LOG = LoggerFactory.getLogger(LoginFilterTest.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 2;//int值来定义过滤器的执行顺序,数值越小优先级越高
}
@Override
public boolean shouldFilter() {// 该过滤器需要执行
return true;
[AppleScript] 纯文本查看 复制代码
}
@Override
public Object run() {
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletResponse response = requestContext.getResponse();
HttpServletRequest request = requestContext.getRequest();
//取出头部信息Authorization
String authorization = request.getHeader("Authorization");
if(StringUtils.isEmpty(authorization)){
requestContext.setSendZuulResponse(false);// 拒绝访问
requestContext.setResponseStatusCode(200);// 设置响应状态码 ResponseResult unauthenticated = new ResponseResult(CommonCode.UNAUTHENTICATED);
String jsonString = JSON.toJSONString(unauthenticated);
requestContext.setResponseBody(jsonString);
requestContext.getResponse().setContentType("application/json;charset=UTF‐8");
return null;
}
return null;
}
}