【问题标题】:What additional configuration needs to be done in order to enforce https using spring-security?为了使用 spring-security 强制执行 https,需要做哪些额外的配置?
【发布时间】:2016-03-13 22:07:21
【问题描述】:

我正在尝试保护我的 web 应用程序,以便所有请求都需要使用 https 进行。我正在使用基于 Java 的配置,特别是我有一个扩展 WebSecurityConfigurerAdapter 的类。我尝试使用@Override protected void configure(HttpSecurity http) {} 方法配置我的安全性,详细说明herehere

我已经尝试了这两种方法以及配置HttpSecurity 对象的许多变体,以及几种身份验证变体。在几乎所有情况下,我都会提出以下问题:

[nio-8080-exec-2] o.a.coyote.http11.Http11NioProcessor : Error parsing HTTP request header

我尝试对此进行一些阅读,但很多搜索结果对我来说都是死胡同。我的假设是,在我引用的两篇文章中暗示的解决方案会给我大致正确的答案,但是在我可以让 https 工作之前我需要做任何额外的配置吗?如果是,那是什么,如果不是,我在这里错过了什么?

下面是我目前的配置,复制了上面描述的错误(基本上是默认的一加https通道):

@Configuration
@EnableWebSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    // Specify the authentication mechanisms that will allow user access to the site.
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder builder) throws Exception {
        builder.inMemoryAuthentication()
            .withUser("user").password("password").roles("ROLES_USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests().anyRequest().authenticated()
            .and()
            .formLogin()
            .and()
            .httpBasic()
            .and()
            .requiresChannel().anyRequest().requiresSecure();
    }
}

在前端我收到了SSL Connection Error : Unable to make a secure connection to the server. This may be a problem with the server, or it may be requiring a client authentication certificate that you don't have.

【问题讨论】:

  • 你能带我们看全班吗?
  • 好的,编辑了原始帖子以包含整个配置
  • 尝试将super.configure(http); 添加为configure 的第一行。
  • 您的安全配置看起来不错,我们可以查看您的控制器类吗?你确定里面一切正常吗?
  • 您的服务器上是否配置了 https?如果您只指示 Spring Security 始终使用 Https,但没有为 https 设置您的服务器,那么它将无法正常工作。当您使用 Spring Boot(为您添加标签)时,这已记录在 here

标签: java spring-security spring-boot


【解决方案1】:

正如@M.Deinum 在 cmets 中提到的那样,困难在于我没有在我的application.properties 中配置 SSL。创建一个类似于以下的:

server.port=9090
server.ssl.key-store=classpath:keystore.jks
server.ssl.key-store-password={password}
security.require-ssl=true

解决了问题。

【讨论】:

    猜你喜欢
    • 2018-07-08
    • 2011-07-16
    • 2012-05-13
    • 2016-05-17
    • 2015-10-08
    • 2012-07-17
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多