본문 바로가기

📋 전자정부프레임워크 4.1.0 스프링 시큐리티로 암호화하기 📋


context-security.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 https://www.springframework.org/schema/security/spring-security.xsd">

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

 

pom.xml <spring v5.3.20>
<dependencies>
	<!-- 기존 라이브러리 ··· -->
	<!-- 스프링 시큐리티 **비밀번호 암호화** -->
	<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
	<dependency>
		<groupId>org.springframework.security</groupId>
		<artifactId>spring-security-core</artifactId>
		<version>5.7.1</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.7.1</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.7.1</version>
	</dependency>
</dependencies>

 

main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>main</title>
</head>
<body>
	<h1>test main</h1>
	<h2>전체회원 수 ${ num }</h2>
	
	<form action="sub.do" method="post">
		암호: <input type="text" name="password">
		<button type="submit">전송</button>
	</form>
</body>
</html>

 

MemberController.java
package egovframework.example.sample.web;



import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import egovframework.example.sample.service.*;

@Controller
public class MemberController {

	@Autowired private MemberService memberservice;
	@Autowired private BCryptPasswordEncoder bCryptPasswordEncoder;
	
	@RequestMapping(value="/main.do", method=RequestMethod.GET)
	public String main(Model model) throws Exception {
		int num = memberservice.select_membercount();	
		model.addAttribute("num", num);
		return "main";
	}
	
	@RequestMapping(value="/sub.do", method = RequestMethod.POST)
	public String sub(String password, HttpSession session) throws Exception {
		// 입력한 암호를 출력
		System.out.println("password: " + password);				
		// password 값을 암호화
		String encodedPW = bCryptPasswordEncoder.encode(password);
		// 암호화된 encodedPW 값을 출력
		System.out.println("encodedPW: " + encodedPW);				
		return "redirect:/";
	}
}

 


Console 출력 값