-
Notifications
You must be signed in to change notification settings - Fork 240
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
Step2 리뷰 요청드립니다~ #346
Open
sang-eun
wants to merge
10
commits into
next-step:sang-eun
Choose a base branch
from
sang-eun:step2
base: sang-eun
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Step2 리뷰 요청드립니다~ #346
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c42932d
[add] form login unit test, acceptance test, 구현
sang-eun bbd2e96
[add] bearer login acceptance test, 구현
sang-eun bc8efda
[add] 권한 기능
sang-eun fde6d3a
[add] 권한 기능 체크 인수테스트
sang-eun a3ef715
[modify] TokenAuthenticationInterceptor, UsernamePasswordAuthenticati…
sang-eun c0c4ab3
[modify] BasicAuthenticationFilter, BearerTokenAuthenticationFilter 추…
sang-eun d7c23e2
[modify] auth에서 LoginMemberService 의존성 제거
sang-eun 54cdba1
[modify] step1 코드리뷰 반영
sang-eun bcf2327
Merge branch 'sang-eun' of https://github.com/next-step/atdd-subway-f…
sang-eun 533ccdf
[modify] 코드리뷰 반영
sang-eun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions
3
src/main/java/nextstep/auth/authentication/AuthenticationToken.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
39 changes: 39 additions & 0 deletions
39
src/main/java/nextstep/auth/authentication/Authenticator.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 nextstep.auth.authentication; | ||
|
||
import nextstep.member.application.UserDetailsService; | ||
import nextstep.member.domain.User; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
public abstract class Authenticator implements HandlerInterceptor { | ||
|
||
private final UserDetailsService userDetailsService; | ||
|
||
public Authenticator(UserDetailsService userDetailsService) { | ||
this.userDetailsService = userDetailsService; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException { | ||
AuthenticationToken token = convert(request); | ||
User user = userDetailsService.loadUserByUsername(token.getPrincipal()); | ||
|
||
checkAuthentication(user, token.getCredentials()); | ||
authenticate(user, response); | ||
|
||
return false; | ||
} | ||
|
||
abstract public AuthenticationToken convert(HttpServletRequest request) throws IOException; | ||
|
||
abstract public void authenticate(User user, HttpServletResponse response) throws IOException; | ||
|
||
private void checkAuthentication(User user, String password) { | ||
if (!user.checkPassword(password)) { | ||
throw new AuthenticationException(); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/nextstep/auth/authentication/Authorizator.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,26 @@ | ||
package nextstep.auth.authentication; | ||
|
||
import nextstep.auth.context.Authentication; | ||
import nextstep.auth.context.SecurityContextHolder; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
public abstract class Authorizator implements HandlerInterceptor { | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
|
||
try { | ||
Authentication authentication = convert(request); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} catch (RuntimeException e){ | ||
return true; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
abstract public Authentication convert(HttpServletRequest request); | ||
} |
53 changes: 24 additions & 29 deletions
53
src/main/java/nextstep/auth/authentication/BasicAuthenticationFilter.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 |
---|---|---|
@@ -1,50 +1,45 @@ | ||
package nextstep.auth.authentication; | ||
|
||
import nextstep.auth.context.Authentication; | ||
import nextstep.auth.context.SecurityContextHolder; | ||
import nextstep.member.application.LoginMemberService; | ||
import nextstep.member.domain.LoginMember; | ||
import nextstep.member.application.UserDetailsService; | ||
import nextstep.member.domain.User; | ||
import org.apache.tomcat.util.codec.binary.Base64; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
public class BasicAuthenticationFilter implements HandlerInterceptor { | ||
private LoginMemberService loginMemberService; | ||
public class BasicAuthenticationFilter extends Authorizator { | ||
|
||
public BasicAuthenticationFilter(LoginMemberService loginMemberService) { | ||
this.loginMemberService = loginMemberService; | ||
private final UserDetailsService userDetailsService; | ||
|
||
public BasicAuthenticationFilter(UserDetailsService userDetailsService) { | ||
this.userDetailsService = userDetailsService; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
try { | ||
String authCredentials = AuthorizationExtractor.extract(request, AuthorizationType.BASIC); | ||
String authHeader = new String(Base64.decodeBase64(authCredentials)); | ||
public Authentication convert(HttpServletRequest request) { | ||
AuthenticationToken token = getToken(request); | ||
User loginMember = userDetailsService.loadUserByUsername(token.getPrincipal()); | ||
|
||
String[] splits = authHeader.split(":"); | ||
String principal = splits[0]; | ||
String credentials = splits[1]; | ||
checkAuthentication(token, loginMember); | ||
|
||
AuthenticationToken token = new AuthenticationToken(principal, credentials); | ||
return new Authentication(loginMember.getEmail(), loginMember.getAuthorities()); | ||
} | ||
|
||
LoginMember loginMember = loginMemberService.loadUserByUsername(token.getPrincipal()); | ||
if (loginMember == null) { | ||
throw new AuthenticationException(); | ||
} | ||
|
||
if (!loginMember.checkPassword(token.getCredentials())) { | ||
throw new AuthenticationException(); | ||
} | ||
private AuthenticationToken getToken(HttpServletRequest request) { | ||
String authCredentials = AuthorizationExtractor.extract(request, AuthorizationType.BASIC); | ||
String authHeader = new String(Base64.decodeBase64(authCredentials)); | ||
|
||
Authentication authentication = new Authentication(loginMember.getEmail(), loginMember.getAuthorities()); | ||
String[] splits = authHeader.split(":"); | ||
String principal = splits[0]; | ||
String credentials = splits[1]; | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
return new AuthenticationToken(principal, credentials); | ||
} | ||
|
||
return true; | ||
} catch (Exception e) { | ||
return true; | ||
private void checkAuthentication(AuthenticationToken token, User user) { | ||
if (!user.checkPassword(token.getCredentials())) { | ||
throw new AuthenticationException(); | ||
} | ||
} | ||
} |
24 changes: 4 additions & 20 deletions
24
src/main/java/nextstep/auth/authentication/BearerTokenAuthenticationFilter.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 |
---|---|---|
@@ -1,37 +1,21 @@ | ||
package nextstep.auth.authentication; | ||
|
||
import nextstep.auth.context.Authentication; | ||
import nextstep.auth.context.SecurityContextHolder; | ||
import nextstep.auth.token.JwtTokenProvider; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.util.List; | ||
|
||
public class BearerTokenAuthenticationFilter implements HandlerInterceptor { | ||
public class BearerTokenAuthenticationFilter extends Authorizator { | ||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
public BearerTokenAuthenticationFilter(JwtTokenProvider jwtTokenProvider) { | ||
this.jwtTokenProvider = jwtTokenProvider; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
try { | ||
String authCredentials = AuthorizationExtractor.extract(request, AuthorizationType.BEARER); | ||
public Authentication convert(HttpServletRequest request) { | ||
String authCredentials = AuthorizationExtractor.extract(request, AuthorizationType.BEARER); | ||
|
||
jwtTokenProvider.validateToken(authCredentials); | ||
String principal = jwtTokenProvider.getPrincipal(authCredentials); | ||
List<String> roles = jwtTokenProvider.getRoles(authCredentials); | ||
|
||
Authentication authentication = new Authentication(principal, roles); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
|
||
return true; | ||
} catch (RuntimeException exception) { | ||
return true; | ||
} | ||
return jwtTokenProvider.toAuthentication(authCredentials); | ||
} | ||
} |
7 changes: 5 additions & 2 deletions
7
...a/nextstep/member/domain/LoginMember.java → ...step/auth/authentication/LoginMember.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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
package nextstep.member.domain; | ||
package nextstep.auth.authentication; | ||
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.
|
||
|
||
|
||
import nextstep.member.domain.Member; | ||
import nextstep.member.domain.User; | ||
|
||
import java.util.List; | ||
|
||
public class LoginMember { | ||
public class LoginMember implements User { | ||
private String email; | ||
private String password; | ||
private List<String> authorities; | ||
|
39 changes: 11 additions & 28 deletions
39
src/main/java/nextstep/auth/authentication/UsernamePasswordAuthenticationFilter.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 |
---|---|---|
@@ -1,45 +1,28 @@ | ||
package nextstep.auth.authentication; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import nextstep.auth.context.Authentication; | ||
import nextstep.auth.context.SecurityContext; | ||
import nextstep.auth.context.SecurityContextHolder; | ||
import nextstep.member.application.LoginMemberService; | ||
import nextstep.member.domain.LoginMember; | ||
import nextstep.member.domain.Member; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import nextstep.member.application.UserDetailsService; | ||
import nextstep.member.domain.User; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.util.List; | ||
import java.io.IOException; | ||
|
||
public class UsernamePasswordAuthenticationFilter implements HandlerInterceptor { | ||
private final LoginMemberService loginMemberService; | ||
public class UsernamePasswordAuthenticationFilter extends Authenticator { | ||
|
||
public UsernamePasswordAuthenticationFilter(LoginMemberService loginMemberService) { | ||
this.loginMemberService = loginMemberService; | ||
public UsernamePasswordAuthenticationFilter(UserDetailsService userDetailsService) { | ||
super(userDetailsService); | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
String email = request.getParameter("email"); | ||
String password = request.getParameter("password"); | ||
|
||
LoginMember member; | ||
|
||
try { | ||
member = loginMemberService.loadUserByUsername(email); | ||
} catch (RuntimeException e) { | ||
throw new AuthenticationException(); | ||
} | ||
|
||
if (!member.checkPassword(password)) { | ||
throw new AuthenticationException(); | ||
} | ||
public AuthenticationToken convert(HttpServletRequest request) { | ||
return new AuthenticationToken(request.getParameter("email"), request.getParameter("password")); | ||
} | ||
|
||
@Override | ||
public void authenticate(User member, HttpServletResponse response) throws IOException { | ||
Authentication authentication = new Authentication(member.getEmail(), member.getAuthorities()); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
|
||
return true; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/java/nextstep/auth/authorization/AuthenticationPrincipalArgumentResolver.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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
미션은
auth
패키지 내에 라이브러리를 제외한 비즈니스 패키지들이 들어오지 않아야 완료됩니다 😢현재
auth
패키지가member
에 의존하고 있어요