-
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
π· step3 : μ¦κ²¨μ°ΎκΈ° κΈ°λ₯ ꡬν #241
base: minyul
Are you sure you want to change the base?
Changes from all commits
ba01b3c
bc0773b
d6285c7
93395aa
ff5dd75
d838e0a
a21678b
0c52d1a
b20453e
cc6bcd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package nextstep.auth.authentication; | ||
|
||
import nextstep.auth.User; | ||
import nextstep.auth.UserDetailsService; | ||
import nextstep.auth.context.Authentication; | ||
import org.springframework.util.ObjectUtils; | ||
|
||
public class Authorizer { | ||
|
||
private UserDetailsService userDetailsService; | ||
|
||
public Authorizer(UserDetailsService userDetailsService) { | ||
this.userDetailsService = userDetailsService; | ||
} | ||
|
||
public Authentication authenticate(AuthenticationToken authenticationToken) { | ||
final String principal = authenticationToken.getPrincipal(); | ||
final User userDetails = userDetailsService.loadUserByUsername(principal); | ||
checkAuthentication(userDetails, authenticationToken); | ||
|
||
return new Authentication(userDetails); | ||
} | ||
|
||
private void checkAuthentication(User userDetails, AuthenticationToken token) { | ||
if (ObjectUtils.isEmpty(userDetails)) { | ||
throw new AuthenticationException(); | ||
} | ||
|
||
if (!userDetails.checkPassword(token.getCredentials())) { | ||
throw new AuthenticationException(); | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package nextstep.auth.authorization; | ||
|
||
import nextstep.auth.authentication.AuthenticationException; | ||
import nextstep.auth.context.Authentication; | ||
import nextstep.auth.context.SecurityContextHolder; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.util.ObjectUtils; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
|
@@ -20,13 +22,20 @@ public boolean supportsParameter(MethodParameter parameter) { | |
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
authenticationValidate(authentication); | ||
if (authentication.getPrincipal() instanceof Map) { | ||
return extractPrincipal(parameter, authentication); | ||
} | ||
|
||
return authentication.getPrincipal(); | ||
} | ||
|
||
private void authenticationValidate(Authentication authentication) { | ||
if (ObjectUtils.isEmpty(authentication)) { | ||
throw new AuthenticationException(); | ||
} | ||
Comment on lines
+34
to
+36
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. π |
||
} | ||
|
||
private Object extractPrincipal(MethodParameter parameter, Authentication authentication) { | ||
try { | ||
Map<String, String> principal = (Map) authentication.getPrincipal(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package nextstep.member.application.dto; | ||
|
||
public class FavoriteRequest { | ||
|
||
private Long source; | ||
private Long target; | ||
|
||
public Long getSource() { | ||
return source; | ||
} | ||
|
||
public Long getTarget() { | ||
return target; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package nextstep.member.application.dto; | ||
|
||
import nextstep.member.domain.Favorite; | ||
import nextstep.subway.applicaion.dto.StationResponse; | ||
|
||
public class FavoriteResponse { | ||
|
||
private Long id; | ||
private StationResponse source; | ||
private StationResponse target; | ||
|
||
public static FavoriteResponse of(Favorite favorite) { | ||
|
||
return new FavoriteResponse( | ||
favorite.getId(), | ||
StationResponse.of(favorite.getSource()), | ||
StationResponse.of(favorite.getTarget())); | ||
} | ||
|
||
public FavoriteResponse(Long id, StationResponse source, StationResponse target) { | ||
this.id = id; | ||
this.source = source; | ||
this.target = target; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public StationResponse getSource() { | ||
return source; | ||
} | ||
|
||
public StationResponse getTarget() { | ||
return target; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package nextstep.member.domain; | ||
|
||
import nextstep.subway.domain.Station; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
public class Favorite { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@JoinColumn(name = "member_id") | ||
private Long member; | ||
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. νλλͺ μ΄ memberIdλ‘ νλκ² μ ν©νμ§ μμκΉμ? 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.
μ μ
νλ‘μ νΈμμ μ΄λ€ μν©μ΄μλ μ§λ μ κ° μ νν μ μ§ λͺ»νμ¬μ μ νν λ΅λ³λλ¦¬μ§ λͺ»νκ² λ€μ. μμλ λ§μλλ Έλ€μνΌ μ§μ μ°Έμ‘°, κ°μ μ°Έμ‘°λ κ°μ μ₯λ¨μ μ΄ μμ΅λλ€. μ κ°μ κ²½μ°λ μ¬μ©μμ κ΄λ ¨λ μν°ν°μμ λ€λ₯Έ μν°ν°μ μ§μ μ°Έμ‘°νλ©΄ λ°λ‘ λ°μ΄ν°λ₯Ό κ°μ Έμμ μ¬μ©ν μ μλ μ₯μ λλΉ λ€λ₯Έ μν°ν°μ κ²°ν©λκ° λμμ§λ λ¬Έμ , N+1μ΄ μκΈΈ μνμ νμ μ κ²½μ¨μΌνλ λ¬Έμ κ° λ ν¬λ€κ³ μκ°ν΄μ κ°μ μ°Έμ‘°νκ±°λ, μλλ°© μν°ν° μͺ½μμ Memberμͺ½μΌλ‘ λ¨λ°©ν₯ μ°κ΄κ΄κ³λ₯Ό λ§Ίλλ‘ νκΈ°λ₯Ό μ νΈνλ νΈμ λλ€ π |
||
|
||
@ManyToOne(cascade = CascadeType.PERSIST) | ||
@JoinColumn(name = "up_station_id") | ||
private Station source; | ||
|
||
@ManyToOne(cascade = CascadeType.PERSIST) | ||
@JoinColumn(name = "down_station_id") | ||
private Station target; | ||
Comment on lines
+17
to
+23
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. cascadeλ μμ΄ νλκ² λ μ’μ κ² κ°μμ. 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.
μ μ§λ¬Έμ μμ§λ₯Ό μ κ° μ μ΄ν΄νμ§ λͺ»νκ² μ΅λλ€....... π |
||
|
||
protected Favorite() {} | ||
|
||
public Favorite(Long memberId, Station source, Station target) { | ||
this.member = memberId; | ||
this.source = source; | ||
this.target = target; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public Long getMember() { | ||
return member; | ||
} | ||
|
||
public Station getSource() { | ||
return source; | ||
} | ||
|
||
public Station getTarget() { | ||
return target; | ||
} | ||
} |
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.
AuthenticationException
μ λμ§λ©΄ 401 UNAUTHORIZEDκ° μλ΅λ©λλ€ :)AuthenticationException
ν΄λμ€μ 보면@ResponseStatus(HttpStatus.UNAUTHORIZED)
μ΄λ Έν μ΄μ μ΄ λΆμ΄μκΈ° λλ¬Έμ λλ€!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.
AuthenticationPrincipalArgumentResolver
μresolveArgument
μμ authentication μ΄ nullμΈ κ²½μ°λ λ‘κ·ΈμΈμ΄ μλ μΌμ΄μ€λ‘ λ³Ό μ μκ² λ€μ :)