SpringBoot2.0中禁用内置Tomcat的不安全请求方法
禁用内置Tomcat的不安全请求方法SpringBoot2.0之前版本在SpringBoot2.0之前的版本中可以向容器注册[EmbeddedServletContainerFactory ]类实现对内置Servlet容器的配置@Beanpublic EmbeddedServletContainerFactory servletContainer() {T...
·
禁用内置Tomcat的不安全请求方法
SpringBoot2.0之前版本
在SpringBoot2.0之前的版本中可以向容器注册[EmbeddedServletContainerFactory ]类实现对内置Servlet容器的配置
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {// 1
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
collection.addMethod("HEAD");
collection.addMethod("PUT");
collection.addMethod("DELETE");
collection.addMethod("OPTIONS");
collection.addMethod("TRACE");
collection.addMethod("COPY");
collection.addMethod("SEARCH");
collection.addMethod("PROPFIND");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
SpringBoot2.0
在SpringBoot2.0中我没找到[EmbeddedServletContainerFactory]类,可以使用[ConfigurableServletWebServerFactory]替代
@Bean
public ConfigurableServletWebServerFactory configurableServletWebServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addContextCustomizers(context -> {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
collection.addMethod("HEAD");
collection.addMethod("PUT");
collection.addMethod("DELETE");
collection.addMethod("OPTIONS");
collection.addMethod("TRACE");
collection.addMethod("COPY");
collection.addMethod("SEARCH");
collection.addMethod("PROPFIND");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
});
return factory;
}
更多推荐
已为社区贡献1条内容
所有评论(0)