297 lines
8.9 KiB
Java
297 lines
8.9 KiB
Java
/************************************************************
|
|
*
|
|
* @packageName : io.company.localhost.controller.common
|
|
* @fileName : UserController.java
|
|
* @author : 조인제
|
|
* @date : 24.12.06
|
|
* @description :
|
|
*
|
|
* ===========================================================
|
|
* DATE AUTHOR NOTE
|
|
* -----------------------------------------------------------
|
|
* 24.12.06 조인제 최초 생성
|
|
* 24.01.17 박지윤 Register 합침
|
|
*
|
|
*************************************************************/
|
|
package io.company.localhost.controller.common;
|
|
|
|
import io.company.localhost.common.annotation.Admin;
|
|
import io.company.localhost.common.annotation.Guest;
|
|
import io.company.localhost.common.annotation.Member;
|
|
import io.company.localhost.common.annotation.ParameterCheck;
|
|
import io.company.localhost.common.annotation.ReqMap;
|
|
import io.company.localhost.common.dto.ApiResponse;
|
|
import io.company.localhost.common.dto.MapDto;
|
|
import io.company.localhost.service.NetmemberService;
|
|
import io.company.localhost.service.commoncodService;
|
|
import io.company.localhost.utils.AuthUtil;
|
|
import io.company.localhost.utils.SessionListener;
|
|
import io.company.localhost.vo.MemberVo;
|
|
import jakarta.servlet.http.Cookie;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import jakarta.servlet.http.HttpSession;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.security.authentication.RememberMeAuthenticationToken;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
import org.springframework.security.core.context.SecurityContext;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.security.core.context.SecurityContextHolderStrategy;
|
|
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PatchMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import static org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY;
|
|
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("/api/user")
|
|
@RequiredArgsConstructor
|
|
public class UserController {
|
|
|
|
private final commoncodService commoncodservice;
|
|
private final NetmemberService netmemberservice;
|
|
|
|
/**
|
|
* 사용 가능 색상 조회
|
|
*
|
|
* @return ApiResponse<List<MapDto>>
|
|
*
|
|
*/
|
|
@ParameterCheck
|
|
@GetMapping("/color")
|
|
public ApiResponse<List<MapDto>> selectColorList(String type) {
|
|
List<MapDto> ColorList = commoncodservice.selectColorList(type);
|
|
return ApiResponse.ok(ColorList);
|
|
}
|
|
|
|
/**
|
|
* MBTI 목록 조회
|
|
*
|
|
* @return ApiResponse<List<MapDto>>
|
|
*
|
|
*/
|
|
@ParameterCheck
|
|
@GetMapping("/mbti")
|
|
public ApiResponse<List<MapDto>> selectMbtiList() {
|
|
List<MapDto> MbtiList = commoncodservice.selectMbtiList();
|
|
return ApiResponse.ok(MbtiList);
|
|
}
|
|
|
|
/**
|
|
* 비밀번호 힌트 목록 조회
|
|
*
|
|
* @return ApiResponse<List<MapDto>>
|
|
*
|
|
*/
|
|
@ParameterCheck
|
|
@GetMapping("/pwhint")
|
|
public ApiResponse<List<MapDto>> selectPwhintList() {
|
|
List<MapDto> PwhintList = commoncodservice.selectPwhintList();
|
|
return ApiResponse.ok(PwhintList);
|
|
}
|
|
|
|
/**
|
|
* 회원가입
|
|
*
|
|
* @param profile
|
|
* @param map
|
|
* @return ApiResponse<Integer>
|
|
*/
|
|
@PostMapping("/join")
|
|
public ApiResponse<Integer> register(@RequestParam("memberPrf") MultipartFile memberPrf, @ReqMap MapDto map) {
|
|
int member = netmemberservice.register(memberPrf, map);
|
|
return ApiResponse.ok(member);
|
|
}
|
|
|
|
/**
|
|
* 아이디 중복 체크
|
|
*
|
|
* @param memberIds
|
|
* @return ApiResponse<Boolean>
|
|
*
|
|
*/
|
|
@GetMapping("/checkId")
|
|
public ApiResponse<Boolean> selectCheckId(@RequestParam String memberIds) {
|
|
boolean isDuplicate = netmemberservice.selectCheckId(memberIds);
|
|
return ApiResponse.ok(!isDuplicate);
|
|
}
|
|
|
|
/**
|
|
* 로그인 여부 체크
|
|
*
|
|
* @return ApiResponse<Boolean>
|
|
*/
|
|
@GetMapping("/isLogin")
|
|
public ApiResponse<Boolean> checkLogin() {
|
|
boolean isLoggedIn = AuthUtil.isLoggedIn();
|
|
return ApiResponse.ok(isLoggedIn);
|
|
}
|
|
|
|
/**
|
|
* 비밀번호 재설정 member 체크
|
|
*
|
|
* @param map
|
|
* @return ApiResponse<Boolean>
|
|
*
|
|
*/
|
|
@PostMapping("/pwReset")
|
|
public ApiResponse<Boolean> selectPwReset(@ReqMap MapDto map) {
|
|
boolean isPwReset = netmemberservice.selectPwReset(map);
|
|
return ApiResponse.ok(isPwReset);
|
|
}
|
|
|
|
/**
|
|
* 기존 비밀번호 체크
|
|
*
|
|
* @param map
|
|
* @return ApiResponse<Boolean>
|
|
*/
|
|
@PostMapping("/checkPassword")
|
|
public ApiResponse<Boolean> selectPassword(@ReqMap MapDto map) {
|
|
boolean isNewPassword = netmemberservice.selectPassword(map);
|
|
return ApiResponse.ok(isNewPassword);
|
|
}
|
|
|
|
/**
|
|
* 비밀번호 재설정
|
|
*
|
|
* @param map
|
|
* @return ApiResponse<Boolean>
|
|
*
|
|
*/
|
|
@PatchMapping("/pwNew")
|
|
public ApiResponse<Boolean> updatePassword(@ReqMap MapDto map) {
|
|
boolean isPwNew = netmemberservice.updatePassword(map);
|
|
return ApiResponse.ok(isPwNew);
|
|
}
|
|
|
|
// security 인증 체크
|
|
@GetMapping("userInfo")
|
|
public ApiResponse<MemberVo> getUserInfo(@AuthenticationPrincipal MemberVo memberVo) {
|
|
SecurityContextHolderStrategy contextHolderStrategy = SecurityContextHolder.getContextHolderStrategy();
|
|
log.info(">> contextHolderStrategy : {}", contextHolderStrategy);
|
|
SecurityContext context = contextHolderStrategy.getContext();
|
|
log.info(">> context : {}", context);
|
|
Authentication authentication = context.getAuthentication();
|
|
log.info(">> authentication : {}", authentication);
|
|
log.info(">> memberVo : {}", memberVo);
|
|
|
|
MemberVo user = AuthUtil.getUser();
|
|
log.info(">> AuthUtil : {}", user);
|
|
|
|
return ApiResponse.ok(memberVo);
|
|
}
|
|
|
|
// 유저 세션 체크
|
|
@GetMapping(value = "check")
|
|
public ApiResponse<?> check() {
|
|
Map<String, HttpSession> sessions = SessionListener.getSessions();
|
|
Map<String, Object> sessionData = new HashMap<>();
|
|
|
|
for (Map.Entry<String, HttpSession> entry : sessions.entrySet()) {
|
|
String sessionId = entry.getKey();
|
|
HttpSession session = entry.getValue();
|
|
Object principal = session.getAttribute("SPRING_SECURITY_CONTEXT");
|
|
sessionData.put(sessionId, principal);
|
|
}
|
|
return ApiResponse.ok(sessionData);
|
|
}
|
|
|
|
// rememberMe 확인용
|
|
@GetMapping(value = "rememberCheck")
|
|
public ApiResponse<?> rememberCheck(HttpServletRequest request) {
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
boolean remember = false;
|
|
|
|
if (authentication instanceof RememberMeAuthenticationToken) {
|
|
remember = true;
|
|
}
|
|
// 쿠키 확인
|
|
Cookie[] cookies = request.getCookies();
|
|
if (cookies != null) {
|
|
for (Cookie cookie : cookies) {
|
|
if (SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY.equals(cookie.getName())) {
|
|
log.debug("Remember-Me cookie found: {}", cookie.getValue());
|
|
remember = true;
|
|
}
|
|
}
|
|
} else {
|
|
log.debug("No cookies found");
|
|
}
|
|
return ApiResponse.ok(remember);
|
|
}
|
|
|
|
// 로그아웃
|
|
@Guest
|
|
@GetMapping("/logout")
|
|
public ApiResponse<String> logout(HttpServletRequest request, HttpServletResponse response) {
|
|
String returnMessage = "Successfully logged out";
|
|
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
if (authentication != null) {
|
|
new SecurityContextLogoutHandler().logout(request, response, authentication);
|
|
|
|
// Remember-Me 쿠키 삭제
|
|
Cookie rememberMeCookie = new Cookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY, null);
|
|
rememberMeCookie.setPath("/");
|
|
rememberMeCookie.setMaxAge(0);
|
|
rememberMeCookie.setHttpOnly(true);
|
|
rememberMeCookie.setSecure(request.isSecure());
|
|
response.addCookie(rememberMeCookie);
|
|
} else {
|
|
returnMessage = "Failed to log out";
|
|
}
|
|
|
|
return ApiResponse.ok(returnMessage);
|
|
}
|
|
|
|
/**
|
|
* 사원 목록 전체 조회
|
|
*
|
|
*
|
|
*
|
|
*/
|
|
@ParameterCheck
|
|
@GetMapping("/allUserList")
|
|
public ApiResponse<MapDto> getallUserList() {
|
|
List<MapDto> allUserList = netmemberservice.getallUserList();
|
|
MemberVo user = AuthUtil.getUser();
|
|
|
|
MapDto outData = new MapDto();
|
|
outData.put("allUserList", allUserList);
|
|
outData.put("user", user);
|
|
return ApiResponse.ok(outData);
|
|
}
|
|
|
|
@Guest
|
|
@GetMapping("get1")
|
|
public ApiResponse<?> getAuthTest1() {
|
|
return ApiResponse.ok(AuthUtil.getUser());
|
|
}
|
|
|
|
@Member
|
|
@GetMapping("get2")
|
|
public ApiResponse<?> getAuthTest2() {
|
|
return ApiResponse.ok(AuthUtil.getUser());
|
|
}
|
|
|
|
@Admin
|
|
@GetMapping("get3")
|
|
public ApiResponse<?> getAuthTest3() {
|
|
return ApiResponse.ok(AuthUtil.getUser());
|
|
}
|
|
|
|
}
|