Skip to content

Commit

Permalink
예외처리 및 시큐리티 config 수정 (Fastcampus-Final-Team3#169)
Browse files Browse the repository at this point in the history
* chore: 예외처리 및 시큐리티 config 수정(Fastcampus-Final-Team3#166)
  • Loading branch information
hybiis authored Oct 19, 2023
1 parent cb4c073 commit dcc3aa8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.javajober.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand Down Expand Up @@ -42,4 +43,9 @@ public ResponseEntity<ApiResponse.InvalidResponse> handleMethodArgumentTypeMisma
log.error(e.getMessage());
return ApiResponse.invalidResponse(ApiStatus.OBJECT_EMPTY, e.getMessage());
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ApiResponse.InvalidResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
return ApiResponse.invalidResponse(ApiStatus.INVALID_DATA, e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import javax.validation.Valid;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -24,7 +23,7 @@

@RestController
@Validated
@RequestMapping("/members")
@RequestMapping("api/members")
public class MemberController {
private final MemberService memberService;
private final RefreshTokenService refreshTokenService;
Expand All @@ -36,19 +35,18 @@ public MemberController(MemberService memberService,
}

@PostMapping("/signup")
public ResponseEntity<ApiResponse.Response<MemberSignupResponse>> signup(@RequestBody @Valid MemberSignupRequest memberSignupRequest,
BindingResult bindingResult) {
public ResponseEntity<ApiResponse.Response<MemberSignupResponse>> signup(@RequestBody @Valid MemberSignupRequest memberSignupRequest) {

MemberSignupResponse data = memberService.signup(memberSignupRequest, bindingResult);
MemberSignupResponse data = memberService.signup(memberSignupRequest);

return ApiResponse.response(ApiStatus.OK, "회원가입에 성공했습니다.", data);
}

@PostMapping("/login")
public ResponseEntity<ApiResponse.Response<MemberLoginResponse>> login(@RequestBody @Valid MemberLoginRequest loginDto, BindingResult bindingResult) {
public ResponseEntity<ApiResponse.Response<MemberLoginResponse>> login(@RequestBody @Valid MemberLoginRequest loginDto) {
try {

MemberLoginResponse data = memberService.login(loginDto,bindingResult);
MemberLoginResponse data = memberService.login(loginDto);

return ApiResponse.response(ApiStatus.OK, "로그인에 성공했습니다.", data);
} catch (IllegalArgumentException e) {
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/com/javajober/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.javajober.exception.ApiStatus;
import com.javajober.exception.ApplicationException;
Expand Down Expand Up @@ -35,11 +34,7 @@ public MemberService(MemberRepository memberRepository, PasswordEncoder password
}

@Transactional
public MemberSignupResponse signup(MemberSignupRequest memberSignupRequest, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
throw new ApplicationException(ApiStatus.INVALID_DATA, "잘못된 요청입니다.");
}

public MemberSignupResponse signup(MemberSignupRequest memberSignupRequest) {
Member member = memberSignupRequest.toEntity(memberSignupRequest);
member.setPassword(passwordEncoder.encode(memberSignupRequest.getPassword()));
Member saveMember = memberRepository.save(member);
Expand All @@ -48,10 +43,7 @@ public MemberSignupResponse signup(MemberSignupRequest memberSignupRequest, Bind
}

@Transactional
public MemberLoginResponse login(MemberLoginRequest loginDto, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
throw new ApplicationException(ApiStatus.INVALID_DATA, "잘못된 요청입니다.");
}
public MemberLoginResponse login(MemberLoginRequest loginDto) {
Member member = memberRepository.findMember(loginDto.getEmail());

if (!passwordEncoder.matches(loginDto.getPassword(), member.getPassword())) {
Expand All @@ -66,4 +58,4 @@ public MemberLoginResponse login(MemberLoginRequest loginDto, BindingResult bind

return new MemberLoginResponse(member,accessToken,refreshToken);
}
}
}
1 change: 1 addition & 0 deletions src/main/java/com/javajober/security/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeRequests()
.antMatchers("/members/login").permitAll()
.antMatchers("/members/signup").permitAll()
.antMatchers("/healthCheck").permitAll()
.anyRequest().authenticated()
.and()
.exceptionHandling()
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring:
profiles:
active: dev,secret,jwt
active: prod,secret,jwt

0 comments on commit dcc3aa8

Please sign in to comment.