-
Notifications
You must be signed in to change notification settings - Fork 20
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] 리펙터링 #6
base: hyunssooo
Are you sure you want to change the base?
[Step2] 리펙터링 #6
Changes from 7 commits
b350621
e136e3e
facf63b
76a93c2
3d06e2d
499702f
44ebd16
fc595d1
3a4a9a5
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,31 @@ | ||
package nextstep.app.config; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import nextstep.app.ui.dto.LoginUser; | ||
import nextstep.security.context.SecurityContext; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
public class LoginUserArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.getParameterType().equals(LoginUser.class); | ||
} | ||
|
||
@Override | ||
public LoginUser resolveArgument( | ||
MethodParameter parameter, | ||
ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, | ||
WebDataBinderFactory binderFactory | ||
) { | ||
final HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest(); | ||
final SecurityContext context = (SecurityContext) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT"); | ||
final String email = context.getAuthentication().getPrincipal().toString(); | ||
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. SecurityContextHolder를 사용해서 가져올 수도 있겠네요!
(+) 조금 더 나아가보면, 지금은 Authentication의 Pricipal로 String(username)을 사용하고 있는데요. |
||
return new LoginUser(email); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package nextstep.app.ui.dto; | ||
|
||
public class LoginUser { | ||
private final String email; | ||
|
||
public LoginUser(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "LoginUser{" + | ||
"email='" + email + '\'' + | ||
'}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package nextstep.security.authorization; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import nextstep.security.authentication.Authentication; | ||
import nextstep.security.context.SecurityContext; | ||
import nextstep.security.context.SecurityContextHolder; | ||
import nextstep.security.context.SecurityContextRepository; | ||
import org.springframework.web.filter.GenericFilterBean; | ||
|
||
public class PreAuthorizationFilter extends GenericFilterBean { | ||
|
||
private final SecurityContextRepository securityContextRepository; | ||
|
||
public PreAuthorizationFilter(SecurityContextRepository securityContextRepository) { | ||
this.securityContextRepository = securityContextRepository; | ||
} | ||
|
||
@Override | ||
public void doFilter( | ||
ServletRequest request, | ||
ServletResponse response, | ||
FilterChain chain | ||
) throws IOException, ServletException { | ||
try { | ||
if (SecurityContextHolder.getContext().getAuthentication() != null) { | ||
chain.doFilter(request, response); | ||
return; | ||
} | ||
|
||
final HttpServletRequest httpServletRequest = (HttpServletRequest) request; | ||
SecurityContext context = Optional.ofNullable( | ||
(SecurityContext) httpServletRequest.getSession() | ||
.getAttribute(SecurityContextHolder.SPRING_SECURITY_CONTEXT_KEY) | ||
) | ||
.orElseGet(() -> securityContextRepository.loadContext(httpServletRequest)); | ||
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. 주입된 securityContextRepository가 HttpSessionSecurityContextRepository 인데 동일한 일을 하고 있지 않나요? |
||
|
||
final Authentication authentication = Optional.ofNullable(context) | ||
.map(it -> it.getAuthentication()) | ||
.orElse(null); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
|
||
} catch (Exception ignored) { | ||
|
||
} | ||
chain.doFilter(request, response); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package nextstep.security.authorization.manager; | ||
|
||
import nextstep.security.authentication.Authentication; | ||
|
||
public class AuthenticationRoleManager implements RoleManager { | ||
|
||
@Override | ||
public boolean check(Authentication authentication) { | ||
return authentication.isAuthenticated(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package nextstep.security.authorization.manager; | ||
|
||
import java.util.Set; | ||
import nextstep.security.authentication.Authentication; | ||
|
||
public class AuthorizationRoleManager implements RoleManager { | ||
|
||
private final Set<String> authorities; | ||
|
||
public AuthorizationRoleManager(Set<String> authorities) { | ||
this.authorities = authorities; | ||
} | ||
|
||
public AuthorizationRoleManager(String... authorities) { | ||
this(Set.of(authorities)); | ||
} | ||
|
||
@Override | ||
public boolean check(Authentication authentication) { | ||
return authentication.getAuthorities().stream() | ||
.anyMatch(authorities::contains); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package nextstep.security.authorization.manager; | ||
|
||
import nextstep.security.authentication.Authentication; | ||
|
||
public class DenyAllRoleManager implements RoleManager { | ||
|
||
@Override | ||
public boolean check(Authentication authentication) { | ||
return false; | ||
} | ||
} |
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.
오오 ArgumentResolver 사용 좋네요! 👏👏