在Spring安全登录成功期间强制Https连接

时间:2016-12-12 23:25:55

标签: java spring cloudfoundry predix

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

我在春天的配置是这样的:

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();

我也曾尝试将绝对网址与<​​strong> https 一起使用,但效果不佳。

1 个答案:

答案 0 :(得分:4)

您是否尝试过requiresChannel()requiresSecure()?要通过https访问特定网址,您可以尝试

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

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

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

您可以使用如下所示的端口映射。

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

请参阅thisthis了解详情。