본문 바로가기

카테고리 없음

[Spring Security]프로젝트 회고 3.비밀번호 암호화

관리자가 내 비밀번호를 알고 있다면 내 모든 개인정보를 조회 할수 있고

이는 사용자로서 매우 불쾌하게 느껴질 것이다.

Spring security에서 제공하는 Bcrypt를 통해 비밀번호를 암호화 할수 있고

이를 통해 사용자의 정보를 보호 할 수 있다.

 

 

pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-core</artifactId>
			<version>4.0.2.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>4.0.2.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>4.0.2.RELEASE</version>
		</dependency>

우선 pom.xml 에 자신의 스프링 버전과 맞게 security관련 dependancy를 작성해준다.

 

security-context.xml

webpp>WEB-INF-appServlet에 security-context.xml파일을 만든다.

 

<?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.xsd">

	<!-- bcryptPasswordEncoder -->
   <bean id="bcryptPasswordEncoder"
   class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

</beans>

그 후 xml 파일에 bcrypt password bean을 추가해준다.

 

web.xml

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:config/*.xml
					/WEB-INF/spring/appServlet/security-context.xml
					
		</param-value>
	</context-param>

web.xml에 security경로를 추가해준다.

 

여기까지 spring security 암호화의 설정은 끝났고, 내가 필요한 부분(회원가입이나 소셜로그인 정보) 에 암호화 처리를 하면 된다. 

 

ex) 회원가입시 암호화 하기 

		try{		
			String passBcrypt = bcryptPasswordEncoder.encode(users.getPassword());
			   users.setPassword(passBcrypt);
			   usersService.joinUsers(users);
			   System.out.println("암호화 후 : " + users);

usersService.joinUsers(users);

로 xml에 있는 insert 쿼리문을 호출 하기전에 Bcrypt암호화를 해준다.

 

 

이후 회원가입시 비밀번호는 모두 암호화 처리 된다.