I hope to be able to perform setAttribute ("key", value) operation on request in filter of zuul, and then the downstream service can obtain data through request.getAttribute ("key"); but the result does not happen as expected. Adding attribute to request in filter is not passed to the downstream service.
/**
* Created by zhaoyuening on 2018/1/26.
*/
@Component
public class TokenCheckFilter extends ZuulFilter {
private final String JWT_HEADER_NAME = "token";
private final Logger logger = LoggerFactory.getLogger(TokenCheckFilter.class);
@Value("${token.filter}")
private boolean IS_FILTER;
@Autowired
private RoleResourceManager roleResourceManager;
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 5;
}
@Override
public boolean shouldFilter() {
if (IS_FILTER){
logger.info("token");
}else{
logger.info("token");
}
return IS_FILTER;
}
//
@Value("${root.role}")
private String ROOT_ROLE;
@Override
//TODO
//mq
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String url = request.getRequestURI();
String method = request.getMethod();
//
// 0 1 2
Integer resourceStatus = roleResourceManager.checkStatusForResource(method, url);
if (resourceStatus == null || resourceStatus == -1||resourceStatus == 2){
createResponce("request fail!",ctx);
return null;
} else if (resourceStatus == 0){
//
return null;
}
//jwtBeanjwt
CodecUtils.JwtBean jwtBean = CodecUtils.JwtBean.getJwtBean(request.getHeader(JWT_HEADER_NAME));
//request
addRequestAttribute(jwtBean,request);
if (jwtBean != null){
//jwt
List<String> roleIdList = (List<String>) jwtBean.getPlayload(JwtFieldEnum.ROLES.getField());
//id1
if (roleIdList != null && roleIdList.contains(ROOT_ROLE)){
return null;
}
if (roleIdList != null && roleResourceManager.checkAuthority(method, url, roleIdList)) {
//
return null;
}
}
//
//Resource
createResponce("request fail",ctx);
return null;
}
/**
*
* @param msg
* @param ctx
*/
private void createResponce(String msg,RequestContext ctx){
//
ctx.setSendZuulResponse(false);
//
ctx.setResponseStatusCode(50008);
//
ctx.setResponseBody(msg);
ctx.set("isSuccess", false);
}
/**
* jwtBeanrequest
* @param jwtBean
* @param request
*/
private void addRequestAttribute(CodecUtils.JwtBean jwtBean,HttpServletRequest request) {
if (jwtBean == null) {
return;
}
//roles
Object roles = jwtBean.getPlayload(JwtFieldEnum.ROLES.getField());
Object username = jwtBean.getPlayload(JwtFieldEnum.USERNAME.getField());
Object admin = jwtBean.getPlayload(JwtFieldEnum.ADMIN.getField());
//save info to request
request.setAttribute(RequestAttributeKeyEnum.ACCOUNT_ROLES.getKey(),roles);
request.setAttribute(RequestAttributeKeyEnum.ACCOUNT_USERNAME.getKey(),username);
request.setAttribute(RequestAttributeKeyEnum.ACCOUNT_ADMIN.getKey(),admin);
RequestContext.getCurrentContext().setRequest(request);
}
}
this is my custom pre filter to operate on request, which occurs in the last addRequestAttribute method. How to let the downstream receive the data I added to request in filter?