2

I have read those posts on StackOverflow:

Redirect http to https on spring boot embedded undertow

Spring RestTemplate redirect 302

Redirect HTTP to HTTPS in Undertow

I would like to configure Undertow to redirect from HTTP to HTTPS with GET and POST. GET works, but when POSTing it looks for GET method with the same URL (and in my case it finds).

My server config:

@Configuration
public class UndertowConfig {

    @Bean
    public EmbeddedServletContainerFactory undertow() {
        UndertowEmbeddedServletContainerFactory undertow = new   UndertowEmbeddedServletContainerFactory();
        undertow.addBuilderCustomizers(builder -> builder.addHttpListener(8080, "0.0.0.0"));
        undertow.addDeploymentInfoCustomizers(deploymentInfo -> {
            deploymentInfo.addSecurityConstraint(new SecurityConstraint()
            .addWebResourceCollection(new WebResourceCollection()
            .addUrlPattern("/*"))
            .setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL)
            .setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT))
                .setConfidentialPortManager(exchange -> 8443);
        });
        return undertow;
    }
}

Enforced HTTPS:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requiresChannel().anyRequest().requiresSecure();
    }
}

Controller:

@RestController
public class HelloController {

    @RequestMapping("/")
    public String get() {
        return "GET";
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String post() {
        return "POST";
    }

}

Client:

public class Client {

    public static void main(String[] args) {

        HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);

        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        HttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).setSSLHostnameVerifier((hostname, session) -> true).build();
        factory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(factory);
        String get = restTemplate.getForObject("http://localhost:8080/", String.class);
        ResponseEntity<String> post = restTemplate.postForEntity("http://localhost:8080/", null, String.class);
}

}

How to make that POST request goes into POST?

marciano
  • 71
  • 5

0 Answers0