Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat-50] : 로그인 및 회원가입 구현 #58

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ dependencies {
//swagger 설정
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'

//Spring security
implementation 'org.springframework.boot:spring-boot-starter-security'

//jjwt
implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.3'


}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/icurriculum/IcurriculumApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;

@SpringBootApplication
@EnableRedisRepositories
public class IcurriculumApplication {

public static void main(String[] args) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/icurriculum/domain/common/BaseRDBEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import java.time.LocalDateTime;

import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -14,6 +16,7 @@ public abstract class BaseRDBEntity {

@CreatedDate
@Column(updatable = false)
@Getter
private LocalDateTime createdAt;

@LastModifiedDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import icurriculum.domain.department.Department;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DepartmentRepository extends JpaRepository<Department, Long> {
import java.util.Optional;

public interface DepartmentRepository extends JpaRepository<Department, Long> {
Optional<Department> findByName(String departmentName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package icurriculum.domain.member.Controller;

import icurriculum.domain.member.Member;
import icurriculum.domain.member.Service.MemberService;
import icurriculum.domain.member.dto.MemberConverter;
import icurriculum.domain.member.dto.MemberRequest;
import icurriculum.domain.member.dto.MemberResponse;
import icurriculum.global.response.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/members")
public class MemberController {

private final MemberService memberService;
private final MemberConverter memberConverter;

@PostMapping("/join")
public ApiResponse<MemberResponse.JoinResponse> join(@RequestBody @Valid MemberRequest.JoinDTO request) {
Member member = memberService.join(request);
MemberResponse.JoinResponse joinResponse = memberConverter.toJoinResponse(member);

return ApiResponse.onSuccess(joinResponse);
}
}
28 changes: 17 additions & 11 deletions src/main/java/icurriculum/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
import static lombok.AccessLevel.PROTECTED;

import icurriculum.domain.common.BaseRDBEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import icurriculum.domain.membermajor.MemberMajor;
import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import lombok.*;

import java.util.List;

@Entity
@NoArgsConstructor(access = PROTECTED)
Expand All @@ -26,6 +23,14 @@ public class Member extends BaseRDBEntity {
@Column(name = "member_id")
private Long id;

@Email
@Column(nullable = false)
private String email;

@Setter
@Column(nullable = false)
private String password;

@Column(name = "member_name", nullable = false)
private String name;

Expand All @@ -36,10 +41,11 @@ public class Member extends BaseRDBEntity {
private RoleType role;

@Builder
private Member(String name, Integer joinYear, RoleType role) {
private Member(String email, String name, Integer joinYear,RoleType roleType) {
this.email = email;
this.name = name;
this.joinYear = joinYear;
this.role = role;
this.role = roleType;
}

}
67 changes: 67 additions & 0 deletions src/main/java/icurriculum/domain/member/Service/MemberService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package icurriculum.domain.member.Service;

import icurriculum.domain.department.Department;
import icurriculum.domain.department.repository.DepartmentRepository;
import icurriculum.domain.member.Member;
import icurriculum.domain.member.dto.MemberConverter;
import icurriculum.domain.member.dto.MemberRequest;
import icurriculum.domain.member.repository.MemberRepository;
import icurriculum.domain.membermajor.MajorType;
import icurriculum.domain.membermajor.MemberMajor;
import icurriculum.domain.membermajor.MemberMajorConverter;
import icurriculum.domain.membermajor.repository.MemberMajorRepository;
import icurriculum.global.response.exception.GeneralException;
import icurriculum.global.response.status.ErrorStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class MemberService {

private final MemberRepository memberRepository;
private final DepartmentRepository departmentRepository;
private final MemberMajorRepository memberMajorRepository;
private final MemberConverter memberConverter;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
private final MemberMajorConverter memberMajorConverter;

@Value("${jwt.access-token-validity-in-seconds}")
private Long ACCESS_TOKEN_VALIDITY_IN_SECONDS;
@Value("${jwt.refresh-token-validity-in-seconds}")
private Long REFRESH_TOKEN_VALIDITY_IN_SECONDS;


@Transactional
public Member join(MemberRequest.JoinDTO request) {
Department department = departmentRepository.findByName(request.getDepartmentName()).orElseThrow(
() -> new GeneralException(ErrorStatus.DEPARTMENT_NOT_FOUND_BY_NAME)
);
Member newMember = memberConverter.toEntity(request);
newMember.setPassword(bCryptPasswordEncoder.encode(request.getPassword().toLowerCase()));
Member member = memberRepository.save(newMember);

MemberMajor memberMajor = memberMajorConverter.toEntity(department, member);
memberMajorRepository.save(memberMajor);
return member;
}

public Member findMember(Long memberId) {
return memberRepository.findById(memberId).orElseThrow(
() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND_BY_MEMBER_ID)
);
}

public Member findMemberByEmail(String email) {
return memberRepository.findByEmail(email).orElseThrow(
() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND_BY_EMAIL)
);
}

}
25 changes: 25 additions & 0 deletions src/main/java/icurriculum/domain/member/dto/MemberConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package icurriculum.domain.member.dto;

import icurriculum.domain.member.Member;
import icurriculum.domain.member.RoleType;
import org.springframework.stereotype.Component;

@Component
public class MemberConverter {

public Member toEntity(MemberRequest.JoinDTO request) {
return Member.builder()
.email(request.getEmail())
.name(request.getName())
.joinYear(request.getJoinYear())
.roleType(RoleType.ROLE_USER) //회원가입은 무조건 User로만 가능
.build();
}

public MemberResponse.JoinResponse toJoinResponse(Member member) {
return MemberResponse.JoinResponse.builder()
.email(member.getEmail())
.join_date(member.getCreatedAt())
.build();
}
}
32 changes: 32 additions & 0 deletions src/main/java/icurriculum/domain/member/dto/MemberRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package icurriculum.domain.member.dto;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;

public abstract class MemberRequest {

@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class JoinDTO{
@NotNull
@Email
private String email;

@NotNull
private String password;

@NotNull
private String name;

@NotNull
private String departmentName;

@NotNull
private Integer joinYear;
}
}
20 changes: 20 additions & 0 deletions src/main/java/icurriculum/domain/member/dto/MemberResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package icurriculum.domain.member.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

public abstract class MemberResponse {

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class JoinResponse {
String email;
LocalDateTime join_date;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import icurriculum.domain.member.Member;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberRepository extends JpaRepository<Member, Long> {
import java.util.Optional;

public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findByEmail(String email);
}
6 changes: 5 additions & 1 deletion src/main/java/icurriculum/domain/membermajor/MajorType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

public enum MajorType {

주전공;
주전공,
복수전공,
부전공,
연계전공,
융합전공;

public static MajorType to(String value) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class MemberMajor extends BaseRDBEntity {
private Member member;

@Builder
private MemberMajor(MajorType majorType, Department department, Member member) {
private MemberMajor( MajorType majorType,Department department, Member member) {
this.majorType = majorType;
this.department = department;
this.member = member;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package icurriculum.domain.membermajor;

import icurriculum.domain.department.Department;
import icurriculum.domain.member.Member;
import org.springframework.stereotype.Component;

@Component
public class MemberMajorConverter {

public MemberMajor toEntity(Department department, Member member) {
return MemberMajor.builder()
.department(department)
.member(member)
.majorType(MajorType.주전공) // 주전공만 고려하는 경우
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public enum ErrorStatus {
*/
DEPARTMENT_NAME_INVALID_DATA(HttpStatus.BAD_REQUEST, "DEPARTMENTNAME400",
"학과이름의 데이터형식이 올바르지 않습니다."),
DEPARTMENT_NOT_FOUND_BY_NAME(HttpStatus.BAD_REQUEST, "DEPARTMENT400", "해당 이름의 학과가 존재하지 않습니다."),

/*
* category
Expand Down Expand Up @@ -97,8 +98,28 @@ public enum ErrorStatus {
DESERIALIZATION_FAILURE(HttpStatus.BAD_REQUEST, "ADDITIONAL400",
"추가정보 역직렬화에 실패하였습니다."),
EMPTY_ADDITIONAL_INFO_FAILURE(HttpStatus.BAD_REQUEST, "ADDITIONAL401",
"추가정보에 key 값에 대응하는 value 값이 존재하지 않습니다.");
"추가정보에 key 값에 대응하는 value 값이 존재하지 않습니다."),

/*
* member
*/
MEMBER_NOT_FOUND_BY_MEMBER_ID(HttpStatus.BAD_REQUEST, "MEMBER4000",
"해당 id를 가진 회원이 존재하지 않습니다."),
MEMBER_NOT_FOUND_BY_EMAIL(HttpStatus.BAD_REQUEST, "MEMBER4001",
"해당 email을 가진 회원이 존재하지 않습니다."),

/*
* login용 token
*/
INVALID_TOKEN(HttpStatus.BAD_REQUEST, "JWT4000",
"유효하지 않은 토큰입니다."),
REFRESH_TOKEN_NOT_FOUND(HttpStatus.BAD_REQUEST, "JWT4001",
"해당 refresh token이 존재하지 않습니다."),
TOKEN_IS_EXPIRED(HttpStatus.BAD_REQUEST, "JWT4002",
"만료된 토큰입니다."),
AUTHENTICATION_TYPE_IS_NOT_BEARER(HttpStatus.BAD_REQUEST, "JWT4003",
"잘못된 토큰 타입입니다."),
;
private final HttpStatus httpStatus;
private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package icurriculum.global.security.annotation;

import org.springframework.security.core.annotation.AuthenticationPrincipal;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member")
public @interface LoginMember {
}
Loading