Amazon web services AWS ELB HTTPS到HTTP使用Spring Boot MVC登录不工作

Amazon web services AWS ELB HTTPS到HTTP使用Spring Boot MVC登录不工作,amazon-web-services,spring-mvc,ssl-certificate,Amazon Web Services,Spring Mvc,Ssl Certificate,我使用的是运行在AWS实例中的SpringBoot,它带有一个弹性负载平衡器。我使用的是SpringMVC,其登录安全性和服务器端口设置为8080。我已在ELB中安装了一个AWS生成的证书,该证书具有正确的web名称test.mydomain.com。ELB侦听器是为端口80HTTP>>8080HTTP和443HTTPS>>8080HTTP设置的。这是我的WebSecurity配置类 public class WebSecurityConfig extends WebSecurityConfig

我使用的是运行在AWS实例中的SpringBoot,它带有一个弹性负载平衡器。我使用的是SpringMVC,其登录安全性和服务器端口设置为8080。我已在ELB中安装了一个AWS生成的证书,该证书具有正确的web名称test.mydomain.com。ELB侦听器是为端口80HTTP>>8080HTTP和443HTTPS>>8080HTTP设置的。这是我的WebSecurity配置类

public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    UserDetailsService userDS;

    @Override
    protected void configure(HttpSecurity http) throws Exception{

        // Allows access for .css, .js, and .images files
        http.authorizeRequests()
            .antMatchers("/resources/**")
            .permitAll()
            .anyRequest()
            .permitAll();

        // Access management for all other requests
        http.authorizeRequests()
            .antMatchers("/", "/home").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .logoutUrl("/logout")
            .logoutSuccessUrl("/home")
            .invalidateHttpSession(true)
            .permitAll();

    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

        auth.userDetailsService(userDS).passwordEncoder(passwordEncoder());

    }

    @Override
    protected UserDetailsService userDetailsService() {
        return userDS;

    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder;
    }


}
当我访问主页时,一切正常,页面安全。我确信证书有效。我尝试使用curl-vkhttps:test.mydomain.com/home并验证证书是否有效。当我单击login时,页面将恢复为HTTP不安全页面,并且所有其他页面都是HTTP


我用ELB做了下面的实验。我删除了端口80侦听器,因此在443-443HTTPS>>HTTP8080上只有一个侦听器。我再次登录到,页面是安全的。当我单击“登录”时,浏览器找不到该页面。我注意到浏览器地址栏上有。由于端口80没有侦听器,请求失败。为什么我的登录页面试图访问端口80?在我的application.properties页面中,我有server.port=8080。登录页面恢复到端口80对我来说毫无意义。有人有什么想法吗?我已经看到很多关于登录ELB重定向的帖子导致了很多Spring MVC ELB问题。这可能是我的问题吗?

由于您的ELB将充当代理,您必须通过在application.properties中将server.use-forward-headers设置为true来配置Spring和正在运行的服务器以使用转发头。有关更多信息,请参阅


编辑:从Spring Boot 2.2开始,server.use-forward-headers属性已被弃用,取而代之的是server.forward-headers-strategy。请参见

由于ELB将充当代理,您必须通过在application.properties中将server.use-forward-headers设置为true来配置Spring和正在运行的服务器以使用转发头。有关更多信息,请参阅


编辑:从Spring Boot 2.2开始,server.use-forward-headers属性已被弃用,取而代之的是server.forward-headers-strategy。请参阅

你会考虑使用ALB吗?这个问题解决起来要容易得多。你会考虑使用ALB吗?这样解决这个问题就容易多了。Awesum-Sauce-我添加了server.use-forward-headers=true to application.properties,它工作得非常好!你的url指针很好地描述了正在发生的事情。我再次陷入这个问题的第二部分。我正在尝试将所有端口80请求重定向到443。重定向工作正常,但我总是转到我的登录页,而不是我的/主页。下面是stackoverflow问题的答案。看起来我又遇到重定向问题了。你能看一下吗?我添加了server.use-forward-headers=true到application.properties,它工作得非常好!你的url指针很好地描述了正在发生的事情。我再次陷入这个问题的第二部分。我正在尝试将所有端口80请求重定向到443。重定向工作正常,但我总是转到我的登录页,而不是我的/主页。下面是stackoverflow问题的答案。看起来我又遇到重定向问题了。你能看一下吗?