Browse Source

jwt패키지 추석 추가

feature/document/spring-rest-docs
lkd9125(이경도) 10 months ago
parent
commit
a255674a66
  1. 58
      pav-server/src/main/java/com/palnet/biz/api/acnt/jwt/controller/JwtAuthenticationController.java
  2. 33
      pav-server/src/main/java/com/palnet/biz/api/acnt/jwt/service/JwtService.java
  3. 34
      pav-server/src/main/java/com/palnet/biz/jpa/repository/pty/PtyCstmrQueryRepository.java

58
pav-server/src/main/java/com/palnet/biz/api/acnt/jwt/controller/JwtAuthenticationController.java

@ -41,20 +41,30 @@ public class JwtAuthenticationController {
@Autowired @Autowired
private JwtService service; private JwtService service;
/**
* 로그인 기능,
* JwtRqModel에 입력받은 회원아이디, 입력받은 회원 비밀번호로 인증처리를 .
* @param authenticationRequest
* @return
* @throws Exception
*/
@PostMapping(value = "/login") @PostMapping(value = "/login")
@ApiOperation(value = "로그인") @ApiOperation(value = "로그인")
@Tag(name = "JWT 토큰 이용", description = "JWT 토큰 관련 API") @Tag(name = "JWT 토큰 이용", description = "JWT 토큰 관련 API")
public ResponseEntity<? extends BasicResponse> createAuthenticationToken(@RequestBody JwtRqModel authenticationRequest) throws Exception { public ResponseEntity<? extends BasicResponse> createAuthenticationToken(@RequestBody JwtRqModel authenticationRequest) throws Exception {
// 로그인 프로세스
Map<String , Object> resultMap = service.loginProcess(authenticationRequest);
Map<String , Object> resultMap = service.loginProcess(authenticationRequest); int loginError = (int) resultMap.get("loginError");
int loginError = (int) resultMap.get("loginError");
// loginError 은 1이 정상, - 값은 모두 로그인 실패임
if(loginError < 0) { if(loginError < 0) {
String errorMessage = (String) resultMap.get("errorMessage"); String errorMessage = (String) resultMap.get("errorMessage");
// 로그인 실패시 로그인 실패한 사유를 반환함
return ResponseEntity.status(HttpStatus.OK) return ResponseEntity.status(HttpStatus.OK)
.body(new ErrorResponse(errorMessage, loginError + "")); // 계정 사용하지 못함 .body(new ErrorResponse(errorMessage, loginError + "")); // 계정 사용하지 못함
}else { }else {
@ -65,49 +75,70 @@ public class JwtAuthenticationController {
} }
/**
* 로그인 회원 정보,
* 회원고유번호[cstmrSno] 조회를 .
* @param cstmrSno
* @return
* @throws Exception
*/
@GetMapping(value = "/profile/{cstmrSno}") @GetMapping(value = "/profile/{cstmrSno}")
@ApiOperation(value = "로그인한 회원의 정보") @ApiOperation(value = "로그인한 회원의 정보")
@Tag(name = "JWT 토큰 이용", description = "JWT 토큰 관련 API") @Tag(name = "JWT 토큰 이용", description = "JWT 토큰 관련 API")
@ApiImplicitParam(name = "cstmrSno",value = "고객일련번호", dataTypeClass = Integer.class) @ApiImplicitParam(name = "cstmrSno",value = "고객일련번호", dataTypeClass = Integer.class)
public ResponseEntity<? extends BasicResponse> proflie(@PathVariable Integer cstmrSno) throws Exception{ public ResponseEntity<? extends BasicResponse> proflie(@PathVariable Integer cstmrSno) throws Exception{
JwtProfileRsModel result = service.profile(cstmrSno); JwtProfileRsModel result = service.profile(cstmrSno); // 회원고유번호로 회원정보를 가져오는 기능.
if(result == null) { if(result == null) {
// 반환받은 회원정보가 없을시 "의도적인" 예외코드를 반환해줌
return ResponseEntity.status(HttpStatus.OK) return ResponseEntity.status(HttpStatus.OK)
.body(new ErrorResponse(RSErrorCode.DATA_NOTFOUNT)); .body(new ErrorResponse(RSErrorCode.DATA_NOTFOUNT));
} }
return ResponseEntity.ok().body(new SuccessResponse<JwtProfileRsModel>(result)); return ResponseEntity.ok().body(new SuccessResponse<JwtProfileRsModel>(result));
} }
/**
* 회원의 인가정보 유효기간 종료시,
* 다시 인가정보[토큰] 받는 기능
* @param body
* @return
* @throws Exception
*/
@PostMapping(value = "/refresh") @PostMapping(value = "/refresh")
@Tag(name = "JWT 토큰 이용", description = "JWT 토큰 관련 API") @Tag(name = "JWT 토큰 이용", description = "JWT 토큰 관련 API")
// public ResponseEntity<? extends BasicResponse> refresh(@RequestParam("cstmrSno") int cstmrSno , @RequestParam("refreshToken") String refreshToken) throws Exception{ // public ResponseEntity<? extends BasicResponse> refresh(@RequestParam("cstmrSno") int cstmrSno , @RequestParam("refreshToken") String refreshToken) throws Exception{
public ResponseEntity<? extends BasicResponse> refresh(@RequestBody Map body) throws Exception{ public ResponseEntity<? extends BasicResponse> refresh(@RequestBody Map body) throws Exception{
//입력값 검증 // 회원고유번호[cstmrSno], 회원고유번호[cstmrSno]가 Integer타입인지, 인가정보를 다시받을 토큰값 [refreshToken] 입력값에 대한 검증처리
if(body.get("cstmrSno") == null || body.get("refreshToken") == null || !(body.get("cstmrSno") instanceof Integer)) { if(body.get("cstmrSno") == null || body.get("refreshToken") == null || !(body.get("cstmrSno") instanceof Integer)) {
// 검증이 되지 않으면 "의도적인" 예외코드를 반환해줌
return ResponseEntity.status(HttpStatus.OK) return ResponseEntity.status(HttpStatus.OK)
.body(new ErrorResponse(RSErrorCode.ER_PARAM)); .body(new ErrorResponse(RSErrorCode.ER_PARAM));
} }
int cstmrSno = (int)body.get("cstmrSno"); int cstmrSno = (int)body.get("cstmrSno");
String refreshToken = (String)body.get("refreshToken"); String refreshToken = (String)body.get("refreshToken");
// 재 인가토큰 발급
JwtRsModel result = service.findRefreshtoken(cstmrSno, refreshToken);
JwtRsModel result = service.findRefreshtoken(cstmrSno, refreshToken);
// JwtRsModel result = null; // JwtRsModel result = null;
if(result ==null) { if(result ==null) {
// 토큰발행 실패 시 "의도적인" 예외코드를 반환해줌
return ResponseEntity.status(HttpStatus.OK) return ResponseEntity.status(HttpStatus.OK)
.body(new ErrorResponse(RSErrorCode.DATA_NOTFOUNT)); .body(new ErrorResponse(RSErrorCode.DATA_NOTFOUNT));
} }
return ResponseEntity.ok().body(new SuccessResponse<JwtRsModel>(result)); return ResponseEntity.ok().body(new SuccessResponse<JwtRsModel>(result));
} }
/**
* 로그아웃 기능.
* @param cstmrSno
* @return
* @throws Exception
*/
@GetMapping(value = "/logout/{cstmrSno}") @GetMapping(value = "/logout/{cstmrSno}")
@ApiOperation(value = "로그아웃 한 회원의 정보") @ApiOperation(value = "로그아웃 한 회원의 정보")
@Tag(name = "JWT 토큰 이용", description = "JWT 토큰 관련 API") @Tag(name = "JWT 토큰 이용", description = "JWT 토큰 관련 API")
@ -120,10 +151,11 @@ public class JwtAuthenticationController {
// } // }
// int cstmrSno = (int)body.get("cstmrSno"); // int cstmrSno = (int)body.get("cstmrSno");
// 로그아웃 할 프로세스
PtyCstmrBas bas = service.logoutProcess(cstmrSno); PtyCstmrBas bas = service.logoutProcess(cstmrSno);
if(bas == null) { if(bas == null) {
// 로그아웃 프로세스 실패 시 "의도적인" 예외코드를 반환해줌
return ResponseEntity.status(HttpStatus.OK) return ResponseEntity.status(HttpStatus.OK)
.body(new ErrorResponse(RSErrorCode.DATA_NOTFOUNT)); .body(new ErrorResponse(RSErrorCode.DATA_NOTFOUNT));
} }

33
pav-server/src/main/java/com/palnet/biz/api/acnt/jwt/service/JwtService.java

@ -39,8 +39,7 @@ public class JwtService {
private final JwtTokenUtil jwtTokenUtil; private final JwtTokenUtil jwtTokenUtil;
/** /**
* 로그인 처리 * 로그인 처리하는 기능.
*
* @param rq * @param rq
* @return * @return
*/ */
@ -78,8 +77,8 @@ public class JwtService {
} }
} }
//loginError 값이 - 인경우 에러 메세지와 함꼐 반환
if (loginError < 0) { if (loginError < 0) {
String errorMessage = ""; String errorMessage = "";
if (loginError == -100) { if (loginError == -100) {
@ -103,11 +102,13 @@ public class JwtService {
return resultMap; return resultMap;
} else { } else { //loginError 값이 + 값이라면 로긍니 성공
String accessToken = jwtTokenUtil.generateToken(userDetails); // 인증이후 인가할 정보 만듬.
String accessToken = jwtTokenUtil.generateToken(userDetails);
String refreshToken = jwtTokenUtil.generateRefreshToken(userDetails); String refreshToken = jwtTokenUtil.generateRefreshToken(userDetails);
// 반환할 객체 설정.
JwtRsModel result = new JwtRsModel(); JwtRsModel result = new JwtRsModel();
result.setAccessToken(accessToken); result.setAccessToken(accessToken);
result.setRefreshToken(refreshToken); result.setRefreshToken(refreshToken);
@ -139,9 +140,18 @@ public class JwtService {
} }
/**
* 로그아웃 하는 기능.
* @param cstmrSno
* @return
* @throws Exception
*/
public PtyCstmrBas logoutProcess(int cstmrSno) throws Exception { public PtyCstmrBas logoutProcess(int cstmrSno) throws Exception {
// 회원의 정보를 받아옴
Optional<PtyCstmrBas> optional = ptyCstmrBasRepository.findById(cstmrSno); Optional<PtyCstmrBas> optional = ptyCstmrBasRepository.findById(cstmrSno);
// 회원의 인가정보를 만료시킴
if (optional.isPresent()) { if (optional.isPresent()) {
PtyCstmrBas entity = optional.get(); PtyCstmrBas entity = optional.get();
entity.setRfrshToken(""); entity.setRfrshToken("");
@ -175,7 +185,7 @@ public class JwtService {
} }
/** /**
* refresh Token 저장 처리 * refresh Token 저장 처리 기능
* *
* @param cstmrSno * @param cstmrSno
* @param refreshToken * @param refreshToken
@ -197,8 +207,7 @@ public class JwtService {
} }
/** /**
* 프로필 조회 * 회원의 프로필을 조회하는 기능.
*
* @param cstmrSno * @param cstmrSno
* @return * @return
* @throws Exception * @throws Exception
@ -211,8 +220,7 @@ public class JwtService {
} }
/** /**
* token 만료시 refresh 토큰으로 재검색 * token 만료시 refresh 토큰으로 재검색 하는 기능.
*
* @param cstmrSno * @param cstmrSno
* @param refreshToken * @param refreshToken
* @return * @return
@ -231,7 +239,8 @@ public class JwtService {
Claims claims = jwtTokenUtil.getAllClaimsFromToken(refreshToken); Claims claims = jwtTokenUtil.getAllClaimsFromToken(refreshToken);
Integer cstmrSnoByRefreshToken = claims.get("cstmrSno", Integer.class); Integer cstmrSnoByRefreshToken = claims.get("cstmrSno", Integer.class);
if(cstmrSnoByRefreshToken != cstmrSno) return null; if(cstmrSnoByRefreshToken != cstmrSno) return null;
// 인가 토큰 발행할수 있게 필요한 회원 정보들을 데이터베이스에서 조회함.
JwtUserModel userDetails = query.findByIdForrefreshToken(cstmrSno); JwtUserModel userDetails = query.findByIdForrefreshToken(cstmrSno);
String createAccessToken = jwtTokenUtil.generateToken(userDetails); String createAccessToken = jwtTokenUtil.generateToken(userDetails);

34
pav-server/src/main/java/com/palnet/biz/jpa/repository/pty/PtyCstmrQueryRepository.java

@ -279,6 +279,11 @@ public class PtyCstmrQueryRepository {
} }
/**
* cstmrSno 값으로 회원정보를 조회하는 SQL 기능.
* @param cstmrSno
* @return
*/
public JwtProfileRsModel findUserProfile(int cstmrSno) { public JwtProfileRsModel findUserProfile(int cstmrSno) {
QPtyCstmrBas basEntity = QPtyCstmrBas.ptyCstmrBas; QPtyCstmrBas basEntity = QPtyCstmrBas.ptyCstmrBas;
QPtyCstmrDtl dtlEntity = QPtyCstmrDtl.ptyCstmrDtl; QPtyCstmrDtl dtlEntity = QPtyCstmrDtl.ptyCstmrDtl;
@ -287,6 +292,22 @@ public class PtyCstmrQueryRepository {
builder.and(basEntity.cstmrSno.eq(cstmrSno)); builder.and(basEntity.cstmrSno.eq(cstmrSno));
/**
* 회원 고유번호[cstmrSno] 회원의 기본 프로필정보를 데이터베이스에서 조회하는 SQL입니다.
* SELECT
* PCB.CSTMR_SNO,
* PCB.USER_ID ,
* PCB.AUTH_ID ,
* PCB.TRMNL_ID ,
* PCB.SITE_CODE ,
* PCB.USER_ID ,
* PCB.CPT_AUTH_CODE ,
* PCD.MEMBER_NAME
* FROM PTY_CSTMR_BAS PCB
* LEFT OUTER JOIN PTY_CSTMR_DTL PCD
* ON PCB.CSTMR_SNO = PCD.CSTMR_SNO
* WHERE PCB.CSTMR_SNO = #{cstmrSno}
*/
JwtProfileRsModel model = query.select(Projections.bean(JwtProfileRsModel.class, JwtProfileRsModel model = query.select(Projections.bean(JwtProfileRsModel.class,
basEntity.cstmrSno, basEntity.cstmrSno,
basEntity.userId, basEntity.userId,
@ -333,11 +354,24 @@ public class PtyCstmrQueryRepository {
} }
/**
* cstmrSno로 기본 회원정보 받아오는 SQL과
* 회원의 인가정보를 조회하여 받는 기능.
* @param cstmrSno
* @return
*/
public JwtUserModel findByIdForrefreshToken(int cstmrSno) { public JwtUserModel findByIdForrefreshToken(int cstmrSno) {
QPtyCstmrBas basEntity = QPtyCstmrBas.ptyCstmrBas; QPtyCstmrBas basEntity = QPtyCstmrBas.ptyCstmrBas;
BooleanBuilder builder = new BooleanBuilder(); BooleanBuilder builder = new BooleanBuilder();
builder.and(basEntity.cstmrSno.eq(cstmrSno)); builder.and(basEntity.cstmrSno.eq(cstmrSno));
/**
* 회원 고유번호[cstmrSno] 기본 회원정보 받아오는 SQL 기능입니다.
* SELECT
* *
* FROM PTY_CSTMR_BAS PCB
* WHERE PCB.CSTMR_SNO = #{cstmrSno}
*/
PtyCstmrBas entity = query.select(basEntity) PtyCstmrBas entity = query.select(basEntity)
.from(basEntity) .from(basEntity)
.where(builder) .where(builder)

Loading…
Cancel
Save