简体   繁体   English

如何使用Spring Security Java配置将HTTP请求重定向到HTTPS?

[英]How to redirect HTTP requests to HTTPS using Spring Security Java configuration?

I have a Spring Security version 3.2.3 application that listens to both HTTP and HTTPS. 我有一个Spring Security版本3.2.3应用程序,它可以监听HTTP和HTTPS。 I want any request to the HTTP port to be redirected to HTTPS. 我希望将对HTTP端口的任何请求重定向到HTTPS。 How do I configure that using Java only? 如何仅使用Java配置?

Spring Security javadoc for HttpSecurity proposes the following solution (trimmed to the essential): 针对HttpSecurity Spring Security javadoc提出了以下解决方案(修剪为必需的):

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    protected void configure(HttpSecurity http) {
        http.channelSecurity().anyRequest().requiresSecure();
    }
}

However that doesn't work because HttpSecurity doesn't have method channelSecurity() . 但是这不起作用,因为HttpSecurity没有方法channelSecurity()

Replacing channelSecurity() with requiresChannel() in the code in the question appears to give the desired behaviour. 在问题代码中使用requiresChannel()替换channelSecurity()似乎可以提供所需的行为。 The working code then looks as following: 然后,工作代码如下所示:

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    protected void configure(HttpSecurity http) {
        http.requiresChannel().anyRequest().requiresSecure();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM