Java Spring - 在运行时添加和删除过滤器
Java Spring - Add and remove Filter at runtime
我有一个请求转储过滤器,我想在运行时 activate/deactivate(使用自定义执行器端点)。但是,如果不重新启动应用程序,我找不到这样做的方法。我目前的解决方案就是这样做的:
@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 方法。
有什么办法可以得到我需要的东西吗?
首先使用 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);
}
}
}
我有一个请求转储过滤器,我想在运行时 activate/deactivate(使用自定义执行器端点)。但是,如果不重新启动应用程序,我找不到这样做的方法。我目前的解决方案就是这样做的:
@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 方法。
有什么办法可以得到我需要的东西吗?
首先使用 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);
}
}
}