2025-04-30
Authentication
๊ฐ์ฒด๋ฅผ ์ปจํธ๋กค๋ฌ ๋ฉ์๋์์ ์ง์ ์ฃผ์
@GetMapping("/user")
public void user(Authentication authentication) {
log.info("GET / user..." + authentication); // ๋ก๊ทธ์ธ ํ ์ธ์ฆ ๊ฐ์ฒด ์ ์ฒด ํ์ธ
log.info("name..." + authentication.getName());
log.info("principal..." + authentication.getPrincipal());
log.info("authorities..." + authentication.getAuthorities()); // ๊ถํ ๋ฆฌ์คํธ
log.info("details..." + authentication.getDetails());
log.info("credential..." + authentication.getCredentials()); // ์ฌ์ฉ์ ์๊ฒฉ์ฆ๋ช
(๋น๋ฐ๋ฒํธ ๋ฑ)
}
@AuthenticationPrincipal
์ด๋
ธํ
์ด์
์ฌ์ฉ@GetMapping("/user")
public void user(@AuthenticationPrincipal Principal principal) {
log.info("GET / user..." + principal); // Principal ๊ฐ์ฒด๋ก ์ฌ์ฉ์ ์ ๋ณด ํ์ธ
}
SecurityContextHolder
๋ฅผ ํตํด ์ธ์ฆ ๊ฐ์ฒด ์ง์ ์กฐํ (๊ฐ์ฅ ์ผ๋ฐ์ )@GetMapping("/user")
public void user(Model model) {
log.info("GET / user...");
// ์ด๋์๋ ์ ๊ทผ ๊ฐ๋ฅํ ๋ฐฉ์
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
log.info("authentication : " + authentication);
model.addAttribute("auth", authentication); // ์ธ์ฆ ์ ๋ณด JSP์ ์ ๋ฌ
}
โ ์ธ์ฆ ์ ๋ณด๋ Controller, View(JSP) ์ด๋์๋ ์ ๊ทผ ๊ฐ๋ฅํ์ง๋ง,
๋ณด์ ์ฒ๋ฆฌ๋ Controller/Service์์, UI ์ถ๋ ฅ์ JSP์์ ๋ถ๋ฆฌํ๋ ๊ฒ์ด ์ ์ง๋ณด์์ ์ ๋ฆฌํจ.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MANAGER PAGE</title>
</head>
<body>
<h1>MANAGER</h1>
<p>PRINCIPAL : <sec:authentication property="principal"/></p>
<p>USERDTO : <sec:authentication property="principal.userDto"/></p>
<p>principal๋ก ๊บผ๋ธ ID : <sec:authentication property="principal.username"/></p><hr/>
<p>์ฌ์ฉ์์ ์ด๋ฆ : <sec:authentication property="principal.userDto.username"/></p><hr/>
<p>์ฌ์ฉ์์ ์์ด๋ : <sec:authentication property="principal.userDto.username"/></p><hr/>
<p>์ฌ์ฉ์์ ๊ถํ ๋ชฉ๋ก : <sec:authentication property="principal.userDto.role"/></p><hr/>
<sec:authorize access="isAuthenticated()">
<p>๋ก๊ทธ์ธ ์ฌ์ฉ์์
๋๋ค.</p>
</sec:authorize>
<sec:authorize access="isAnonymous()">
<p>๋น๋ก๊ทธ์ธ ์ฌ์ฉ์์
๋๋ค. ๋ก๊ทธ์ธ ํด์ฃผ์ธ์.</p>
</sec:authorize>
<a href="${pageContext.request.contextPath}/logout">๋ก๊ทธ์์</a>
</body>
</html>
ROLE_USER
, ROLE_ADMIN
๋ฑ ๊ถํ์ ๋ณด์์ ์ผ๋ก ๋ฏผ๊ฐํ ์ ๋ณด@PreAuthorize("hasRole('ADMIN')")
@PostMapping("/admin/assign-role")
public String assignRole(@RequestParam String username, @RequestParam String role) {
// ์ฌ์ฉ์์๊ฒ ๋กค(role) ๋ถ์ฌ
return "redirect:/admin";
}
๊ตฌ๋ถ | ์ค๋ช |
---|---|
์ธ์ฆ ์ ๋ณด ์ ๊ทผ | Authentication , @AuthenticationPrincipal , SecurityContextHolder |
JSP ๋ณด์ ํํ | <sec:authentication> , <sec:authorize> |
๊ด๋ฆฌ์ ๊ถํ | ๊ถํ(Role) ๋ถ์ฌ๋ ๋ฐ๋์ ROLE_ADMIN ์ฌ์ฉ์๋ง ์ํ |
์คํ ์ฃผ์ | principal , isAuthenticated() ์ ํํ๊ฒ ์
๋ ฅํ ๊ฒ |
๋ณด์ ์ค๊ณ ํ | ์ธ์ฆ ์ถ๋ ฅ์ JSP, ์ญํ ํ๋จ ๋ฐ ์ ์ด๋ Controller/Service์์ ๋ด๋น |
์ด๋ฒ ํ์ต์ ํตํด Spring Security์์ ์ธ์ฆ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ๋ค์ํ ๋ฐฉ๋ฒ์ ์ตํ๊ณ , ํนํ JSP์์ ์ง์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๊ณ ๊ถํ ๋ถ๊ธฐ๋ฅผ ์ค์ ํ๋ ๋ฐฉ๋ฒ๋ ์ดํดํ๊ฒ ๋์๋ค. ๋ก๊ทธ์ธ ์ฌ๋ถ, ์ฌ์ฉ์ ๊ฐ์ฒด ๊ตฌ์กฐ, SecurityContext์ ํ๋ฆ์ ์ค์ ์ฝ๋์ ๋ก๊ทธ๋ฅผ ํตํด ํ์ธํ๋ฉด์ ๋์ ๋ฐฉ์์ ๋ช ํํ ์ดํดํ ์ ์์๋ค.
์คํ ํ๋๋ก๋ ํฐ ์ค๋ฅ๊ฐ ๋ ์ ์๊ธฐ ๋๋ฌธ์, ์ ํํ ์์ฑ๋ช ์ ์ฌ์ฉํ๋ ์ต๊ด์ด ์ค์ํ๋ค๋ ์ ๋ ๋๊ผ๋ค.