362 lines
11 KiB
Java
362 lines
11 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 static org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY;
|
|
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
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.RequestPart;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
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.FileService;
|
|
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;
|
|
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("/api/user")
|
|
@RequiredArgsConstructor
|
|
public class UserController {
|
|
|
|
private final commoncodService commoncodservice;
|
|
private final NetmemberService netmemberservice;
|
|
|
|
/**
|
|
* 사용 가능 색상 조회
|
|
*
|
|
* @return
|
|
*
|
|
*/
|
|
@ParameterCheck
|
|
@GetMapping("/color")
|
|
public ApiResponse<List<MapDto>> selectColorList(String type) {
|
|
List<MapDto> ColorList = commoncodservice.selectColorList(type);
|
|
return ApiResponse.ok(ColorList);
|
|
}
|
|
|
|
/**
|
|
* 색상 중복 체크
|
|
*
|
|
* @return
|
|
*
|
|
*/
|
|
@ParameterCheck
|
|
@GetMapping("/checkColor")
|
|
public ApiResponse<Boolean> selectMemberColor(String memberCol) {
|
|
boolean isDuplicate = netmemberservice.selectMemberColor(memberCol);
|
|
return ApiResponse.ok(isDuplicate);
|
|
}
|
|
|
|
/**
|
|
* MBTI 목록 조회
|
|
*
|
|
* @return
|
|
*
|
|
*/
|
|
@ParameterCheck
|
|
@GetMapping("/mbti")
|
|
public ApiResponse<List<MapDto>> selectMbtiList() {
|
|
List<MapDto> MbtiList = commoncodservice.selectMbtiList();
|
|
return ApiResponse.ok(MbtiList);
|
|
}
|
|
|
|
/**
|
|
* 비밀번호 힌트 목록 조회
|
|
*
|
|
* @return
|
|
*
|
|
*/
|
|
@ParameterCheck
|
|
@GetMapping("/pwhint")
|
|
public ApiResponse<List<MapDto>> selectPwhintList() {
|
|
List<MapDto> PwhintList = commoncodservice.selectPwhintList();
|
|
return ApiResponse.ok(PwhintList);
|
|
}
|
|
|
|
/**
|
|
* 회원가입
|
|
*
|
|
* @param profile
|
|
* @param map
|
|
* @return
|
|
*/
|
|
@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
|
|
*
|
|
*/
|
|
@GetMapping("/checkId")
|
|
public ApiResponse<Boolean> selectCheckId(String memberIds) {
|
|
boolean isDuplicate = netmemberservice.selectCheckId(memberIds);
|
|
return ApiResponse.ok(!isDuplicate);
|
|
}
|
|
|
|
/**
|
|
* 전화번호 중복 체크
|
|
*
|
|
* @param memberTel
|
|
* @return
|
|
*
|
|
*/
|
|
@GetMapping("/checkPhone")
|
|
public ApiResponse<Boolean> selectCheckPhone(String memberTel) {
|
|
boolean isDuplicate = netmemberservice.selectCheckPhone(memberTel);
|
|
return ApiResponse.ok(!isDuplicate);
|
|
}
|
|
|
|
/**
|
|
* 로그인 여부 체크
|
|
*
|
|
* @return
|
|
*/
|
|
@GetMapping("/isLogin")
|
|
public ApiResponse<Boolean> checkLogin() {
|
|
boolean isLoggedIn = AuthUtil.isLoggedIn();
|
|
return ApiResponse.ok(isLoggedIn);
|
|
}
|
|
|
|
/**
|
|
* 비밀번호 재설정 member 체크
|
|
*
|
|
* @param map
|
|
* @return
|
|
*
|
|
*/
|
|
@PostMapping("/pwReset")
|
|
public ApiResponse<Boolean> selectPwReset(@ReqMap MapDto map) {
|
|
boolean isPwReset = netmemberservice.selectPwReset(map);
|
|
return ApiResponse.ok(isPwReset);
|
|
}
|
|
|
|
/**
|
|
* 기존 비밀번호 체크
|
|
*
|
|
* @param map
|
|
* @return
|
|
*/
|
|
@PostMapping("/checkPassword")
|
|
public ApiResponse<Boolean> selectPassword(@ReqMap MapDto map) {
|
|
boolean isNewPassword = netmemberservice.selectPassword(map);
|
|
return ApiResponse.ok(isNewPassword);
|
|
}
|
|
|
|
/**
|
|
* 비밀번호 재설정
|
|
*
|
|
* @param map
|
|
* @return
|
|
*
|
|
*/
|
|
@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("userInfo")
|
|
public ApiResponse<MemberVo> getUserInfo2(@AuthenticationPrincipal MemberVo memberVo) {
|
|
Long memberId = AuthUtil.getUser().getId();
|
|
|
|
log.info("🧩 memberId from AuthUtil: {}", memberId);
|
|
|
|
MemberVo user = netmemberservice.getUserInfoById(memberId);
|
|
log.info("📦 User from DB: {}", user);
|
|
|
|
return ApiResponse.ok(user);
|
|
}
|
|
|
|
|
|
// 유저 세션 체크
|
|
@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> selectallUserList() {
|
|
List<MapDto> allUserList = netmemberservice.selectallUserList();
|
|
MemberVo user = AuthUtil.getUser();
|
|
|
|
MapDto outData = new MapDto();
|
|
outData.put("allUserList", allUserList);
|
|
outData.put("user", user);
|
|
return ApiResponse.ok(outData);
|
|
}
|
|
|
|
@PatchMapping("/updateInfo")
|
|
public ApiResponse<?> updateUserInfo(@ReqMap MapDto map,
|
|
@RequestPart(value = "profileFile", required = false) MultipartFile profileFile
|
|
) throws IOException {
|
|
Long userId = AuthUtil.getUser().getId();
|
|
map.put("memberId", userId);
|
|
netmemberservice.updateUserInfo(map, profileFile);
|
|
return ApiResponse.ok("수정 완료");
|
|
}
|
|
|
|
@PatchMapping("/updateColorYon")
|
|
public ApiResponse<Integer> updateColorYon(@ReqMap MapDto map) {
|
|
return ApiResponse.ok(commoncodservice.updateColorYon(map));
|
|
}
|
|
|
|
@PatchMapping("/updateColorChange")
|
|
public ApiResponse<Integer> updateColorChange(@ReqMap MapDto map) {
|
|
return ApiResponse.ok(commoncodservice.updateColorChange(map));
|
|
}
|
|
|
|
|
|
@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());
|
|
}
|
|
|
|
}
|