我有一个请求转储过滤器,我想在运行时激活/停用(使用自定义执行器端点)。但是,如果不重新启动应用程序,我无法找到方法。我目前的解决方案就是:
@Configuration
public class AppConfigs {
@Value("${request.dumper.active:false}")
private Boolean dumpActive;
@Bean
public FilterRegistrationBean requestDumperFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
Filter requestDumperFilter = new RequestDumperFilter();
registration.setFilter(requestDumperFilter);
registration.addUrlPatterns("/*");
registration.setEnabled(dumpActive);
return registration;
}
现在我想使用类似的东西:
@Component
public class RequestdumperEndpoint implements Endpoint<Boolean > {
@Autowired
private RequestDumperFilter requestDumperFilter;
public String getId() {
return "requestdumper";
}
public Boolean invoke() {
return requestDumperFilter.isEnabled();
}
public Boolean setEnabled(Boolean enabled) {
return requestDumperFilter.setEnabled(enabled);
}
但很明显,Filter上没有isEnabled和setEnabled方法。
我有什么想法可以得到我需要的东西吗?
答案 0 :(得分:1)
首先使用bean作为bean添加过滤器,以便挂钩:
public class MyWebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Value("${request.dumper.active:false}")
private Boolean dumpActive;
@Bean
private MyRequestDumperFilter myRequestDumperFilter() {
MyRequestDumperFilter filter = new MyRequestDumperFilter();
filter.setEnabled(dumpActive);
return filter;
}
@Override
protected String[] getServletMappings() {
//..
return new String[0];
}
@Override
protected Filter[] getServletFilters() {
return new Filter[]{myRequestDumperFilter()};
}
@Override
protected Class<?>[] getRootConfigClasses() {
//..
return new Class<?>[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
//..
return new Class<?>[0];
}
}
您过滤后会看起来像:
public class MyRequestDumperFilter extends RequestDumperFilter{
private Boolean enabled;
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public Boolean getEnabled() {
return enabled;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (enabled) {
super.doFilter(request, response, chain);
}
else {
chain.doFilter(request,response);
}
}
}