-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : OAuth2 인증 성공시 Response에 RedirectionURL과 토큰을 세팅하는 SuccessHandle…
…r 구현 (#112)
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/gdsc/binaryho/imhere/security/oauth/CustomOAuth2SuccessHandler.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,46 @@ | ||
package gdsc.binaryho.imhere.security.oauth; | ||
|
||
import gdsc.binaryho.imhere.security.SignUpProcessRedirectionPath; | ||
import gdsc.binaryho.imhere.security.jwt.Token; | ||
import gdsc.binaryho.imhere.security.jwt.TokenService; | ||
import gdsc.binaryho.imhere.util.ClientUrlUtil; | ||
import java.io.IOException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CustomOAuth2SuccessHandler extends SimpleUrlAuthenticationSuccessHandler { | ||
|
||
private static final String HEADER_STRING = HttpHeaders.AUTHORIZATION; | ||
private static final String ACCESS_TOKEN_PREFIX = "Token "; | ||
|
||
private final TokenService tokenService; | ||
private final ClientUrlUtil clientUrlUtil; | ||
|
||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, | ||
Authentication authentication) throws IOException { | ||
CustomOAuth2User customOAuth2User = (CustomOAuth2User) authentication.getPrincipal(); | ||
|
||
setRedirectUrl(request, response, customOAuth2User.getSignUpProcessRedirectionPath()); | ||
setAccessToken(response, customOAuth2User); | ||
} | ||
|
||
private void setRedirectUrl(HttpServletRequest request, HttpServletResponse response, | ||
SignUpProcessRedirectionPath signupProcessRedirectionPath) throws IOException { | ||
String redirectUrl = clientUrlUtil.getClientUrl() + signupProcessRedirectionPath.getRedirectUrlPath(); | ||
this.getRedirectStrategy().sendRedirect(request, response, redirectUrl); | ||
} | ||
|
||
private void setAccessToken(HttpServletResponse response, CustomOAuth2User oAuthUser) { | ||
Token jwtToken = tokenService.createToken(oAuthUser.getMemberId(), oAuthUser.getRole()); | ||
response.addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.AUTHORIZATION); | ||
response.addHeader(HEADER_STRING, ACCESS_TOKEN_PREFIX + jwtToken.getAccessToken()); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/gdsc/binaryho/imhere/util/ClientUrlUtil.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,29 @@ | ||
package gdsc.binaryho.imhere.util; | ||
|
||
import static gdsc.binaryho.imhere.constant.UrlConstant.LOCAL_CLIENT_URL; | ||
import static gdsc.binaryho.imhere.constant.UrlConstant.PROD_CLIENT_URL; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ClientUrlUtil { | ||
|
||
private final static String PROD = "prod"; | ||
private final Environment environment; | ||
|
||
public String getClientUrl() { | ||
if (isProd()) { | ||
return PROD_CLIENT_URL; | ||
} | ||
return LOCAL_CLIENT_URL; | ||
} | ||
|
||
private boolean isProd() { | ||
return List.of(environment.getActiveProfiles()) | ||
.contains(PROD); | ||
} | ||
} |