Issue with enabling https using Java Spring

Eamon Mac :

I am following this short tutorial on enabling https security for a project. https://www.thomasvitale.com/https-spring-boot-ssl-certificate/

However when i run the project i get the following error:

The Tomcat connector configured to listen on port 8443 failed to start. The port may already be in use or the connector may be misconfigured.

I have generated the keystore as mentioned in the tutorial. I have seen this question raised before but with no clear answer. The port 8443 is not configured right or something as i cant see it when i type netstate -ao in the command line.

Any help at all is appreciated.

============

My files

1) application.properties

spring.data.rest.base-path=/api
server.port = 8443
server.ssl.key-store-type=PKCS12
server.ssl.key-store=src/main/resources:keystore.p12
server.ssl.key-store-password=Welcome1
server.ssl.key-alias=tomcat
server.servlet.contextPath=/

2) SecurityConfig

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.requestMatchers()
        .antMatchers("/login", "/oauth/authorize")
        .and()
        .authorizeRequests()
        .anyRequest()
        //.permitAll()
        .authenticated()
        .and()
        .formLogin()
        .permitAll();
        //.httpBasic();

    http.csrf().disable();
}

/*@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}*/

/*
// Create an encoder with strength 16
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(16);
    String result = encoder.encode("myPassword");
    assertTrue(encoder.matches("myPassword", result));
        */

@Bean
public TomcatServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(getHttpConnector());
    return tomcat;
}

private Connector getHttpConnector() {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setScheme("http");
    connector.setPort(8080);
    connector.setSecure(false);
    connector.setRedirectPort(8443);
    return connector;
}

}

3) HelloResource

@RestController
@RequestMapping("/rest/hello")
public class HelloResource {

  @GetMapping
  public String hello(){
    return "Hello World";
  }
}
Awad :

Change the port to 443 in the config.

server.port = 443

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=106060&siteId=1