【发布时间】:2018-01-12 12:35:07
【问题描述】:
我正在使用 Spring 4 和 Tomcat 7 实现一个 Web 应用程序。
我使用 SSL 证书在 Tomcat 上运行应用程序,它工作正常,但我想强制任何 HTTP 请求重定向到其 HTTPS 请求。
我已经搜索了 Spring Security 并添加了一些配置如下:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class CoreSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**")
.requiresChannel()
.anyRequest().requiresSecure();
http
.antMatcher("/**")
.portMapper()
.http(80).mapsTo(443);
}
}
但它似乎无法正常工作,并且 Spring Security 没有进行重定向。
这个配置有问题吗?
【问题讨论】:
-
您可以尝试添加
http.headers().httpStrictTransportSecurity()告诉客户端他们需要使用HTTPS。 -
@ngreen 我将重定向配置添加到 tomcat web.xml 并且它工作正常。无论如何,感谢您的帮助。
标签: java spring tomcat ssl spring-security