평문(데이터)
알고리즘(algorithm)
키(key)
[평문] -> 암호화 알고리즘 + Key -> <암호문>
<암호문> -> 복호화 알고리즘 + Key -> [평문]
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<import resource="/jdbc-Context.xml"/>
<import resource="/aop-Context.xml"/>
</beans>
package kr.or.iei.common;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.springframework.stereotype.Component;
@Component
public class SHA256Util {
public String encryptionData(String data, String salt) throws Exception {
MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
String str=data+salt;
mDigest.update(str.getBytes());
byte[] encryptMsg = mDigest.digest(); // 암호화 처리한 값 리턴
StringBuffer hexString=new StringBuffer();
for(byte b : encryptMsg) {
hexString.append(String.format("%02X",b));
}
return hexString.toString();
}
}
@Aspect
@Component
public class MemberAOP {
@Autowired
private SHA256Util enc;
// 회원가입 암호화 처리 AOP 로직
@Pointcut("execution(int kr.or.iei.member.model.service.MemberServiceImpl.insertMember(..))")
public void insertMemberPointCut() {
}
@Before("insertMemberPointCut()")
public void insertMemberPasswordEncryption(JoinPoint jp) throws Exception {
Member m = (Member) jp.getArgs()[0];
String userPwd = m.getUserPwd();
String userId = m.getUserId();
String encryptPwd = enc.encryptionData(userPwd, userId);
m.setUserPwd(encryptPwd);
}
@Controller
public class MemberController {
@Autowired
private MemberService mService;
@RequestMapping(value = "/member/memberJoin.do", method = RequestMethod.POST)
public ModelAndView memberJoinus(Member m, ModelAndView mav) {
// view페이지 보낸 데이터 받는 방법
// 1. request 객체 사용 - request.getParameter();
// 2. @requestParam 사용 - ex) @requestParam String userId
// 3. VO를 활용하는 방법(서로 다른 여러 객체 사용 가능) - ex) 인자 값을 Member m 설정
int result = mService.insertMember(m);
if (result > 0) {
mav.addObject("msg", "회원 가입 성공");
mav.addObject("location", "/");
} else {
mav.addObject("msg", "회원 가입 실패 - 지속적인 문제 발생시 관리자에게 문의 -");
mav.addObject("location", "/member/joinPage.do");
}
mav.setViewName("member/msg");
return mav;
}
public interface MemberService {
int insertMember(Member m);
}
@Service
public class MemberServiceImpl implements MemberService{
@Override
public int insertMember(Member m) {
return mDAO.insertMember(m);
}
}
@Repository
public class MemberDAO {
@Autowired
@Qualifier(value = "jdbcTemplate")
JdbcTemplate jdbc;
public int insertMember(Member m) {
String query="INSERT INTO MEMBER VALUES(MEMBER_SEQ.NEXTVAL, ?, ?, ?, ?, ?, SYSDATE, 'N')";
return jdbc.update(query, m.getUserId(), m.getUserPwd(), m.getUserName(), m.getAge(), m.getAddress());
}
}