Skip to content

Commit

Permalink
[fix] logout 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
gol2580 committed Jan 23, 2024
1 parent de63595 commit 76d4cdc
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,16 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
//.addFilterBefore(new JwtExceptionFilter(),JwtFilter.class)

//로그아웃
.logout((logoutConfig) ->
/*.logout((logoutConfig) ->
logoutConfig
.logoutUrl("/auth/logout")
.logoutSuccessHandler((request, response, authentication)
-> {response.sendRedirect("/auth/log-in");})
.deleteCookies("remember-me")
)
*/


// 조건별로 요청 허용/제한 설정
.authorizeHttpRequests((authorizeRequests)-> authorizeRequests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ public Response changePassword(@RequestBody ChangePasswordDto changePasswordDto)
return success(CHANGE_PASSWORD_SUCCESS);
}

@PostMapping("/logout")
@ResponseStatus(OK)
@Operation(summary = "로그아웃")
public Response logout(@RequestHeader("Authorization") String token) {
authService.logout(token);
return success("로그아웃 성공");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import com.kimgreen.backend.exception.LogInFailureEmail;
import com.kimgreen.backend.exception.LogInFailurePassword;
import com.kimgreen.backend.exception.RefreshTokenExpired;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
Expand Down Expand Up @@ -60,6 +62,13 @@ public void signUp(SignUpRequestDto signUpRequestDto) {
saveMember(signUpRequestDto,email, password, nickname);
}

@Transactional
public void logout(String token) {
SecurityContextHolder.clearContext();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Authorization", "");
}

@Transactional
public TokenDto logIn(LogInRequestDto dto) {
String email = dto.getEmail();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.kimgreen.backend.domain.profile.repository.ProfileBadgeRepository;
import com.kimgreen.backend.domain.profile.repository.RepresentativeBadgeRepository;
import com.kimgreen.backend.exception.LogInFailurePassword;
import com.kimgreen.backend.exception.LogInRequiredException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand Down Expand Up @@ -43,7 +44,12 @@ public class MemberService {


public Member getCurrentMember() {
return memberRepository.findByEmail(SecurityContextHolder.getContext().getAuthentication().getName());

Member member = memberRepository.findByEmail(SecurityContextHolder.getContext().getAuthentication().getName());
if(member==null) {
throw new LogInRequiredException();
}
return member;
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.kimgreen.backend.exception;

public class LogInRequiredException extends RuntimeException{
}
6 changes: 6 additions & 0 deletions src/main/java/com/kimgreen/backend/response/Advice.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ public Response WrongPath() {
return Response.failure(HttpStatus.NOT_FOUND, "잘못된 경로입니다.");
}

@ExceptionHandler(LogInRequiredException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public Response LogInRequiredResponse() {
return Response.failure(HttpStatus.NOT_FOUND, "토큰이 존재하지 않습니다. 로그인 후 이용해주세요.");
}

}

0 comments on commit 76d4cdc

Please sign in to comment.