I have configured additional port to work on https for an REST endpoint. However, while sending request to this endpoint, application does not negotiate over https instead it uses only http. Framework is on Spring Boot 2.7.5.
@Configuration
public class PublicPortConfig {
private static final Logger logger = LogManager.getLogger(PublicPortConfig.class);
@Value("${server.port.public:19280}")
private int port;
@Bean
@ConditionalOnProperty(name = "server.port.public")
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> publicServletContainerCustomizer (
ServerProperties serverProperties) {
logger.info("Configuring public port: {}", port);
return tomcat -> {
Connector connector = new Connector();
connector.setPort(port);
connector.setScheme("https");
SSLHostConfig sslHostConfig = new SSLHostConfig();
sslHostConfig.setCertificateKeystoreFile(serverProperties.getSsl().getKeyStore());
sslHostConfig.setCertificateKeyPassword(serverProperties.getSsl().getKeyStorePassword());
sslHostConfig.setCertificateKeystoreType(serverProperties.getSsl().getKeyStoreType());
connector.addSslHostConfig(sslHostConfig);
tomcat.addAdditionalTomcatConnectors(connector);
};
}
}
@RestController
public class ExternalApiController {
@GetMapping("/api/public")
public ResponseEntity<String> hello() {
return ResponseEntity.ok("Hello Customer (public)");
}
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
logger.info("Configuring HttpSecurity via SecurityFilterChain...");
// @formatter:off
http.authorizeRequests()
.antMatchers(AUTH_PATH_WHITELIST).permitAll()
.antMatchers("/api/public/**").permitAll()
.antMatchers("/api/admin/**").hasAnyRole("ADMIN")
.antMatchers("/actuator/**").hasAnyRole("ADMIN", "ACTUATOR")
.and()
.authorizeRequests().anyRequest().fullyAuthenticated();
http.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.maximumSessions(MAX_SESSION_ACTIVE);
http.httpBasic()
.realmName("API Auth via DB or LDAP")
.and()
.authenticationProvider(customAuthenticationProvider);
http.csrf().csrfTokenRepository(cookieCsrfTokenRepository());
/**
* Force Disable for API
*/
http.csrf().disable();
// http.cors().disable();
// @formatter:on
return http.build();
}
ExternalApiController sends response when request is sent on http and not on https. All fully authenticated endpoints works perfectly well on https.
cURL gives following results
$ curl --insecure -H "Accept: application/json" -H "Content-Type: application/json" -X GET "https://localhost:19280/app/api/public"
Response: curl: (35) LibreSSL/3.3.6: error:1404B42E:SSL routines:ST_CONNECT:tlsv1 alert protocol version Java Backend Error: java.lang.IllegalArgumentException: Invalid character found in method name [0x160x030x010x01(0x010x000x01$0x030x03090xe9}0xef0x980x970x1b+0x820x0f0xc10xbc0xde0x8370xfceI0xdey0x100x1f0x99vJ0xa50x7f_0xd9Y ]. HTTP method names must be tokens
Where as http request using cURL
$ curl --insecure -H "Accept: application/json" -H "Content-Type: application/json" -X GET "http://localhost:19280/app/api/public"
Response: Hello Customer (public)
Application is started on 2 ports as per below log INFO 2023-06-06/17:08:18/GMT+05:30 [app-rest-api] (TomcatWebServer.java:220) (start) Tomcat started on port(s): 9280 (https) 19280 (https) with context path /app
Port 9280 is ok. However, anything I access on 19280 throws error
Appreciate any help. Thanks in advance