letsencrypt-helper icon indicating copy to clipboard operation
letsencrypt-helper copied to clipboard

Instructions/code on how to redirect http to https would be helpful

Open metaruslan opened this issue 3 years ago • 2 comments

Hi! This is a useful project, thanks!

Instructions/code on how to redirect http to https would be helpful especially given that this is for small pet projects. I've been able to apply it to my pet project and now I don't have this 301 https redirect. Googling gave me too many ways and it's interesting what would be the best way for spring-boot.

metaruslan avatar Jun 19 '22 20:06 metaruslan

Ok, makes sense, thanks for feedback

valb3r avatar Jun 24 '22 05:06 valb3r

I have made an app which redirects all http to https. You need to tell the WebServer to open the http port and that all traffic to this port should be redirected to another port. This is done by registering a redirecting Connector, eg like this lib does for Tomcat The Spring Security Redirect config looks like this for me:

@Configuration
@EnableWebSecurity
public class SecurityConfig {
  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http) {
    // other config
    if (sslEnabled) {
      http.portMapper(Customizer.withDefaults())
          .requiresChannel(channels -> channels
              .anyRequest().requiresSecure()
          );
    }
    return http.build();
  }
}

If you have non-default ports, so not 8080/8443 or 80/443, you would have to also configure the portMapper, that's why I included it here. It's created by default, so calling .portMapper(Customizer.withDefaults()) is not strictly necessary.

ShadowCreator250 avatar Dec 09 '24 13:12 ShadowCreator250