-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
장준희
committed
Nov 5, 2024
1 parent
3deb97b
commit 714eec2
Showing
10 changed files
with
153 additions
and
4 deletions.
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
29 changes: 29 additions & 0 deletions
29
src/main/java/nextstep/security/HttpSessionSecurityContextRepository.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,29 @@ | ||
package nextstep.security; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
|
||
public class HttpSessionSecurityContextRepository { | ||
|
||
private final String SPRING_SECURITY_CONTEXT_KEY = "SPRING_SECURITY_CONTEXT"; | ||
|
||
public SecurityContext loadContext(HttpServletRequest request) { | ||
HttpSession session = request.getSession(false); | ||
|
||
if (session == null) { | ||
return null; | ||
} | ||
|
||
return (SecurityContext) session.getAttribute(SPRING_SECURITY_CONTEXT_KEY); | ||
} | ||
|
||
private void saveContext( | ||
SecurityContext context, | ||
HttpServletRequest request, | ||
HttpServletResponse response | ||
) { | ||
HttpSession session = request.getSession(); | ||
session.setAttribute(SPRING_SECURITY_CONTEXT_KEY, context); | ||
} | ||
} |
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,18 @@ | ||
package nextstep.security; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import nextstep.security.authentication.Authentication; | ||
|
||
@Setter | ||
@Getter | ||
@NoArgsConstructor | ||
public class SecurityContext { | ||
|
||
private Authentication authentication; | ||
|
||
public SecurityContext(Authentication authentication) { | ||
this.authentication = authentication; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/nextstep/security/SecurityContextHolder.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,35 @@ | ||
package nextstep.security; | ||
|
||
public class SecurityContextHolder { | ||
|
||
private static final ThreadLocal<SecurityContext> contextHolder; | ||
|
||
static { | ||
contextHolder = new ThreadLocal<>(); | ||
} | ||
|
||
public static void clearContext() { | ||
contextHolder.remove(); | ||
} | ||
|
||
public static SecurityContext getContext() { | ||
SecurityContext ctx = contextHolder.get(); | ||
|
||
if (ctx == null) { | ||
ctx = createEmptyContext(); | ||
contextHolder.set(ctx); | ||
} | ||
|
||
return ctx; | ||
} | ||
|
||
public static void setContext(SecurityContext context){ | ||
if (context != null){ | ||
contextHolder.set(context); | ||
} | ||
} | ||
|
||
public static SecurityContext createEmptyContext() { | ||
return new SecurityContext(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/nextstep/security/SecurityContextHolderFilter.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,25 @@ | ||
package nextstep.security; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import org.springframework.web.filter.GenericFilterBean; | ||
|
||
public class SecurityContextHolderFilter extends GenericFilterBean { | ||
|
||
private final HttpSessionSecurityContextRepository sessionSecurityContextRepository = new HttpSessionSecurityContextRepository(); | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
SecurityContext context = this.sessionSecurityContextRepository.loadContext((HttpServletRequest) request); | ||
SecurityContextHolder.setContext(context); | ||
|
||
chain.doFilter(request, response); | ||
|
||
SecurityContextHolder.clearContext(); | ||
} | ||
} |
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
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