스프링부트 https 적용, http to https 리디렉션 적용
2021. 2. 26. 03:59ㆍSpring Boot
반응형
1. 자바를 이용한 인증서 만들기
Intellij 터미널에서 다음과 같이 keystore.p12라는 키를 만든다. 입력해야할 이름, 도시 등 정보 입력한다.(맘대로 해도됨)
keytool -genkey -alias spring -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 4000
2. application.yml 채우기
server:
port: 9000 #포트 정의하는 부분
ssl:
enabled: true
key-store: keystore.p12
key-store-password: 12341234
key-store-type: PKCS12
key-alias: spring
3. 다중커넥터 설정
스프링 톰캣 내장 서버는 기본적으로 커넥터가 한개로 설정되어있다. 따라서 위의 https요청과 http요청 모두 받기위해서는 커넥터를 하나 더 만들어야한다.
TemplateApplication.java
package com.softsquared.template;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class TemplateApplication {
public static void main(String[] args) {
SpringApplication.run(TemplateApplication.class, args);
}
@Bean
public ServletWebServerFactory serverFactory() {
TomcatServletWebServerFactory tocat = new TomcatServletWebServerFactory();
tocat.addAdditionalTomcatConnectors(createStandardConnector());
return tocat;
}
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(8080);
return connector;
}
}
4. 리디렉션 http to https 적용
package com.softsquared.template;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class TemplateApplication {
public static void main(String[] args) {
SpringApplication.run(TemplateApplication.class, args);
}
@Bean
public ServletWebServerFactory serveltContainer(){
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(createStandardConnector());
return tomcat;
}
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setSecure(false);
connector.setPort(9001);
connector.setRedirectPort(9000);
return connector;
}
}
<참고자료>
다중커넥터
http to https적용
반응형
'Spring Boot' 카테고리의 다른 글
스프링부트 어노테이션(Annotation)이란 (0) | 2021.03.17 |
---|---|
스프링부트 페이징 , 정렬 (0) | 2021.03.10 |
@RequestMapping 과 @GetMapping 차이 (0) | 2021.02.11 |
스프링 빈 순환 참조 - The dependencies of some of the beans in the application context form a cycle (0) | 2021.02.11 |
스프링부트 서브쿼리 (0) | 2021.02.04 |