Http¸¦ Https·Î Àüȯ/ ¸®´ÙÀÌ·ºÆ® ½ÃÅ°±â (Redirecting HTTP to HTTPS in Spring Boot)
Spring Boot¿¡¼ ÀÓº£µðµå ÅèÄÏ ¼¹ö¸¦ ÀÌ¿ëÇÒ¶§ µðÆúÆ® ¼¹ö·Î HTTP ¶Ç´Â HTTPS¸¦ ¼±ÅÃÇØ¾ß ÇÕ´Ï´Ù.
¾Æ·¡´Â HTTP·Î Á¢¼Ó ÇÒ °æ¿ì HTTPS·Î Àüȯ ½ÃÄÑÁÖ´Â ¹æ¹ýÀÔ´Ï´Ù.
1. ¸ÕÀú SSL Áõ¸í¼¸¦ ¹ß±Þ ¹Þ¾Æ¾ß ÇÕ´Ï´Ù.
HTTPS¿¬µ¿À» À§Çؼ ¿ì¼± keystore¸¦ ¸¸µé¾î¾ß ÇÕ´Ï´Ù.
keytool -genkey -alias tomcat
-storetype PKCS12 -keyalg RSA -keysize 2048
-keystore keystore.p12 -validity 3650
Enter keystore password:
Re-enter new password:
What is your first and last name?
[Unknown]:
What is the name of your organizational unit?
[Unknown]:
What is the name of your organization?
[Unknown]:
What is the name of your City or Locality?
[Unknown]:
What is the name of your State or Province?
[Unknown]:
What is the two-letter country code for this unit?
[Unknown]:
Is CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct?
[no]: yes
2. ½ºÇÁ¸µ ºÎÆ® ³»¿¡¼ HTTPS°¡ °¡´ÉÇϵµ·Ï ¼³Á¤ÇÕ´Ï´Ù.
½ºÇÁ¸µ ºÎÆ®´Â src/main/resources Æú´õ ³»¿¡ À§Ä¡ÇÑ application.properties ÆÄÀÏ¿¡¼ ÇÊ¿äÇÑ ¼³Á¤µéÀ» ÇÒ ¼ö ÀÖ½À´Ï´Ù. ¾Æ·¡¿Í °°ÀÌ ¼³Á¤ ÇÒ °æ¿ì, https://localhost:8443À¸·Î Á¢¼ÓÇÒ ¼ö ÀÖ½À´Ï´Ù.
#ssl
server.ssl.key-store=classpath:pathÀ§Ä¡
server.ssl.key-store-password=password
server.ssl.key-password=password
server.port=8443
server.port.http=8080
3. HTTP·Î Á¢¼ÓÇÒ °æ¿ì, HTTPS·Î ¸®´ÙÀÌ·ºÆ®(Àüȯ) ½ÃÄÑÁÝ´Ï´Ù. (@ConfigurationŬ·¡½º³»¿¡)
@Configuration
@EnableConfigurationProperties
public class SampleConfig {
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat =
new TomcatEmbeddedServletContainerFactory() {
@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(createHttpConnector());
return tomcat;
}
@Value("${server.port.http}")
private int serverPortHttp;
@Value("${server.port}")
private int serverPortHttps;
private Connector createHttpConnector() {
Connector connector =
new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setSecure(false);
connector.setPort(serverPortHttp);
connector.setRedirectPort(serverPortHttps);
return connector;
}
}
Àüü ¼Ò½º ÄÚµå´Â https://github.com/annajinee/Spring-Boot-redirect-HTTP-to-HTTPS ¿¡¼ È®ÀÎ °¡´ÉÇÕ´Ï´Ù.
Âü°í »çÀÌÆ® : https://drissamri.be/blog/java/enable-https-in-spring-boot/
http://williewheeler.com/2015/03/25/redirecting-http-to-https-in-spring-boot/
Ãâó: http://annajinee.tistory.com/25?category=762989 [Hello, world!]