04-26 18:42
Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
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
Archives
관리 메뉴

기록을 합시다.

[Spring] Spring security로 비밀번호 암호화 본문

공부/Java

[Spring] Spring security로 비밀번호 암호화

울집고양이세마리 2023. 5. 15. 19:46

Dependency 등록

[project]에 pom.xml 파일에 dependency 3개 등록해준다.

Maven Repository: org.springframework.security » spring-security-core (mvnrepository.com)

Maven Repository: org.springframework.security » spring-security-web (mvnrepository.com)

Maven Repository: org.springframework.security » spring-security-config (mvnrepository.com)

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

Bean 등록

[project] => [src] => [main] => [webapp]=> [WEB-INF] => [spring] 에 spring-security.xml 생성 

spring-security.xml이 아니더라도 root-context.xml에 등록을 해도 되지만, root-context.xml이 너무 길어지면 보기 힘들기 때문에 나눠서 작성해줬다.

//sprint-security.xml
<?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.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security.xsd">
      
    <beans:bean id="bcryptPasswordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" /> 
</beans:beans>

Controller 작성

비밀번호의 암호화는 BCryptPasswordEncoder를 이용한다. 아래는 BCryptPasswordEncoder의 특징이다. 

  • BCryptPasswordEncoder는 스프링 시큐리티 프레임워크에서 제공하는 비밀번호 암호화 클래스 중 하나이며, 임의의 salt 값을 이용해 암호화된 결과를 생성한다.
  • BCryptPasswordEncoder 클래스는 인코딩을 위한 encode() 메소드와 매칭을 위한 matches() 메소드를 제공한다. encode() 메소드는 비밀번호를 암호화한 문자열을 반환하며, matches() 메소드는 암호화된 문자열과 비밀번호를 비교하여 둘의 내용이 일치하는지 알려준다. 
  • BCryptPasswordEncoder는 단방향 암호화 알고리즘이기 때문에, 암호화된 문자열을 다시 원래 비밀번호로 복호화할 수 없다.
public class BoardController {
	@Autowired //비밀번호 암호화 이존성 주입
	private BCryptPasswordEncoder pwdEncoder;
    
    @GetMapping("/user/login")
	public void getLogin(HttpSession session) throws Exception {
		String password_before = "12345";
		String password_after = pwdEncoder.encode(password_before);
       		pwdEncoder.matches(password_before, password_after);
	}
}

 

    스프링 시큐리티의 빙산의 일각만을 맛 보았다.. 다른 분들의 Configuration 파일을 본 적이 있는데, 도저히 이해할 수 없었다. 지금은 쉬워도 나중에는 어떻게 될지 참 ㅜㅜ.. 공식 문서를 보아도 api들만 달랑 적혀있어서 지식을 얻기가 조금 힘들다.. 그래도 공부해야지.. 
Comments