目前,我有AuthFilter,在这里我收到了一个UserState。我需要把它传递给下一个过滤器。但是怎么做才对呢?或者存在其他实践来解决它?
public class AuthFilter extends ZuulFilter {
@Autowired
private AuthService authService;
@Autowired
private ApplicationContext appContext;
@Override
public String filterType() {
return PRE_TYPE;
}
@Override
public int filterOrder() {
return PRE_DECORATION_FILTER_ORDER - 2;
}
@Override
public boolean shouldFilter() {
RequestContext context = RequestContext.getCurrentContext();
String requestURI = context.getRequest().getRequestURI();
for (String authPath : authPaths) {
if (requestURI.contains(authPath)) return true;
}
return false;
}
@Override
public Object run() throws ZuulException {
try {
UserState userState = authService.getUserData();
DefaultListableBeanFactory context = new DefaultListableBeanFactory();
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(UserState.class);
beanDefinition.setPropertyValues(new MutablePropertyValues() {
{
add("user", userState);
}
});
context.registerBeanDefinition("userState", beanDefinition);
} catch (UndeclaredThrowableException e) {
if (e.getUndeclaredThrowable().getClass() == UnauthorizedException.class) {
throw new UnauthorizedException(e.getMessage());
}
if (e.getUndeclaredThrowable().getClass() == ForbiddenException.class) {
throw new ForbiddenException(e.getMessage(), "The user is not allowed to make this request");
}
}
return null;
}
}发布于 2018-08-07 16:08:41
我非常确定过滤器被链接在一起,请求/响应通过它们传递。您可以将数据添加到请求中,并让下一个筛选器查找它。
https://stackoverflow.com/questions/51720147
复制相似问题