Spring Security requireSecure重定向循环

时间:2019-10-10 23:46:13

标签: spring-boot spring-security

具有以下配置的默认注销方法将重定向到HTTP url。当我添加.requiresChannel().anyRequest().requiresSecure()时,它将进入重定向循环。据我了解,这是因为spring发送HTTPS请求,tomcat使其成为http请求,然后spring再次尝试执行https请求,这就是循环吗?不确定。此循环发生在根URL。

我尝试添加

server.tomcat.remote-ip-header = x-forwarded-for
server.tomcat.protocol-header = x-forwarded-proto
server.use-forward-headers: true

到属性文件,但无效。

httpSecurity
    .csrf().disable()
    .anonymous()
        .and()
    .exceptionHandling()
        .authenticationEntryPoint(new OowAccessDeniedEntryPoint())
        .and()
    .authorizeRequests()
        .antMatchers(ignoreStaticResourceMatchers()).permitAll()
        .antMatchers(ignoreEndpointMatchers()).permitAll()
        .anyRequest().authenticated()
        .and()
    .oauth2Login()
        .and()
    .rememberMe()
        .key(key)
        .rememberMeServices(new OowTokenBasedRememberMeServices(key, cookies, encrypt, gson))
        .and()
    .logout()

目前正计划解决此问题,但令我有些惊讶的是,我在刮擦网络6个小时后仍未找到解决此问题的方法。

1 个答案:

答案 0 :(得分:0)

解决方法为

static class OowLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.debug("Logout success"); 
    }
}

但是我觉得下面的代码解决了这个问题。

(1)application.yml

server:
  use-forward-headers: true

(2)在服务器/etc/apache2/sites-enabled/oow.com-le-ssl.conf

RequestHeader set X-Forwarded-Proto https
RequestHeader set X-Forwarded-Port 443

(2.1)并启用apache模块

sudo a2enmod headers

借助thisthis