2 분 소요

tomcat 에 server.xml을

이런 식으로 적용했다 치면

이제 저 qwe123 패스워드를 암호화 해보자

먼저 암호화는 dbproperties 암호화 한 방식으로 jasypt 라이브러리 이용하여 암호화함. 위 글 참조

그럼 이제 암호화 된 값을 password 에 넣어야 하고 이를 복호화 후에 작동 되게 만들어야 함

<톰캣 SSL keystore Encryption>

다음 글에서 참고 하였음.

핵심은 server.xml Connector 태그에서 protocol을 커스텀 클래스로 바꿔주어야 한다는 것.

이를 위해 자바 프로젝트를 하나 만들자.

다음과 같이 만들었음

패키지 경로는 반드시 com.apache 로 설정 !!!

먼저 파일을 설명하자면

  1. CustomHttp11Nio2Protocol.java < 이게 실제 복호화 해줄 커스텀 클래스 (프로토콜)
  2. DecryptPassword.java < 복호화 로직
  3. Encryption.java < 암호화 + main 함수 (없어도 됨.)

1. CustomHttp11Nio2Protocol.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.apache;  
  
public class CustomHttp11Nio2Protocol extends org.apache.coyote.http11.Http11Nio2Protocol {  
  
    @Override  
    public void setKeystorePass(String s) {  
          try {  
             super.setKeystorePass(DecryptPassword.decrypt(s));  
          } catch (final Exception e){  
            super.setKeystorePass("");  
          }  
    }  
    }

다음에서 가장 중요한 건 extends org.apache.coyote.http11.Http11Nio2Protocol 다음 프로토콜을 상속받는 클래스라는 것. 그리고는 사실 super.setKeystorePass(DecryptPassword.decrypt(s)); 이거밖에 없음.

2. DecryptPassword.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.apache;  
  
import org.jasypt.util.text.BasicTextEncryptor;  
  
public class DecryptPassword {  
  
    private static final String salt = "tigensoft";  
  
    public static String decrypt(String s) {  
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();  
        textEncryptor.setPassword(salt);  
        return textEncryptor.decrypt(s);  
    }  
    }

여기도 별거 없다. key 값 (salt) 만 tigensoft 로 해줬다.

암호화는 저런 식으로 하면 됨

3. Encryption.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.apache;  
  
import java.util.Scanner;  
  
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;  
  
public class Encryption {  
  
    public static void main(String[] args) {  
       Scanner scanner = new Scanner(System.in);  
       System.out.print("Enter a value to encrypt: ");  
        String userInput = scanner.nextLine();  
                  
// Set the salt value  
        String salt = "tigensoft";  
  
        // Encrypt the user input  
        String encryptedValue = encryptValue(userInput, salt);  
        // Print the encrypted value  
        System.out.println("Encrypted Value: " + encryptedValue);  
  
  
    }  
          
public static String encryptValue(String value, String salt) {  
        StandardPBEStringEncryptor enc = new StandardPBEStringEncryptor();  
        enc.setAlgorithm("PBEWithMD5AndDES");  
        enc.setPassword(salt);  
        return enc.encrypt(value);  
    }  
          
}

여기선 메인함수를 이용해서 cmd 창에서 사용자에게 값을 받으면 암호화 된 값을 출력 시켜주기 위해 메인함수 작성하였음.

그리고 밑에 encryptValue 는 암호화 해주는 메소드 임. 똫같이 key 는 “tigensoft” 로 하였음.

근데 이렇게 만들면 다 오류가 날꺼임.

외부 라이브러리(jar)가 4개가 필요함

필요한 라이브러리는 다음과 같고, jasypt 는 암/복호화 라이브러리 DB때도 썻던 것 나머지는 tomcat 폴더 하위에 lib 폴더에 coyote, utile 은 있고, juli 는 bin 폴더에 있음.

이제 다 넣었으면 잘 작동 할 꺼임.

이제 jar 파일로 추출이 필요함.

Project Structure > Artifacts > + 버튼 > JAR > From module…

다음과 같이 나오면 모듈이 잘 들어갔나 보고 Main Class 를 아까 만든 클래스로 설정. 그 후 OK

다음과 같이 생겼다면 Output directory 경로 확인 후 OK

이제 Build 만 하면 Jar 는 생성 됨.

Build > Build Artifacts

Build 하면 끝, 아까 확인한 경로로 가보면

다음과 같이 jar 가 생성됨.

이제 이 생성된 파일을 tomcat/lib 폴더에 넣기

다음과 같이 넣었다면 이제 server.xml 을 수정해 주면 끝난다.

1
2
3
4
<Connector port="8443" protocol="com.apache.CustomHttp11Nio2Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true" keystoreFile="ssl/keystore.jks"
                keystorePass="Cx3Xwyw5ApVJEpzsLxHcpw==" sslProtocol="TLS">
</Connector>

다음과 같이 ssl 설정 부분을 변경해줬다.

참고로 암호화 하는 방법은

lib 폴더 > cmd > java -jar tomcatDecrypt.jar

이렇게 만들어 놨다.

이제 실행해 보면

다음과 같이 잘 적용된 걸 볼 수 있다!

태그:

카테고리:

업데이트:

댓글남기기