spring - 我如何使用弹簧上下文在 Zuul 过滤器之间传递新对象?
问题描述
目前,我已经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;
}
}
解决方案
我很确定过滤器被链接在一起,请求/响应通过它们传递。您可以将数据添加到请求中,并让下一个过滤器查找它。
推荐阅读
- libgit2 - 如何测试凭证是否有效?
- java - 共享图像无法通过意图加载
- error-handling - 由于“文件正在使用”,在 lua 中处理覆盖错误很热
- angular - RouterService 中的 this.route 没有正确的 url
- python - 如果拆分后存在,则提取(获取)第二个字符串,否则首先
- python - python中的图像处理——色相、饱和度、对比度和亮度
- python - Reg Ex 用于否定环视或否定断言需要下划线
- php - 如何在postgis中从php和insrt poligon转换geojson中的几何图形
- docker - Logrotation 未通过 cron 作业在 docker 容器中运行
- c# - 在更新现有记录时更新数据而不修改 CreatedBy 和 CreatedDate