-
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.
- Loading branch information
Showing
16 changed files
with
193 additions
and
70 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
49 changes: 49 additions & 0 deletions
49
daepiro-auth/src/main/java/com/numberone/backend/filter/NaverAuthenticationFilter.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,49 @@ | ||
package com.numberone.backend.filter; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.numberone.backend.handler.SocialAuthenticationFailureHandler; | ||
import com.numberone.backend.handler.SocialAuthenticationSuccessHandler; | ||
import com.numberone.backend.provider.NaverAuthenticationProvider; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.security.authentication.ProviderManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; | ||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StreamUtils; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class NaverAuthenticationFilter extends AbstractAuthenticationProcessingFilter { | ||
private final ObjectMapper objectMapper; | ||
private static final String JSON_PARAM = "token"; | ||
private static final String REQUEST_URL = "/token/naver"; | ||
private static final String REQUEST_METHOD = "POST"; | ||
|
||
public NaverAuthenticationFilter(NaverAuthenticationProvider naverAuthenticationProvider, | ||
SocialAuthenticationSuccessHandler socialAuthenticationSuccessHandler, | ||
SocialAuthenticationFailureHandler socialAuthenticationFailureHandler, | ||
ObjectMapper objectMapper){ | ||
super(new AntPathRequestMatcher(REQUEST_URL, REQUEST_METHOD)); | ||
setAuthenticationManager(new ProviderManager(naverAuthenticationProvider)); | ||
setAuthenticationSuccessHandler(socialAuthenticationSuccessHandler); | ||
setAuthenticationFailureHandler(socialAuthenticationFailureHandler); | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException { | ||
String requestBody = StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8); | ||
Map<String, String> requestBodyMap = objectMapper.readValue(requestBody, Map.class); | ||
String token = requestBodyMap.get(JSON_PARAM); | ||
Authentication authentication = UsernamePasswordAuthenticationToken.unauthenticated(token, null); | ||
return getAuthenticationManager().authenticate(authentication); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
daepiro-auth/src/main/java/com/numberone/backend/provider/NaverAuthenticationProvider.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,39 @@ | ||
package com.numberone.backend.provider; | ||
|
||
import com.numberone.backend.domain.member.entity.Member; | ||
import com.numberone.backend.domain.member.repository.MemberRepository; | ||
import com.numberone.backend.feign.NaverFeign; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class NaverAuthenticationProvider implements AuthenticationProvider { | ||
private final NaverFeign naverFeign; | ||
private final MemberRepository memberRepository; | ||
|
||
|
||
@Override | ||
public Authentication authenticate(Authentication authentication) throws AuthenticationException { | ||
String token = (String) authentication.getPrincipal(); | ||
String naverId; | ||
try { | ||
naverId = naverFeign.getUserData(JwtProvider.PREFIX_BEARER + token).getResponse().getId(); | ||
} catch (Exception e) { | ||
throw new BadCredentialsException("유효하지 않은 OAuth 토큰입니다."); | ||
} | ||
Member member = memberRepository.findByNaverId(naverId) | ||
.orElseGet(() -> memberRepository.save(Member.ofNaver(naverId))); | ||
return UsernamePasswordAuthenticationToken.authenticated(member.getId(), null, null); | ||
} | ||
|
||
@Override | ||
public boolean supports(Class<?> authentication) { | ||
return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication)); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
daepiro-common/src/main/java/com/numberone/backend/feign/NaverFeign.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,11 @@ | ||
package com.numberone.backend.feign; | ||
|
||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
|
||
@FeignClient(name = "naverFeign", url = "${spring.naver.api-url}") | ||
public interface NaverFeign { | ||
@GetMapping(value = "${spring.naver.token-info-url}") | ||
NaverIdDto getUserData(@RequestHeader(name = "Authorization") String token); | ||
} |
18 changes: 18 additions & 0 deletions
18
daepiro-common/src/main/java/com/numberone/backend/feign/NaverIdDto.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.numberone.backend.feign; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class NaverIdDto { | ||
private Response response; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public static class Response { | ||
private String id; | ||
} | ||
} |
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
Oops, something went wrong.