-
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.
Merge pull request #71 from Nexters/feature/oauth
OAuth 추가
- Loading branch information
Showing
18 changed files
with
456 additions
and
54 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
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
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
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
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
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,5 @@ | ||
package com.sirius.spurt.common.meta; | ||
|
||
public enum Social { | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/sirius/spurt/common/oauth/handler/OAuth2AuthenticationFailHandler.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,47 @@ | ||
package com.sirius.spurt.common.oauth.handler; | ||
|
||
import static com.sirius.spurt.common.meta.ResultCode.AUTHENTICATION_FAILED; | ||
import static jakarta.servlet.http.HttpServletResponse.SC_OK; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.sirius.spurt.service.controller.RestResponse; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.authentication.AuthenticationFailureHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class OAuth2AuthenticationFailHandler implements AuthenticationFailureHandler { | ||
private final ObjectMapper objectMapper; | ||
|
||
@Override | ||
public void onAuthenticationFailure( | ||
HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) | ||
throws IOException, ServletException { | ||
setErrorResponse(response, RestResponse.error(AUTHENTICATION_FAILED)); | ||
} | ||
|
||
private void setErrorResponse(HttpServletResponse response, RestResponse<?> res) | ||
throws IOException { | ||
response.setCharacterEncoding(UTF_8.name()); | ||
response.setContentType(APPLICATION_JSON_VALUE); | ||
response.setStatus(SC_OK); | ||
|
||
try { | ||
response.getWriter().write(objectMapper.writeValueAsString(res)); | ||
} catch (Exception e) { | ||
log.error(e.getMessage()); | ||
} finally { | ||
response.getWriter().close(); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/sirius/spurt/common/oauth/handler/OAuth2AuthenticationSuccessHandler.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,54 @@ | ||
package com.sirius.spurt.common.oauth.handler; | ||
|
||
import com.sirius.spurt.common.auth.PrincipalDetails; | ||
import com.sirius.spurt.common.jwt.JwtUtils; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class OAuth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { | ||
private final JwtUtils jwtUtils; | ||
|
||
private final String IS_COMMITTED = "Response has already committed."; | ||
|
||
@Value("${redirect-uri.prod}") | ||
private String redirectUri; | ||
|
||
@Override | ||
public void onAuthenticationSuccess( | ||
HttpServletRequest request, HttpServletResponse response, Authentication authentication) | ||
throws IOException { | ||
String targetUrl = determineTargetUrl(request, response, authentication); | ||
if (response.isCommitted()) { | ||
log.debug(IS_COMMITTED + targetUrl); | ||
return; | ||
} | ||
|
||
getRedirectStrategy().sendRedirect(request, response, targetUrl); | ||
} | ||
|
||
@Override | ||
protected String determineTargetUrl( | ||
HttpServletRequest request, HttpServletResponse response, Authentication authentication) { | ||
PrincipalDetails principalDetails = (PrincipalDetails) authentication.getPrincipal(); | ||
jwtUtils.setAccessToken( | ||
response, | ||
principalDetails.getUserEntity().getUserId(), | ||
principalDetails.getUserEntity().getEmail()); | ||
jwtUtils.setRefreshToken( | ||
response, | ||
principalDetails.getUserEntity().getUserId(), | ||
principalDetails.getUserEntity().getEmail()); | ||
return UriComponentsBuilder.fromUriString(redirectUri).build().toString(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/sirius/spurt/common/oauth/info/OAuth2UserInfo.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,18 @@ | ||
package com.sirius.spurt.common.oauth.info; | ||
|
||
import java.util.Map; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public abstract class OAuth2UserInfo { | ||
protected Map<String, Object> attributes; | ||
|
||
public abstract String getUserId(); | ||
|
||
public abstract String getEmail(); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/sirius/spurt/common/oauth/info/OAuth2UserInfoFactory.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,19 @@ | ||
package com.sirius.spurt.common.oauth.info; | ||
|
||
import static com.sirius.spurt.common.meta.ResultCode.UNKNOWN_SOCIAL; | ||
import static com.sirius.spurt.common.meta.Social.GOOGLE; | ||
|
||
import com.sirius.spurt.common.exception.GlobalException; | ||
import com.sirius.spurt.common.meta.Social; | ||
import com.sirius.spurt.common.oauth.info.impl.GoogleOAuth2UserInfo; | ||
import java.util.Map; | ||
|
||
public class OAuth2UserInfoFactory { | ||
public static OAuth2UserInfo getOAuth2UserInfo(Social social, Map<String, Object> attributes) { | ||
if (GOOGLE.equals(social)) { | ||
return new GoogleOAuth2UserInfo(attributes); | ||
} | ||
|
||
throw new GlobalException(UNKNOWN_SOCIAL); | ||
} | ||
} |
Oops, something went wrong.