This repository has been archived by the owner on Jan 20, 2025. It is now read-only.
-
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.
- Loading branch information
1 parent
46ed9fd
commit e56e546
Showing
2 changed files
with
60 additions
and
4 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
55 changes: 55 additions & 0 deletions
55
...sm-web/src/main/java/team/themoment/hellogsm/web/global/security/auth/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,55 @@ | ||
package team.themoment.hellogsm.web.global.security.auth; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
import team.themoment.hellogsm.entity.domain.user.enums.Role; | ||
|
||
import java.io.IOException; | ||
|
||
@Controller | ||
@RequiredArgsConstructor | ||
@RequestMapping("/auth/v1") | ||
public class AuthController { | ||
|
||
private final AuthEnvironment authEnvironment; | ||
|
||
|
||
// spring security logout 기능 관련 이슈로 인한 임시 구현 | ||
//TODO 이슈 해결하고 spring security logout으로 대체하기 | ||
@GetMapping("/logout") | ||
public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
var auth = SecurityContextHolder.getContext().getAuthentication(); | ||
if (auth instanceof OAuth2AuthenticationToken) { | ||
new SecurityContextLogoutHandler().logout(request, response, SecurityContextHolder.getContext().getAuthentication()); | ||
} | ||
String redirectUrl = buildRedirectUrl(isAdmin(auth)); | ||
response.sendRedirect(redirectUrl); | ||
|
||
} | ||
|
||
protected final boolean isAdmin(Authentication authentication) { | ||
return authentication.getAuthorities().stream() | ||
.anyMatch(authority -> Role.ROLE_ADMIN.name().equals(authority.getAuthority())); | ||
} | ||
|
||
protected final String buildRedirectUrl(boolean isAdmin) { | ||
final String defaultTargetUrl = authEnvironment.redirectBaseUri(); | ||
final String adminUrl = authEnvironment.redirectBaseUri(); | ||
|
||
String targetUrl = isAdmin ? adminUrl : defaultTargetUrl; | ||
|
||
return UriComponentsBuilder.fromUriString(targetUrl) | ||
.queryParam("logout", "success") | ||
.build() | ||
.toUriString(); | ||
} | ||
} |