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

๐Ÿ”€ ์„œ๋น„์Šค ํƒˆํ‡ด ๊ธฐ๋Šฅ #12

Merged
merged 7 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
@RedisHash(value = "refreshToken", timeToLive = 60L * 60 * 24 * 7)
public class RefreshToken {

@Id
@Indexed
private String email;

@Id
@Indexed
private String token;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.gcms.v3.domain.auth.presentation.data.request.SignInRequestDto;
import com.gcms.v3.domain.auth.presentation.data.response.TokenInfoResponseDto;
import com.gcms.v3.domain.auth.service.CancelMembershipService;
import com.gcms.v3.domain.auth.service.LogoutService;
import com.gcms.v3.domain.auth.service.ReissueTokenService;
import com.gcms.v3.domain.auth.service.SignInService;
Expand All @@ -12,12 +13,13 @@

@RequiredArgsConstructor
@RestController
@RequestMapping("/v3/auth")
@RequestMapping("/auth")
public class AuthController {

private final SignInService signInService;
private final ReissueTokenService reissueTokenService;
private final LogoutService logoutService;
private final CancelMembershipService cancelMembershipService;

@PostMapping
public ResponseEntity<TokenInfoResponseDto> signIn (@RequestBody SignInRequestDto signInRequestDto) {
Expand All @@ -32,8 +34,14 @@ public ResponseEntity<TokenInfoResponseDto> reissueToken (@RequestBody String re
}

@DeleteMapping("/logout")
public ResponseEntity<Void> logout(HttpServletRequest request) {
public ResponseEntity<Void> logout (HttpServletRequest request) {
logoutService.execute(request);
return ResponseEntity.noContent().build();
}

@DeleteMapping
public ResponseEntity<Void> cancelMembership () {
cancelMembershipService.execute();
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.gcms.v3.domain.auth.service;

public interface CancelMembershipService {
void execute();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.gcms.v3.domain.auth.service.impl;

import com.gcms.v3.domain.auth.domain.repository.RefreshTokenRepository;
import com.gcms.v3.domain.auth.service.CancelMembershipService;
import com.gcms.v3.domain.user.domain.entity.User;
import com.gcms.v3.domain.user.domain.repository.UserRepository;
import com.gcms.v3.domain.user.domain.repository.UserRoleRepository;
import com.gcms.v3.domain.user.util.UserUtil;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Transactional
public class CancelMembershipServiceImpl implements CancelMembershipService {

private final UserUtil userUtil;
private final UserRepository userRepository;
private final UserRoleRepository userRoleRepository;
private final RefreshTokenRepository refreshTokenRepository;

public void execute() {
User user = userUtil.getCurrentUser();

refreshTokenRepository.deleteById(user.getEmail());

userRoleRepository.deleteByUser(user);
userRepository.deleteById(user.getId());
Comment on lines +28 to +29
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ ˆ๋””์Šค์— ์ €์žฅ๋˜๋Š” ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ๋„ ํ•จ๊ป˜ ์‚ญ์ œํ•œ๋‹ค๋ฉด ๋” ๊น”๋”ํ•œ ํƒˆํ‡ด ์ฒ˜๋ฆฌ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™๋„ค์š”

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.springframework.stereotype.Service;

@Service
@Transactional(rollbackOn = Exception.class)
KimTaeO marked this conversation as resolved.
Show resolved Hide resolved
@RequiredArgsConstructor
public class SignInServiceImpl implements SignInService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@

public interface UserRoleRepository extends JpaRepository<UserRole, byte[]> {
List<UserRole> findByUser(User user);

void deleteByUser(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti

.authorizeHttpRequests((authorizeRequests) ->
authorizeRequests
.requestMatchers(HttpMethod.POST, "/v3/auth").permitAll()
.requestMatchers(HttpMethod.POST, "/v3/auth/reissueToken").authenticated()
.requestMatchers(HttpMethod.DELETE, "/v3/auth/logout").authenticated()
.requestMatchers(HttpMethod.POST, "/auth").permitAll()
.requestMatchers(HttpMethod.POST, "/auth/reissueToken").authenticated()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ์„ ์žฌ๋ฐœ๊ธ‰ํ•˜๋ ค๋ฉด ์ ‘๊ทผ ๊ถŒํ•œ์ด permitAll์ด์–ด์•ผ ํ•˜์ง€ ์•Š์„๊นŒ ์‹ถ์–ด์š”

.requestMatchers(HttpMethod.DELETE, "/auth/logout").authenticated()
.requestMatchers(HttpMethod.DELETE, "/auth").authenticated()
)

.addFilterBefore(new ExceptionFilter(objectMapper), UsernamePasswordAuthenticationFilter.class)
Expand Down
Loading