-
[spring] 비밀번호 암호화Study/Java 2020. 7. 5. 20:35
UserService.java
1234567891011121314151617181920212223package simple.user;import java.util.List;import org.springframework.security.crypto.password.PasswordEncoder;public interface UserService {public List<UserVo> selectUserList(UserVo userVo);public UserVo selectUserOne(UserVo userVo);public int insertUser(UserVo userVo);public int updateUser(UserVo userVo);public int deleteUser(String userId);public PasswordEncoder passwordEncoder();}cs public PasswordEncoder passwordEncoder(); 추가
UserServiceImpl.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253package simple.user;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.security.crypto.password.PasswordEncoder;import org.springframework.stereotype.Service;@Servicepublic class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();@Overridepublic List<UserVo> selectUserList(UserVo userVo) {return userDao.selectUserList(userVo);}@Overridepublic UserVo selectUserOne(UserVo userVo) {return userDao.selectUserOne(userVo);}@Overridepublic int insertUser(UserVo userVo) {String rawPassword = userVo.getPasswd();String encodePasswd = passwordEncoder.encode(rawPassword);userVo.setPasswd(encodePasswd);return userDao.insertUser(userVo);}@Overridepublic int updateUser(UserVo userVo) {return userDao.updateUser(userVo);}@Overridepublic int deleteUser(String userId) {return userDao.deleteUser(userId);}@Overridepublic PasswordEncoder passwordEncoder() {return this.passwordEncoder;}}cs private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
@Override
public PasswordEncoder passwordEncoder() {
return this.passwordEncoder;
}
코드 추가
AuthProvider.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071package simple.configuration;import java.util.ArrayList;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.security.authentication.AuthenticationProvider;import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;import org.springframework.security.core.Authentication;import org.springframework.security.core.AuthenticationException;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.authority.SimpleGrantedAuthority;import org.springframework.security.crypto.password.PasswordEncoder;import org.springframework.security.web.session.HttpSessionEventPublisher;import org.springframework.stereotype.Component;import com.barunsw.simple.user.UserService;import com.barunsw.simple.user.UserVo;@Componentpublic class AuthProvider implements AuthenticationProvider {@Autowiredprivate UserService userService;@Overridepublic Authentication authenticate(Authentication authentication) throws AuthenticationException {String userIdInput = authentication.getName();String passwdInput = (String) authentication.getCredentials();// 패스워드 암호화PasswordEncoder passwordEncoder = userService.passwordEncoder();//String encoderPasswd = passwordEncoder.encode(passwdInput);UsernamePasswordAuthenticationToken token;UserVo userVo = userService.selectUserOne(new UserVo(userIdInput));if ( userVo != null && passwordEncoder.matches(passwdInput, userVo.getPasswd()) ) {List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();roles.add(new SimpleGrantedAuthority("USER"));token = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), authentication.getCredentials(), roles);token.setDetails(userVo);System.out.println("Authentication 일치하는 id, passwd 존재");System.out.println("token" + token);return token;}else {System.out.println("Authentication 일치하는 id, passwd 없음");return null;}}@Overridepublic boolean supports(Class<?> authentication) {return authentication.equals(UsernamePasswordAuthenticationToken.class);}}cs // 패스워드 암호화
PasswordEncoder passwordEncoder = userService.passwordEncoder();
//String encoderPasswd = passwordEncoder.encode(passwdInput);
UsernamePasswordAuthenticationToken token;
코드 추가하고 if문에 비밀번호검사 조건문 코드 작성
'Study > Java' 카테고리의 다른 글
[Eclipse] 이클립스 commit시 안올릴 파일 무시하기 ignored Resources (0) 2020.07.28 [Class String] Encode/Decode Constructor, Method (0) 2020.07.16 [Spring] spring boot security - login 만들기 (0) 2020.07.05 [spring, jQWidgets] 주소록 CRUD 만들기 (0) 2020.07.05 Http -> Https TLS/SSL 적용하기 (0) 2020.06.30