-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from survey-mate/feat/25
feat: 회원가입, 로그인 일부 구현 #25
- Loading branch information
Showing
36 changed files
with
684 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/java/uk/jinhy/survey_mate_api/auth/application/dto/AuthServiceDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package uk.jinhy.survey_mate_api.auth.application.dto; | ||
|
||
public class AuthServiceDTO { | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/uk/jinhy/survey_mate_api/auth/application/service/AuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package uk.jinhy.survey_mate_api.auth.application.service; | ||
|
||
import com.amazonaws.services.kms.model.NotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
import uk.jinhy.survey_mate_api.auth.domain.entity.Member; | ||
import uk.jinhy.survey_mate_api.auth.domain.repository.MemberRepository; | ||
import uk.jinhy.survey_mate_api.auth.presentation.dto.LoginControllerDTO; | ||
import uk.jinhy.survey_mate_api.auth.presentation.dto.MemberControllerDTO; | ||
import uk.jinhy.survey_mate_api.common.auth.AuthProvider; | ||
import uk.jinhy.survey_mate_api.jwt.JwtTokenProvider; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@Slf4j | ||
public class AuthService { | ||
|
||
@Qualifier("AuthenticationManager") | ||
private final AuthenticationManager authenticationManager; | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
private final PasswordEncoder passwordEncoder; | ||
|
||
public Member join(MemberControllerDTO.MemberRequestDTO requestDTO){ | ||
Member member = Member.builder() | ||
.memberId(requestDTO.getMemberId()) | ||
.nickname(requestDTO.getNickname()) | ||
.password(passwordEncoder.encode(requestDTO.getPassword())) | ||
.messageConsent(requestDTO.isMessageConsent()) | ||
.marketingConsent(requestDTO.isMarketingConsent()) | ||
.point(0L) | ||
.profileUrl(null) | ||
.build(); | ||
|
||
memberRepository.save(member); | ||
return member; | ||
} | ||
|
||
public String login(LoginControllerDTO requestDTO){ | ||
String id = requestDTO.getId(); | ||
String password = requestDTO.getPassword(); | ||
|
||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(id, password); | ||
|
||
Authentication authentication = authenticationManager.authenticate(authenticationToken); | ||
String jwtToken = jwtTokenProvider.createToken(authentication); | ||
|
||
return jwtToken; | ||
|
||
} | ||
|
||
public Member getCurrentMember() { | ||
String memberId = AuthProvider.getAuthenticationInfoMemberId(); | ||
return memberRepository.findById(memberId) | ||
.orElseThrow(() -> new NotFoundException("해당 유저가 없습니다.")); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/uk/jinhy/survey_mate_api/auth/domain/repository/MemberRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package uk.jinhy.survey_mate_api.auth.domain.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import uk.jinhy.survey_mate_api.auth.domain.entity.Member; | ||
|
||
public interface MemberRepository extends JpaRepository<Member, String> { | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/uk/jinhy/survey_mate_api/auth/presentation/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package uk.jinhy.survey_mate_api.auth.presentation; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import uk.jinhy.survey_mate_api.auth.application.service.AuthService; | ||
import uk.jinhy.survey_mate_api.auth.domain.entity.Member; | ||
import uk.jinhy.survey_mate_api.auth.presentation.dto.LoginControllerDTO; | ||
import uk.jinhy.survey_mate_api.auth.presentation.dto.MemberControllerDTO; | ||
import uk.jinhy.survey_mate_api.common.response.ApiResponse; | ||
import uk.jinhy.survey_mate_api.common.response.Status; | ||
|
||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/auth") | ||
public class AuthController { | ||
private final AuthService authService; | ||
|
||
@PostMapping("/join") | ||
@Operation(summary = "회원가입") | ||
public ApiResponse<?> join(MemberControllerDTO.MemberRequestDTO requestDTO){ | ||
Member member = authService.join(requestDTO); | ||
return ApiResponse.onSuccess(Status.CREATED.getHttpStatus().toString(), | ||
Status.CREATED.getMessage(), member.getMemberId()); | ||
} | ||
|
||
@PostMapping("/login") | ||
@Operation(summary = "로그인") | ||
public ApiResponse<?> login(LoginControllerDTO requestDTO){ | ||
String jwtTokenInfo = authService.login(requestDTO); | ||
return ApiResponse.onSuccess(Status.OK.getHttpStatus().toString(), | ||
Status.OK.getMessage(), jwtTokenInfo); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/uk/jinhy/survey_mate_api/auth/presentation/dto/LoginControllerDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package uk.jinhy.survey_mate_api.auth.presentation.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Builder | ||
@Getter | ||
public class LoginControllerDTO { | ||
private String id; | ||
|
||
private String password; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/uk/jinhy/survey_mate_api/auth/presentation/dto/MemberControllerDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package uk.jinhy.survey_mate_api.auth.presentation.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
public class MemberControllerDTO { | ||
@Builder | ||
@Getter | ||
public static class MemberRequestDTO{ | ||
private String memberId; | ||
|
||
private String nickname; | ||
|
||
private String password; | ||
|
||
private boolean messageConsent; | ||
|
||
private boolean marketingConsent; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/uk/jinhy/survey_mate_api/common/auth/AuthProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package uk.jinhy.survey_mate_api.common.auth; | ||
|
||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.User; | ||
|
||
public class AuthProvider { | ||
public static User getAuthenticationInfo() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
if (authentication == null || !authentication.isAuthenticated()) { | ||
throw new RuntimeException("인증정보가 없습니다."); | ||
} | ||
return (User) authentication.getPrincipal(); | ||
} | ||
|
||
public static String getAuthenticationInfoMemberId() { | ||
return getAuthenticationInfo().getUsername(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 17 additions & 2 deletions
19
src/main/java/uk/jinhy/survey_mate_api/common/config/SwaggerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,37 @@ | ||
package uk.jinhy.survey_mate_api.common.config; | ||
|
||
import io.swagger.v3.oas.models.Components; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.info.Info; | ||
import io.swagger.v3.oas.models.security.SecurityRequirement; | ||
import io.swagger.v3.oas.models.security.SecurityScheme; | ||
import io.swagger.v3.oas.models.servers.Server; | ||
import java.util.Arrays; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
|
||
@Configuration | ||
public class SwaggerConfig { | ||
@Bean | ||
public OpenAPI SpringBootAPI() { | ||
|
||
Info info = new Info() | ||
.title("썰매 API 문서") | ||
.description("썰매 스프링 부트 서버 API 문저") | ||
.description("썰매 스프링 부트 서버 API 문서") | ||
.version("1.0.0"); | ||
|
||
SecurityScheme securityScheme = new SecurityScheme() | ||
.type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT") | ||
.in(SecurityScheme.In.HEADER).name("Authorization"); | ||
SecurityRequirement securityRequirement = new SecurityRequirement().addList("bearerAuth"); | ||
|
||
return new OpenAPI() | ||
.addServersItem(new Server().url("/")) | ||
.info(info); | ||
.info(info) | ||
.components(new Components().addSecuritySchemes("bearerAuth", securityScheme)) | ||
.security(Arrays.asList(securityRequirement)); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.