[Spring / BootStrap] Spring Legacy MVC Project로 게시판 및 채팅 기능 구현 - 5 (한글 EncodingFitler / 비밀번호 암호화 SpringSecurity)

리트리버J

·

2021. 1. 11. 22:53

728x90

- 한글(UTF-8) Encoding - 

1. 게시판 INSERT를 해보면, 한글이 깨진다.

EncodingFilter 적용 전

2. web.xml에 encodingFilter를 등록해준다!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    <!-- 한글 깨짐 방지 -->
    <!-- 
        filter : 필터는 웹 애플리케이션 전반에 걸쳐 특정 URL이나 파일 요청 시
        먼저 로딩되어 사전에 처리할 작업을 수행(필터링)하고 해당 요청을 처리하는 자바 웹 애플리케이션 유형 중 하나이다. 
    -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <!-- 
        <filter-mapping> 에서는 해당 필터를 적용할 URL패턴이나 Servlet 등을 등록한다. 
        스프링 시큐리티 예제를 만들어보면서도 시큐리티 관련 filter를 등록해야 한다. 
    -->
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
cs

3. 출력 확인

EncodingFilter 적용 후

- 비밀번호 암호화 SpringSecurity - 

4. pom.xml에 dependcy를 설정해준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- 2. Spring Security Module -->
        <!-- core / web / config 3가지의 라이브러리 필요 -->
        <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>5.2.1.RELEASE</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>5.2.1.RELEASE</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>5.2.1.RELEASE</version>
        </dependency>
<!-- // Spring Security Module -->
cs

5. src/java/resources 경로에 Spring Bean Configuration File 클릭

6. 위의 XSD에서 security 클릭 후 Finish

6. bcryptPasswordEncoder bean 등록

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-5.1.xsd">
 
    <!-- bcryptPasswordEncoder -->
    <bean id="bcryptPasswordEncoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
 
</beans>
 
cs

7. web.xml의 <param-value>에 spring-security.xml 등록

1
2
3
4
5
6
7
8
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:root-context.xml
            classpath:spring-security.xml
        </param-value>
    </context-param>
cs

8. BoardController에 @Autowired를 통한 BCryptPasswordEncoder 필드변수 생성

9. bcryptPasswordEncoder.encode("원래비밀번호"); 메소드로 암호화

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
     /**
      * 4. 게시글 작성 메소드
      * @param u
      * @param model
      * @return
      */
     @RequestMapping("/write/complete")
     public String InsertMember(Board board, Model model) {
         
         // 확인용
         System.out.println(board);
         
         // BCryptPasswordEncoder클래스의 encode 메소드를 통한 암호화 처리
         board.setbPasswordEnc(bcryptPasswordEncoder.encode(board.getbPassword()));
         
         int result = service.InsertBoard(board);
         
         if(result == 1) {
              return "redirect:/board";
         }else {
             return "redirect:/write";
         }
     }
cs

9. SELECT * FROM BOARD;

기본 비밀번호 / 암호화된 비밀번호

728x90