지대한
1 year ago
4 changed files with 244 additions and 254 deletions
@ -1,238 +1,238 @@
|
||||
package com.palnet.biz.api.acnt.jwt.service; |
||||
|
||||
import java.time.Instant; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
import java.util.Optional; |
||||
|
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import com.palnet.biz.api.acnt.cstmr.service.AcntCstmrService; |
||||
import com.palnet.biz.api.acnt.jwt.model.JwtProfileRsModel; |
||||
import com.palnet.biz.api.acnt.jwt.model.JwtRqModel; |
||||
import com.palnet.biz.api.acnt.jwt.model.JwtRsModel; |
||||
import com.palnet.biz.api.acnt.jwt.model.JwtUserModel; |
||||
import com.palnet.biz.api.acnt.jwt.utils.JwtTokenUtil; |
||||
import com.palnet.biz.api.comn.response.ErrorResponse; |
||||
import com.palnet.biz.api.comn.response.SuccessResponse; |
||||
import com.palnet.biz.jpa.entity.CtrCntrlBas; |
||||
import com.palnet.biz.jpa.entity.PtyCstmrBas; |
||||
import com.palnet.biz.jpa.entity.PtyCstmrConectHist; |
||||
import com.palnet.biz.jpa.repository.pty.PtyCstmrBasRepository; |
||||
import com.palnet.biz.jpa.repository.pty.PtyCstmrConectHistRepository; |
||||
import com.palnet.biz.jpa.repository.pty.PtyCstmrDtlRepository; |
||||
import com.palnet.biz.jpa.repository.pty.PtyCstmrQueryRepository; |
||||
import com.palnet.comn.code.ErrorCode; |
||||
import com.palnet.comn.exception.CustomException; |
||||
import com.palnet.comn.utils.DateUtils; |
||||
import com.palnet.comn.utils.EncryptUtils; |
||||
import com.palnet.comn.utils.HttpUtils; |
||||
import lombok.RequiredArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import lombok.extern.log4j.Log4j2; |
||||
import java.time.Instant; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
import java.util.Optional; |
||||
|
||||
@Slf4j |
||||
@RequiredArgsConstructor |
||||
@Service |
||||
@Log4j2 |
||||
public class JwtService { |
||||
@Autowired |
||||
private PtyCstmrBasRepository ptyCstmrBasRepository; |
||||
|
||||
@Autowired |
||||
private PtyCstmrConectHistRepository ptyCstmrConectHistRepository; |
||||
|
||||
@Autowired |
||||
private JwtUserDetailsService userDetailsService; |
||||
|
||||
@Autowired |
||||
private PtyCstmrQueryRepository query ; |
||||
|
||||
@Autowired |
||||
private JwtTokenUtil jwtTokenUtil; |
||||
|
||||
/** |
||||
* 로그인 처리 |
||||
* @param rq |
||||
* @return |
||||
*/ |
||||
public Map<String , Object> loginProcess(JwtRqModel rq) throws Exception{ |
||||
|
||||
int loginError = 1; // -100 : 아이디/비밀번호가 없습니다 , -101 : 계정정보를 찾을수 없습니다 , -102 : 비밀번호가 잘못 되었습니다 , -103 : 계정을 사용할수 없습니다.
|
||||
|
||||
Map<String , Object> resultMap = new HashMap<String , Object>(); |
||||
|
||||
//입력값 검증 처리
|
||||
if(StringUtils.isEmpty(rq.getUserId()) || StringUtils.isEmpty(rq.getUserPswd())) { |
||||
loginError = -100; |
||||
} |
||||
|
||||
JwtUserModel userDetails = (JwtUserModel)userDetailsService |
||||
.loadUserByUsername(rq.getUserId()); |
||||
|
||||
|
||||
//계정이 없는경우
|
||||
if(userDetails == null) { |
||||
loginError = -101; |
||||
}else{ |
||||
String password = EncryptUtils.sha256Encrypt(rq.getUserPswd()); |
||||
|
||||
//비밀번호 검증 처리
|
||||
if (!userDetails.getPassword().equals(password)) { |
||||
loginError = -102; |
||||
|
||||
} |
||||
|
||||
//계정 검증로직
|
||||
if(!userDetails.isAccountNonLocked() || !userDetails.isAccountNonExpired() || !userDetails.isEnabled() || !userDetails.isCredentialsNonExpired()) { |
||||
loginError = -103; |
||||
|
||||
} |
||||
} |
||||
|
||||
|
||||
if(loginError < 0) { |
||||
|
||||
String errorMessage = ""; |
||||
if(loginError == -100) { |
||||
errorMessage = "Please parameter Check"; |
||||
}else if(loginError == -101) { |
||||
errorMessage = "Account not found"; |
||||
|
||||
}else if(loginError == -102) { |
||||
errorMessage = "Password does not match"; |
||||
}else if(loginError == -103) { |
||||
errorMessage = "Account is unavailable"; |
||||
} |
||||
//실패 이력 저장
|
||||
//cstmrSno , String loginYn , String errorCode
|
||||
if(userDetails != null) { |
||||
this.historySave(userDetails.getCstmrSno(), "N", loginError+""); |
||||
} |
||||
|
||||
resultMap.put("loginError", loginError); |
||||
resultMap.put("errorMessage", errorMessage); |
||||
|
||||
return resultMap; |
||||
|
||||
}else { |
||||
|
||||
String accessToken = jwtTokenUtil.generateToken(userDetails); |
||||
String refreshToken = jwtTokenUtil.generateRefreshToken(userDetails); |
||||
|
||||
JwtRsModel result = new JwtRsModel(); |
||||
result.setAccessToken(accessToken); |
||||
result.setRefreshToken(refreshToken); |
||||
result.setAuth(userDetails.getAuth()); |
||||
result.setUserId(userDetails.getUserId()); |
||||
result.setCstmrSno(userDetails.getCstmrSno()); |
||||
//토큰 저장 처리
|
||||
log.debug("========= refresh>>>>" + refreshToken); |
||||
this.refreshTokenSave(userDetails.getCstmrSno(), refreshToken); |
||||
|
||||
//성공이력 저장
|
||||
this.historySave(userDetails.getCstmrSno(), "Y", loginError+""); |
||||
|
||||
resultMap.put("loginError", loginError); |
||||
resultMap.put("errorMessage", ""); |
||||
resultMap.put("result", result); |
||||
|
||||
return resultMap; |
||||
} |
||||
|
||||
} |
||||
|
||||
public PtyCstmrBas logoutProcess(int cstmrSno) throws Exception{ |
||||
|
||||
Optional<PtyCstmrBas> optional = ptyCstmrBasRepository.findById(cstmrSno); |
||||
if (optional.isPresent()) { |
||||
PtyCstmrBas entity = optional.get(); |
||||
entity.setRfrshToken(""); |
||||
return ptyCstmrBasRepository.save(entity); |
||||
}else { |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
/** |
||||
* 로그인 이력 저장 |
||||
* @param cstmrSno |
||||
* @param loginYn |
||||
* @param errorCode |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public PtyCstmrConectHist historySave(int cstmrSno , String loginYn , String errorCode) throws Exception{ |
||||
String conectIp = HttpUtils.getRequestIp(); |
||||
|
||||
PtyCstmrConectHist entity = new PtyCstmrConectHist(); |
||||
entity.setCstmrSno(cstmrSno); |
||||
entity.setConectSucesYn(loginYn); |
||||
entity.setConectErrorCd(errorCode); |
||||
entity.setConectIp(conectIp); |
||||
entity.setConectDt(Instant.now()); |
||||
return ptyCstmrConectHistRepository.save(entity); |
||||
} |
||||
|
||||
/** |
||||
* refresh Token 저장 처리 |
||||
* @param cstmrSno |
||||
* @param refreshToken |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public PtyCstmrBas refreshTokenSave(int cstmrSno , String refreshToken) throws Exception{ |
||||
|
||||
Optional<PtyCstmrBas> optional = ptyCstmrBasRepository.findById(cstmrSno); |
||||
|
||||
if (!optional.isPresent()) { |
||||
throw new CustomException(ErrorCode.DATA_NOTFIND); |
||||
} |
||||
|
||||
PtyCstmrBas entity = optional.get(); |
||||
entity.setRfrshToken(refreshToken); |
||||
|
||||
return ptyCstmrBasRepository.save(entity); |
||||
} |
||||
|
||||
/** |
||||
* 프로필 조회 |
||||
* @param cstmrSno |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public JwtProfileRsModel profile(int cstmrSno) throws Exception{ |
||||
JwtProfileRsModel model = query.findUserProfile(cstmrSno); |
||||
|
||||
private final PtyCstmrBasRepository ptyCstmrBasRepository; |
||||
private final PtyCstmrDtlRepository ptyCstmrDtlRepository; |
||||
private final PtyCstmrConectHistRepository ptyCstmrConectHistRepository; |
||||
private final JwtUserDetailsService userDetailsService; |
||||
private final PtyCstmrQueryRepository query; |
||||
private final JwtTokenUtil jwtTokenUtil; |
||||
|
||||
/** |
||||
* 로그인 처리 |
||||
* |
||||
* @param rq |
||||
* @return |
||||
*/ |
||||
public Map<String, Object> loginProcess(JwtRqModel rq) throws Exception { |
||||
|
||||
int loginError = 1; // -100 : 아이디/비밀번호가 없습니다 , -101 : 계정정보를 찾을수 없습니다 , -102 : 비밀번호가 잘못 되었습니다 , -103 : 계정을 사용할수 없습니다.
|
||||
|
||||
Map<String, Object> resultMap = new HashMap<String, Object>(); |
||||
|
||||
//입력값 검증 처리
|
||||
if (StringUtils.isEmpty(rq.getUserId()) || StringUtils.isEmpty(rq.getUserPswd())) { |
||||
loginError = -100; |
||||
} |
||||
|
||||
JwtUserModel userDetails = (JwtUserModel) userDetailsService |
||||
.loadUserByUsername(rq.getUserId()); |
||||
|
||||
|
||||
//계정이 없는경우
|
||||
if (userDetails == null) { |
||||
loginError = -101; |
||||
} else { |
||||
String password = EncryptUtils.sha256Encrypt(rq.getUserPswd()); |
||||
|
||||
//비밀번호 검증 처리
|
||||
if (!userDetails.getPassword().equals(password)) { |
||||
loginError = -102; |
||||
|
||||
} |
||||
|
||||
//계정 검증로직
|
||||
if (!userDetails.isAccountNonLocked() || !userDetails.isAccountNonExpired() || !userDetails.isEnabled() || !userDetails.isCredentialsNonExpired()) { |
||||
loginError = -103; |
||||
|
||||
} |
||||
} |
||||
|
||||
|
||||
if (loginError < 0) { |
||||
|
||||
String errorMessage = ""; |
||||
if (loginError == -100) { |
||||
errorMessage = "Please parameter Check"; |
||||
} else if (loginError == -101) { |
||||
errorMessage = "Account not found"; |
||||
|
||||
} else if (loginError == -102) { |
||||
errorMessage = "Password does not match"; |
||||
} else if (loginError == -103) { |
||||
errorMessage = "Account is unavailable"; |
||||
} |
||||
//실패 이력 저장
|
||||
//cstmrSno , String loginYn , String errorCode
|
||||
if (userDetails != null) { |
||||
this.historySave(userDetails.getCstmrSno(), "N", loginError + ""); |
||||
} |
||||
|
||||
resultMap.put("loginError", loginError); |
||||
resultMap.put("errorMessage", errorMessage); |
||||
|
||||
return resultMap; |
||||
|
||||
} else { |
||||
|
||||
String accessToken = jwtTokenUtil.generateToken(userDetails); |
||||
String refreshToken = jwtTokenUtil.generateRefreshToken(userDetails); |
||||
|
||||
JwtRsModel result = new JwtRsModel(); |
||||
result.setAccessToken(accessToken); |
||||
result.setRefreshToken(refreshToken); |
||||
result.setAuth(userDetails.getAuth()); |
||||
result.setUserId(userDetails.getUserId()); |
||||
result.setCstmrSno(userDetails.getCstmrSno()); |
||||
|
||||
ptyCstmrDtlRepository.findById(userDetails.getCstmrSno()).ifPresent(ptyCstmrDtl -> { |
||||
result.setClncd(ptyCstmrDtl.getClncd()); |
||||
result.setHpno(ptyCstmrDtl.getHpno()); |
||||
result.setEmail(ptyCstmrDtl.getEmail()); |
||||
result.setBrthdyDate(ptyCstmrDtl.getBrthdyDate()); |
||||
}); |
||||
|
||||
//토큰 저장 처리
|
||||
log.debug("========= refresh>>>>" + refreshToken); |
||||
this.refreshTokenSave(userDetails.getCstmrSno(), refreshToken); |
||||
|
||||
//성공이력 저장
|
||||
this.historySave(userDetails.getCstmrSno(), "Y", loginError + ""); |
||||
|
||||
resultMap.put("loginError", loginError); |
||||
resultMap.put("errorMessage", ""); |
||||
resultMap.put("result", result); |
||||
|
||||
return resultMap; |
||||
} |
||||
|
||||
} |
||||
|
||||
public PtyCstmrBas logoutProcess(int cstmrSno) throws Exception { |
||||
|
||||
Optional<PtyCstmrBas> optional = ptyCstmrBasRepository.findById(cstmrSno); |
||||
if (optional.isPresent()) { |
||||
PtyCstmrBas entity = optional.get(); |
||||
entity.setRfrshToken(""); |
||||
return ptyCstmrBasRepository.save(entity); |
||||
} else { |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
/** |
||||
* 로그인 이력 저장 |
||||
* |
||||
* @param cstmrSno |
||||
* @param loginYn |
||||
* @param errorCode |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public PtyCstmrConectHist historySave(int cstmrSno, String loginYn, String errorCode) throws Exception { |
||||
String conectIp = HttpUtils.getRequestIp(); |
||||
|
||||
PtyCstmrConectHist entity = new PtyCstmrConectHist(); |
||||
entity.setCstmrSno(cstmrSno); |
||||
entity.setConectSucesYn(loginYn); |
||||
entity.setConectErrorCd(errorCode); |
||||
entity.setConectIp(conectIp); |
||||
entity.setConectDt(Instant.now()); |
||||
return ptyCstmrConectHistRepository.save(entity); |
||||
} |
||||
|
||||
/** |
||||
* refresh Token 저장 처리 |
||||
* |
||||
* @param cstmrSno |
||||
* @param refreshToken |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public PtyCstmrBas refreshTokenSave(int cstmrSno, String refreshToken) throws Exception { |
||||
|
||||
Optional<PtyCstmrBas> optional = ptyCstmrBasRepository.findById(cstmrSno); |
||||
|
||||
if (!optional.isPresent()) { |
||||
throw new CustomException(ErrorCode.DATA_NOTFIND); |
||||
} |
||||
|
||||
PtyCstmrBas entity = optional.get(); |
||||
entity.setRfrshToken(refreshToken); |
||||
|
||||
return ptyCstmrBasRepository.save(entity); |
||||
} |
||||
|
||||
/** |
||||
* 프로필 조회 |
||||
* |
||||
* @param cstmrSno |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public JwtProfileRsModel profile(int cstmrSno) throws Exception { |
||||
JwtProfileRsModel model = query.findUserProfile(cstmrSno); |
||||
// model.setMemberName(EncryptUtils.decrypt(model.getMemberName()));
|
||||
|
||||
return model; |
||||
} |
||||
|
||||
/** |
||||
* token 만료시 refresh 토큰으로 재검색 |
||||
* @param cstmrSno |
||||
* @param refreshToken |
||||
* @return |
||||
*/ |
||||
public JwtRsModel findRefreshtoken(int cstmrSno , String refreshToken) { |
||||
JwtUserModel userDetails = query.findRefreshtoken(cstmrSno, refreshToken); |
||||
|
||||
if(userDetails == null) { |
||||
return null; |
||||
} |
||||
|
||||
String accessToken = jwtTokenUtil.generateToken(userDetails); |
||||
|
||||
JwtRsModel result = new JwtRsModel(); |
||||
result.setAccessToken(accessToken); |
||||
result.setRefreshToken(refreshToken); |
||||
result.setAuth(userDetails.getAuth()); |
||||
result.setUserId(userDetails.getUserId()); |
||||
result.setCstmrSno(userDetails.getCstmrSno()); |
||||
|
||||
return result; |
||||
} |
||||
|
||||
|
||||
return model; |
||||
} |
||||
|
||||
/** |
||||
* token 만료시 refresh 토큰으로 재검색 |
||||
* |
||||
* @param cstmrSno |
||||
* @param refreshToken |
||||
* @return |
||||
*/ |
||||
public JwtRsModel findRefreshtoken(int cstmrSno, String refreshToken) { |
||||
JwtUserModel userDetails = query.findRefreshtoken(cstmrSno, refreshToken); |
||||
|
||||
if (userDetails == null) { |
||||
return null; |
||||
} |
||||
|
||||
String accessToken = jwtTokenUtil.generateToken(userDetails); |
||||
|
||||
JwtRsModel result = new JwtRsModel(); |
||||
result.setAccessToken(accessToken); |
||||
result.setRefreshToken(refreshToken); |
||||
result.setAuth(userDetails.getAuth()); |
||||
result.setUserId(userDetails.getUserId()); |
||||
result.setCstmrSno(userDetails.getCstmrSno()); |
||||
|
||||
return result; |
||||
} |
||||
|
||||
} |
||||
|
@ -1,41 +1,29 @@
|
||||
package com.palnet.biz.api.acnt.jwt.service; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.security.core.userdetails.UserDetailsService; |
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import com.palnet.biz.api.acnt.jwt.model.JwtUserModel; |
||||
import com.palnet.biz.jpa.repository.pty.PtyCstmrBasRepository; |
||||
import com.palnet.biz.jpa.repository.pty.PtyCstmrQueryRepository; |
||||
import com.palnet.comn.utils.JsonUtils; |
||||
|
||||
import lombok.RequiredArgsConstructor; |
||||
import lombok.extern.log4j.Log4j2; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.security.core.userdetails.UserDetailsService; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
@Slf4j |
||||
@Service |
||||
@RequiredArgsConstructor |
||||
@Log4j2 |
||||
public class JwtUserDetailsService implements UserDetailsService{ |
||||
|
||||
@Autowired |
||||
private PtyCstmrQueryRepository query ; |
||||
|
||||
@Autowired |
||||
private PtyCstmrBasRepository repository; |
||||
|
||||
|
||||
|
||||
@Override |
||||
public JwtUserModel loadUserByUsername(String username){ |
||||
|
||||
JwtUserModel model = query.findUserPassword(username); |
||||
// log.debug("jwtUser>>>>" + JsonUtils.toJson(model));
|
||||
if(model == null) { |
||||
return null; |
||||
}else { |
||||
return model; |
||||
} |
||||
|
||||
} |
||||
public class JwtUserDetailsService implements UserDetailsService { |
||||
|
||||
private final PtyCstmrQueryRepository query; |
||||
|
||||
@Override |
||||
public JwtUserModel loadUserByUsername(String username) { |
||||
|
||||
JwtUserModel model = query.findUserPassword(username); |
||||
|
||||
if (model == null) { |
||||
return null; |
||||
} else { |
||||
return model; |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue