๐Ÿ“Œ Spring Security ์ธ์ฆ ์ •๋ณด ํ™•์ธ ๋ฐฉ๋ฒ• ์ด์ •๋ฆฌ + JSP Taglib ํ™œ์šฉ

My Pale Blue Dotยท2025๋…„ 4์›” 30์ผ
0

SPRING

๋ชฉ๋ก ๋ณด๊ธฐ
34/36
post-thumbnail

๐Ÿ“… ๋‚ ์งœ

2025-04-30


๐Ÿ“ ํ•™์Šต ๋‚ด์šฉ

1๏ธโƒฃ Spring Security์—์„œ ์ธ์ฆ(Authentication) ์ •๋ณด ํ™•์ธ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•

โœ… ๋ฐฉ๋ฒ• 1: 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()); // ์‚ฌ์šฉ์ž ์ž๊ฒฉ์ฆ๋ช… (๋น„๋ฐ€๋ฒˆํ˜ธ ๋“ฑ)
}

โœ… ๋ฐฉ๋ฒ• 2: @AuthenticationPrincipal ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ

@GetMapping("/user")
public void user(@AuthenticationPrincipal Principal principal) {
	log.info("GET / user..." + principal); // Principal ๊ฐ์ฒด๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด ํ™•์ธ
}

โœ… ๋ฐฉ๋ฒ• 3: 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์—์„œ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์œ ์ง€๋ณด์ˆ˜์— ์œ ๋ฆฌํ•จ.


2๏ธโƒฃ JSP์—์„œ Spring Security ํƒœ๊ทธ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(taglib) ์‚ฌ์šฉํ•˜๊ธฐ

<%@ 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>

3๏ธโƒฃ ๋กค(Role) ๋ถ€์—ฌ๋Š” ๋ฐ˜๋“œ์‹œ ๊ด€๋ฆฌ์ž(Admin) ๊ถŒํ•œ๋งŒ ๊ฐ€๋Šฅ!

  • 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์˜ ํ๋ฆ„์„ ์‹ค์ œ ์ฝ”๋“œ์™€ ๋กœ๊ทธ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•˜๋ฉด์„œ ๋™์ž‘ ๋ฐฉ์‹์„ ๋ช…ํ™•ํžˆ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

์˜คํƒ€ ํ•˜๋‚˜๋กœ๋„ ํฐ ์˜ค๋ฅ˜๊ฐ€ ๋‚  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ •ํ™•ํ•œ ์†์„ฑ๋ช…์„ ์‚ฌ์šฉํ•˜๋Š” ์Šต๊ด€์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ์ ๋„ ๋А๊ผˆ๋‹ค.


profile
Here, My Pale Blue.๐ŸŒ

0๊ฐœ์˜ ๋Œ“๊ธ€