在 spring 安全登录成功期间强制使用 Https 连接

Forcing Https connection during the spring security login success

我有一个 spring 启动应用程序,其中包含 spring 安全性,其中添加了 formLogin 和自定义 loginPage 。每当我通过身份验证时,它都会将我发送到 /app/dashboard 的 defaultSuccessUrl 并使用模式 http 发送我一整天都在尝试将 successUrl 模式设为 https只是调整 application.properties 上的一些更改,有时使用 Bean,但我仍然无法实现它。我的应用程序在 cloudfoundry 中,我没有 80 端口,只有 443(https)。

我在spring中的配置是这样的:

http
    .authorizeRequests()
    .antMatchers("/", "/forbidden", "/index.html", "/webjars/*", "/app.js", "/access/*", "/signup", "/l10n/*.js", "/", "/tpl/**/*.html", "/fonts/**/*.woff").permitAll()
    .anyRequest().authenticated()

    .and().addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class).
    csrf().csrfTokenRepository(repo)

    .and() .httpBasic().disable()
    .formLogin()
    .loginPage("/access/signin").permitAll()
    .failureUrl("/error")
    .defaultSuccessUrl("/app/dashboard")
    .and().logout()
    .logoutRequestMatcher(new AntPathRequestMatcher("access/logout"))
    .logoutSuccessUrl("/access/signin").permitAll();

我也曾尝试将绝对 url 与 https 一起使用,但效果不佳。

你试过requiresChannel()requiresSecure()了吗?要通过 https 访问特定的 url,您可以尝试

.defaultSuccessUrl("/app/dashboard").and().requiresChannel().antMatchers("/app/dashboard").requiresSecure() 

对于所有通过https的请求,你可以使用like

.and().requiresChannel().anyRequest().requiresSecure()

您可以像下面这样使用端口映射。

 http
     .portMapper()              
        .http(8080).mapsTo(443);

详情请参考this and this