금융프로젝트

[spring][https][security][jks] 스프링 프로젝트 https로 만들기

h-yujin 2022. 10. 19. 15:56

https

ssl프로토콜 위에서 돌아가는 프로토콜. http에 보안이 향상된 버전이다.

  • spring프로젝트에 https 적용하기
  • jks키파일 받기

cmd명령창에서 jre또는 jdk가 설치된 폴더의 bin 폴더로 이동한다.

cd 경로

keytool을 실행한다.

keytool -genkey -alias [keystore 별칭] -keyalg RSA -keystore [keyfile이름]

생성한 키를 바탕으로 인증서 파일을 생성한다

keytool -certreq -alias [keystore 별칭] -keyalg rsa file [파일 이름] -keystore [keyfile 이름]
  • server.xml수정

server.xml에 다음 내용을 추가한다.

<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
           maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
           defaultSSLHostConfigName="localhost">
    <SSLHostConfig hostName="localhost" protocols="TLSv1.2,+TLSv1.1,+TLSv1">
        <Certificate certificateKeystoreFile="키파일 경로"
                     type="RSA" certificateKeystorePassword="키파일 비번"/>
    </SSLHostConfig>
</Connector>

여기까지 하면 https://localhost:443이 접속된다.

  • pom.xml: spring security 설치
<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-core</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-web</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-config</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
  • security-context.xml 파일 만들기

WEB-INF/spring밑에 새 파일을 추가한다.

파일 유형은 spring bean configuration file로 한다.

설정들을 추가한 후 마친다.

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.1.xsd">

	<http auto-config='true'>
        <intercept-url pattern="/**" access="ROLE_ADMIN" />                
    </http>
    <authentication-manager>
        <authentication-provider>
            <user-service>
               <user name="admin" password="1234" authorities="ROLE_USER, ROLE_ADMIN" />
              
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

내용을 이렇게 채운다.

  • web.xml
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
 <!-- //스프링 시큐리티 -->
 <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
				/WEB-INF/spring/root-context.xml
				/WEB-INF/spring/security-context.xml
		</param-value>
	</context-param>

<context-param>의 <param-value>에 security-context를 추가한다.

filter와 filter-mapping을 추가한다.