-
Notifications
You must be signed in to change notification settings - Fork 2
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
[FEAT] 네이버 소셜 로그인 구현 #36
Changes from all commits
64b38b9
85e2605
17f8f3a
ed4a902
87cf6f3
ff52f8c
7faf57b
53fb669
f7d7c2d
b47d729
dd01e75
7fe78a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.talkka.server.oauth.controller; | ||
|
||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
|
||
import com.talkka.server.oauth.domain.OAuth2UserInfo; | ||
|
||
// 인증 테스트를 위한 임시 컨트롤러 | ||
@Controller | ||
public class BaseController { | ||
@GetMapping("/") | ||
public String authIndex(Model model, @AuthenticationPrincipal OAuth2UserInfo userInfo) { | ||
model.addAttribute("name", userInfo.getName()); | ||
model.addAttribute("email", userInfo.getEmail()); | ||
model.addAttribute("nickname", userInfo.getNickName()); | ||
model.addAttribute("oauth2Id", userInfo.getOAuth2Id()); | ||
model.addAttribute("provider", userInfo.getProvider()); | ||
model.addAttribute("accessToken", userInfo.getAccessToken()); | ||
return "index"; | ||
} | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.talkka.server.oauth.domain; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
|
||
public class NaverOAuth2User extends OAuth2UserInfo { | ||
|
||
public NaverOAuth2User(Map<String, Object> attributes) { | ||
super((Map<String, Object>)attributes.get("response")); | ||
} | ||
|
||
public NaverOAuth2User(Map<String, Object> attributes, Collection<? extends GrantedAuthority> authorities) { | ||
super((Map<String, Object>)attributes, authorities); | ||
} | ||
|
||
@Override | ||
public String getProvider() { | ||
return "NAVER"; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.talkka.server.oauth.domain; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
|
||
public abstract class OAuth2UserInfo implements OAuth2User { | ||
|
||
protected final Map<String, Object> attributes; | ||
private final Collection<? extends GrantedAuthority> authorities; | ||
|
||
public OAuth2UserInfo(Map<String, Object> attributes, Collection<? extends GrantedAuthority> authorities) { | ||
this.attributes = attributes; | ||
this.authorities = authorities; | ||
} | ||
|
||
// OAuth2 인증까지만 통과하면 UNREGISTERED 권한 부여 | ||
public OAuth2UserInfo(Map<String, Object> attributes) { | ||
this.attributes = attributes; | ||
this.authorities = List.of(new SimpleGrantedAuthority("UNREGISTERED")); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getAttributes() { | ||
return attributes; | ||
} | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return authorities; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return (String)attributes.get("name"); | ||
} | ||
|
||
public String getOAuth2Id() { | ||
return (String)attributes.get("id"); | ||
} | ||
|
||
public String getEmail() { | ||
return (String)attributes.get("email"); | ||
} | ||
|
||
public String getAccessToken() { | ||
return (String)attributes.get("accessToken"); | ||
} | ||
|
||
public String getNickName() { | ||
return (String)attributes.get("nickname"); | ||
} | ||
Comment on lines
+38
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (String) 으로 강제 캐스팅하는 것이 꺼림칙한데, 어떤 이유가 있는지 궁금합니다
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 동일한 질문입니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
현재 OAuth2 인증객체( |
||
|
||
// oauth provider 는 각 제공자 별로 구현 | ||
public abstract String getProvider(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.talkka.server.oauth.filter; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
|
||
import jakarta.servlet.Filter; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.FilterConfig; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
public class UnregisteredUserFilter implements Filter { | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
HttpServletRequest httpRequest = (HttpServletRequest)request; | ||
HttpServletResponse httpResponse = (HttpServletResponse)response; | ||
|
||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
|
||
if (authentication != null) { | ||
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); | ||
for (GrantedAuthority authority : authorities) { | ||
if (authority.getAuthority().equals("UNREGISTERED") | ||
&& !httpRequest.getRequestURI().equals("/auth/signUp")) { | ||
httpResponse.sendRedirect("/auth/signUp"); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
chain.doFilter(request, response); | ||
Comment on lines
+28
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final 로 Object reference 바뀌는지 안바뀌는지 확실히 해주는 것이 좋아보입니다. |
||
} | ||
|
||
@Override | ||
public void destroy() { | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앞으로 Session 에 저장된 유저 인가 정보를 조회할 때
@AuthenticationPrincipal
을 사용하면 된다고 이해하면 될까요?그리고
OAuthUserInfo
의 역할도 궁금합니다.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 아직 시큐리티와 세션쪽을 잘 몰라서
@AuthenticationPrincipal
외에도 세션을 관리할 수 있는 것들이 많다고 들었는데해당 어노테이션을 선택하신 이유가 궁금합니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AuthenticationPrincipal
를 사용해 현재 사용자의 인증 정보를 가져오는게 맞습니다.OAuthUserInfo
는 인증객체 + dto 느낌으로 생각하면 될 것 같습니다.기본적인 인증 정보들과 함께 db에 보관중인 추가 정보를 넣을 수 있게
OAuth2User
를 상속했습니다.SecurityContext
나Authentication
객체로 주입받을 경우getPrincipal()
로 인증정보(OAuth2UserInfo)를 한번 더 꺼내야 하는 번거로움이 있어@AuthenticationPrincipal
로OAuth2UserInfo
를 직접 주입받았습니다.