Skip to content

Commit

Permalink
Merge pull request #95 from BaeJunH0/dev
Browse files Browse the repository at this point in the history
개발 내용 반영
  • Loading branch information
Sunja-An authored Jan 2, 2025
2 parents 5d1fdfc + 4aedd84 commit 7c43397
Show file tree
Hide file tree
Showing 25 changed files with 201 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ResponseEntity<?> readAllUsers(
@PageableDefault Pageable pageable
) {
return new ResponseEntity<>(
memberService.readAllMembers(memberInfo.nickname(),pageable)
memberService.readAllMembers(memberInfo.email(),pageable)
.map(MemberResponse.Special::from),
HttpStatus.OK
);
Expand All @@ -40,8 +40,8 @@ public ResponseEntity<?> adminLogin(@RequestBody MemberRequest memberRequest){
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}

String accessToken = jwtTokenProvider.makeAccessToken(memberRequest.nickname());
String refreshToken = jwtTokenProvider.makeRefreshToken(memberRequest.nickname());
String accessToken = jwtTokenProvider.makeAccessToken(memberRequest.email());
String refreshToken = jwtTokenProvider.makeRefreshToken(memberRequest.email());
return new ResponseEntity<>(Token.of(accessToken, refreshToken), HttpStatus.OK);
}

Expand All @@ -50,7 +50,7 @@ public ResponseEntity<?> makeUserAdmin(
@AuthorizedUser MemberInfo.Default memberInfo,
@PathVariable Long userId
) {
memberService.grantingMember(memberInfo.nickname(), userId);
memberService.grantingMember(memberInfo.email(), userId);
return new ResponseEntity<>(HttpStatus.OK);
}

Expand All @@ -59,7 +59,7 @@ public ResponseEntity<?> deleteUser(
@AuthorizedUser MemberInfo.Default memberInfo,
@PathVariable Long userId
) {
memberService.deleteMember(memberInfo.nickname(), userId);
memberService.deleteMember(memberInfo.email(), userId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public ResponseEntity<?> login(@RequestBody MemberRequest memberRequest){
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}

String accessToken = jwtTokenProvider.makeAccessToken(memberRequest.nickname());
String refreshToken = jwtTokenProvider.makeRefreshToken(memberRequest.nickname());
String accessToken = jwtTokenProvider.makeAccessToken(memberRequest.email());
String refreshToken = jwtTokenProvider.makeRefreshToken(memberRequest.email());
return new ResponseEntity<>(Token.of(accessToken, refreshToken), HttpStatus.OK);
}

@PostMapping("/register")
public ResponseEntity<?> register(@RequestBody MemberRequest memberRequest){
memberService.makeNewUser(MemberCommand.from(memberRequest));

String accessToken = jwtTokenProvider.makeAccessToken(memberRequest.nickname());
String refreshToken = jwtTokenProvider.makeRefreshToken(memberRequest.nickname());
String accessToken = jwtTokenProvider.makeAccessToken(memberRequest.email());
String refreshToken = jwtTokenProvider.makeRefreshToken(memberRequest.email());
return new ResponseEntity<>(Token.of(accessToken, refreshToken), HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class JwtTokenProvider {
/*
* 액세스 토큰 생성 : Claim => nickname, "access"
*/
public String makeAccessToken(String nickname) {
public String makeAccessToken(String email) {
long nowMillis = System.currentTimeMillis();
Date now = new Date(System.currentTimeMillis());

return Jwts.builder()
.claim("nickname", nickname)
.claim("email", email)
.claim("type", "access")
.issuedAt(now)
.expiration(new Date(nowMillis + validTime)) // 1시간
Expand All @@ -35,12 +35,12 @@ public String makeAccessToken(String nickname) {
/*
* 리프레시 토큰 생성 : Claim => nickname, "refresh"
*/
public String makeRefreshToken(String nickname) {
public String makeRefreshToken(String email) {
long nowMillis = System.currentTimeMillis();
Date now = new Date(System.currentTimeMillis());

return Jwts.builder()
.claim("nickname", nickname)
.claim("email", email)
.claim("type", "refresh")
.issuedAt(now)
.expiration(new Date(nowMillis + validTime * 168)) // 7일
Expand All @@ -51,14 +51,14 @@ public String makeRefreshToken(String nickname) {
/*
* 토큰에서 클레임 ( nickname ) 추출
*/
public String getNicknameFromToken(String token) {
public String getEmailFromToken(String token) {
try {
Claims claims = Jwts.parser()
.verifyWith(Keys.hmacShaKeyFor(secretKey.getBytes()))
.build()
.parseSignedClaims(token)
.getPayload();
return claims.get("nickname", String.class);
return claims.get("email", String.class);
} catch(Exception e){
return "null";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
public enum MemberErrorCode implements ErrorCode{
NOT_FOUND(HttpStatus.NOT_FOUND, "M000", "member finding fail"),
DUPLICATED_NICKNAME(HttpStatus.BAD_REQUEST, "M001", "already exist nickname"),
NO_AUTHENTICATION(HttpStatus.FORBIDDEN, "M002", "you're not admin member");
DUPLICATED_EMAIL(HttpStatus.BAD_REQUEST, "M002", "already exist email"),
NO_AUTHENTICATION(HttpStatus.FORBIDDEN, "M003", "you're not admin member");

private final HttpStatus httpStatus;
private final String errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.sparksInTheStep.webBoard.global.errorHandling.CustomException;
import com.sparksInTheStep.webBoard.global.errorHandling.errorCode.AuthErrorCode;
import com.sparksInTheStep.webBoard.global.errorHandling.errorCode.MemberErrorCode;
import com.sparksInTheStep.webBoard.member.application.dto.MemberInfo;
import com.sparksInTheStep.webBoard.member.persistent.MemberRepository;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.core.MethodParameter;
Expand All @@ -21,7 +23,7 @@
@Component
@RequiredArgsConstructor
public class MemberArgumentResolver implements HandlerMethodArgumentResolver {
private final MemberService memberService;
private final MemberRepository memberRepository;
private final JwtTokenProvider jwtTokenProvider;

@Override
Expand All @@ -45,16 +47,16 @@ public Object resolveArgument(
}

token = token.substring(7); // "Bearer " 부분을 제거
String nickname = jwtTokenProvider.getNicknameFromToken(token);
if(!memberService.isExistMember(nickname)) {
String email = jwtTokenProvider.getEmailFromToken(token);
if(!memberRepository.existsByEmail(email)) {
throw CustomException.of(MemberErrorCode.NOT_FOUND);
}

String type = jwtTokenProvider.getTypeFromToken(token);
if(type.equals("refresh")) {
HttpServletResponse response = (HttpServletResponse) webRequest.getNativeResponse();

String newAccessToken = jwtTokenProvider.makeAccessToken(nickname);
String newAccessToken = jwtTokenProvider.makeAccessToken(email);
Objects.requireNonNull(response).setContentType("application/json");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().write("{\"accessToken\": \"" + newAccessToken + "\"}");
Expand All @@ -63,6 +65,6 @@ public Object resolveArgument(
mavContainer.setRequestHandled(true);
}

return memberService.loginMember(nickname);
return MemberInfo.Default.from(memberRepository.findByEmail(email));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,102 +17,85 @@
public class MemberService {
private final MemberRepository memberRepository;

// 일반 사용자용
// 로그인
@Transactional(readOnly = true)
public boolean memberCheck(MemberCommand memberCommand){
if(isExistMember(memberCommand.nickname())){
Member savedMember = memberRepository.findByNickname(memberCommand.nickname());
Member checkMember = Member.of(
memberCommand.nickname(), memberCommand.password(), memberCommand.employed()
);

// 아이디 ( 이메일 ) 검증
if(memberRepository.existsByEmail(memberCommand.email())){
// 비밀번호 검증
Member savedMember = memberRepository.findByEmail(memberCommand.email());
Member checkMember = Member.of(memberCommand);
return savedMember.passCheck(checkMember.getPassword());
}
return false;
}

// 회원가입
@Transactional
public void makeNewUser(MemberCommand memberCommand){
if(isExistMember(memberCommand.nickname())){
// 이메일 중복 검사
if(memberRepository.existsByEmail(memberCommand.email())) {
throw CustomException.of(MemberErrorCode.DUPLICATED_EMAIL);
}
// 닉네임 중복 검사
if(memberRepository.existsByNickname(memberCommand.nickname())) {
throw CustomException.of(MemberErrorCode.DUPLICATED_NICKNAME);
}

memberRepository.save(Member.of(
memberCommand.nickname(), memberCommand.password(), memberCommand.employed()
));
memberRepository.save(Member.of(memberCommand));
}

// 회원 정보 수정
@Transactional
public void updateEmployed(String nickname) {
if(!isExistMember(nickname)){
throw CustomException.of(MemberErrorCode.NOT_FOUND);
}

Member member = memberRepository.findByNickname(nickname);
member.employing();
public void updateMember(String email, MemberCommand memberCommand) {
Member member = memberRepository.findByEmail(email);

member.update(
memberCommand.nickname(),
memberCommand.password(),
memberCommand.employed(),
memberCommand.gitLink(),
memberCommand.resumeLink()
);
}

// 관리자용
// 관리자 로그인
@Transactional(readOnly = true)
public boolean adminCheck(MemberCommand memberCommand){
if(!isNotAdminMember(memberCommand.nickname())){
Member savedMember = memberRepository.findByNickname(memberCommand.nickname());
Member checkMember = Member.of(
memberCommand.nickname(), memberCommand.password(), memberCommand.employed()
);

return savedMember.passCheck(checkMember.getPassword());
}
return false;
return memberCheck(memberCommand) && isAdminMember(memberCommand.email());
}

@Transactional(readOnly = true)
public Page<MemberInfo.Special> readAllMembers(String nickname, Pageable pageable){
if(isNotAdminMember(nickname)){
throw CustomException.of(MemberErrorCode.NO_AUTHENTICATION);
public Page<MemberInfo.Special> readAllMembers(String email, Pageable pageable){
if(isAdminMember(email)){
return memberRepository.findAll(pageable).map(MemberInfo.Special::from);
}

return memberRepository.findAll(pageable).map(MemberInfo.Special::from);
throw CustomException.of(MemberErrorCode.NO_AUTHENTICATION);
}

@Transactional
public void grantingMember(String adminNickname, Long memberId) {
if(isNotAdminMember(adminNickname)){
throw CustomException.of(MemberErrorCode.NO_AUTHENTICATION);
public void grantingMember(String email, Long memberId) {
if(isAdminMember(email)){
Member savedMember = memberRepository.findById(memberId).orElseThrow(
() -> CustomException.of(MemberErrorCode.NOT_FOUND)
);
savedMember.granting();
}

Member savedMember = memberRepository.findById(memberId).orElseThrow(
() -> CustomException.of(MemberErrorCode.NOT_FOUND)
);
savedMember.granting();
throw CustomException.of(MemberErrorCode.NO_AUTHENTICATION);
}

@Transactional
public void deleteMember(String adminNickname, Long memberId) {
if(isNotAdminMember(adminNickname)){
throw CustomException.of(MemberErrorCode.NO_AUTHENTICATION);
public void deleteMember(String email, Long memberId) {
if(isAdminMember(email)){
memberRepository.deleteById(memberId);
}

memberRepository.deleteById(memberId);
}

// 필터 사용
@Transactional(readOnly = true)
public MemberInfo.Default loginMember(String nickname){
return MemberInfo.Default.from(memberRepository.findByNickname(nickname));
throw CustomException.of(MemberErrorCode.NO_AUTHENTICATION);
}

@Transactional(readOnly = true)
public boolean isExistMember(String nickname){
return memberRepository.existsByNickname(nickname);
}

@Transactional(readOnly = true)
public boolean isNotAdminMember(String nickname){
if(!isExistMember(nickname)){
throw CustomException.of(MemberErrorCode.NOT_FOUND);
}

return !memberRepository.findByNickname(nickname).adminCheck();
public boolean isAdminMember(String email){
return memberRepository.findByEmail(email).adminCheck();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@

import com.sparksInTheStep.webBoard.member.presentation.dto.MemberRequest;

public record MemberCommand(String nickname, String password, Boolean employed) {
public record MemberCommand(
String email,
String nickname,
String password,
Boolean employed,
String gitLink,
String resumeLink) {
public static MemberCommand from(MemberRequest memberRequest){
return new MemberCommand(
memberRequest.nickname(), memberRequest.password(), memberRequest.employed()
memberRequest.email(),
memberRequest.nickname(),
memberRequest.password(),
memberRequest.employed(),
memberRequest.gitLink(),
memberRequest.resumeLink()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
import com.sparksInTheStep.webBoard.member.domain.Member;

public record MemberInfo() {
public record Default(String nickname, Boolean employed){
public record Default(
String email, String nickname, Boolean employed, String gitLink, String resumeLink
){
public static MemberInfo.Default from(Member member){
return new MemberInfo.Default(member.getNickname(), member.isEmployed());
return new MemberInfo.Default(
member.getEmail(),
member.getNickname(),
member.isEmployed(),
member.getGitLink(),
member.getResumeLink()
);
}
}

public record Special(String nickname, Long id, boolean isAdmin){
public record Special(String email, String nickname, Long id, boolean isAdmin){
public static MemberInfo.Special from(Member member){
return new MemberInfo.Special(member.getNickname(), member.getId(), member.isAdmin());
return new MemberInfo.Special(
member.getEmail(), member.getNickname(), member.getId(), member.isAdmin());
}
}
}
Loading

0 comments on commit 7c43397

Please sign in to comment.