-
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 9 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.LoginMember; | ||
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); | ||
LoginMember member = userDetailsService.loadUserByUsername(token.getPrincipal()); | ||
|
||
checkAuthentication(member, token.getCredentials()); | ||
authenticate(member, response); | ||
|
||
return false; | ||
} | ||
|
||
abstract public AuthenticationToken convert(HttpServletRequest request) throws IOException; | ||
|
||
abstract public void authenticate(LoginMember member, HttpServletResponse response) throws IOException; | ||
|
||
private void checkAuthentication(LoginMember member, String password) { | ||
if (!member.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); | ||
} |
51 changes: 23 additions & 28 deletions
51
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.application.UserDetailsService; | ||
import nextstep.member.domain.LoginMember; | ||
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); | ||
LoginMember 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, LoginMember loginMember) { | ||
if (!loginMember.checkPassword(token.getCredentials())) { | ||
throw new AuthenticationException(); | ||
} | ||
} | ||
} |
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
37 changes: 10 additions & 27 deletions
37
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.application.UserDetailsService; | ||
import nextstep.member.domain.LoginMember; | ||
import nextstep.member.domain.Member; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
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(LoginMember member, HttpServletResponse response) throws IOException { | ||
Authentication authentication = new Authentication(member.getEmail(), member.getAuthorities()); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
|
||
return true; | ||
} | ||
} |
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.
아래 요구사항이 반영되지 않은 것 같아요 😢