Spring Boot

Spring boot SSL 사용

상국이 2022. 2. 1. 01:01
728x90

SSL 인증서

 : 서버와 클라이언트 간의 통신을 제 3자가 보증해주는 인증서

 

HTTPS 사용

1. keystore 생성

 keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 4000

2. application.properties 설정

server.ssl.key-store: keystore.p12
server.ssl.key-store-password: 123456
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: tomcat

* 기존처럼 http://localhost:8080/ 로 접속하는 경우 

라고 뜸

* https:localhost:8080/ 으로 접속해도 다음과 같이 뜨는 이유

        > 해당 브라우저에서 우리가 local에서 만든 인증서의 pub키를 모르기 때문

        > https를 적용하고나면 http connector가 하나이므로 http로의 접근이 불가(현재는)

        > curl -I -k --http2 https://localhost:8080/ 로 접근할 수 있음

 

* 해결방법

     

@SpringBootApplication
@RestController
public class SpringbootsslstudyApplication {

    @GetMapping("/hello")
    public String hello(){
        return "hello SangGuk";
    }

    @Bean
    public ServletWebServerFactory servletFactory(){
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createStandardConnector());

        return tomcat;
    }

    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(8081);
        return connector;
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringbootsslstudyApplication.class, args);
    }
}

 

HTTP/2 추가

       > application.properties 설정

server.http2.enabled=true

        > undertow사용

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
728x90