kcscDev/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/auth/EgovLoginApiController.java

130 lines
4.2 KiB
Java
Raw Normal View History

2023-11-21 09:01:22 +00:00
package com.dbnt.kcscbackend.auth;
import com.dbnt.kcscbackend.auth.service.EgovLoginService;
import com.dbnt.kcscbackend.config.common.BaseController;
import com.dbnt.kcscbackend.auth.entity.LoginVO;
import com.dbnt.kcscbackend.config.common.ResponseCode;
import com.dbnt.kcscbackend.config.common.ResultVO;
import com.dbnt.kcscbackend.config.egov.EgovMessageSource;
import com.dbnt.kcscbackend.config.jwt.EgovJwtTokenUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
2023-11-21 09:01:22 +00:00
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
2023-11-21 09:01:22 +00:00
import java.util.HashMap;
import java.util.List;
2023-11-21 09:01:22 +00:00
/**
*
* @author
* @since 2009.03.06
* @version 1.0
* @see
*
* <pre>
* << (Modification Information) >>
*
*
* ------- -------- ---------------------------
* 2009.03.06
* 2011.08.31 JJY 릿
*
* </pre>
*/
@Slf4j
@RestController
@RequestMapping("/auth")
@Tag(name="EgovLoginApiController",description = "로그인 관련")
public class EgovLoginApiController extends BaseController {
/** EgovLoginService */
@Resource(name = "loginService")
2023-11-21 09:01:22 +00:00
private EgovLoginService loginService;
/** EgovMessageSource */
@Resource(name = "egovMessageSource")
EgovMessageSource egovMessageSource;
/** JWT */
@Autowired
private EgovJwtTokenUtil jwtTokenUtil;
@Operation(
summary = "JWT 로그인",
description = "JWT 로그인 처리",
tags = {"EgovLoginApiController"}
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "가입 성공"),
@ApiResponse(responseCode = "300", description = "가입 실패")
2023-11-21 09:01:22 +00:00
})
@PostMapping(value = "/join")
public HashMap<String, Object> actionJoin(@RequestBody @Valid LoginVO loginVO, Errors errors, HttpServletRequest request) throws Exception {
2023-11-21 09:01:22 +00:00
HashMap<String, Object> resultMap = new HashMap<String, Object>();
if(errors.hasErrors()){
StringBuilder msg = new StringBuilder();
for(FieldError error: errors.getFieldErrors()){
msg.append(error.getDefaultMessage());
msg.append("\n");
}
2023-11-21 09:01:22 +00:00
resultMap.put("resultCode", "300");
resultMap.put("resultMessage", msg.toString());
}else if(!loginVO.getPassword().equals(loginVO.getPasswordChk())){
2023-11-21 09:01:22 +00:00
resultMap.put("resultCode", "300");
resultMap.put("resultMessage", "비밀번호 확인이 잘못 입력되었습니다.");
}else{
Integer insertResult = loginService.insertUser(loginVO);
if(insertResult!=null){
resultMap.put("resultCode", "200");
resultMap.put("resultMessage", "저장 되었습니다.");
}else{
resultMap.put("resultCode", "300");
resultMap.put("resultMessage", "저장에 실패하였습니다.");
}
2023-11-21 09:01:22 +00:00
}
return resultMap;
}
/**
* .
* @return resultVO
* @exception Exception
*/
@Operation(
summary = "로그아웃",
description = "로그아웃 처리(JWT,일반 관계 없이)",
tags = {"EgovLoginApiController"}
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "로그아웃 성공"),
})
@GetMapping(value = "/logout")
public ResultVO actionLogoutJSON(HttpServletRequest request, HttpServletResponse response) throws Exception {
ResultVO resultVO = new ResultVO();
new SecurityContextLogoutHandler().logout(request, response, null);
resultVO.setResultCode(ResponseCode.SUCCESS.getCode());
resultVO.setResultMessage(ResponseCode.SUCCESS.getMessage());
return resultVO;
}
}