Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

게으른 완벽주의자의 개발자 도전기

[Spring Security] 로그인한 사용자 security data 보기 본문

Spring Boot

[Spring Security] 로그인한 사용자 security data 보기

머리방울 2022. 9. 23. 19:22
	security 정보 빼오기
	 @GetMapping("/tag")
	 public String htmlTest() {
		 
		 
		 return"test";
	 }

<로그인 했을 때>

<로그인 하지 않았을 때>

<h1>시큐리티 태그 사용</h1>
<p th:text="${#authentication.name}"></p> <!-- 아이디 -->
<p th:text="${#authentication.authorities}"></p> <!-- 권한 -->
<p th:text="${#authentication.authenticated}"></p> <!-- 인증 받았니? -->

<!-- 인증되지 않은(로그인하지 않은) 사용자에게 보임 -->
<button sec:authorize="isAnonymous()" type="button" onclick="location.href='/admin/loginView'">로그인</button>
<!-- 인증된(로그인한) 사용자에게 보임 -->
<button sec:authorize="isAuthenticated()" type="button" onclick="location.href='/admin/logout'">로그아웃</button>

<!-- ROLE_ADMIN 권한을 가지고 있다면 보임 -->
<div sec:authorize="hasRole('ROLE_ADMIN')">ROLE_ADMIN 권한이 있습니다.</div>
<!-- ROLE_SUB_ADMIN 권한을 가지고 있다면 보임 -->
<div sec:authorize="hasRole('ROLE_MANAGER')">ROLE_MANAGER 권한이 있습니다.</div>
<!-- ROLE_USER 권한을 가지고 있다면 보임 -->
<div sec:authorize="hasRole('ROLE_MEMBER')">ROLE_MEMBER 권한이 있습니다.</div>
<!-- ROLE_ADMIN 혹은 ROLE_SUB_ADMIN 권한을 가지고 있다면 보임 -->
<div sec:authorize="hasAnyRole('ROLE_ADMIN', 'ROLE_MANAGER')">ROLE_ADMIN 혹은 ROLE_MANAGER 권한이 있습니다.</div>

<br/>
<!--인증시 사용된 객체에 대한 정보-->
<b>Authenticated DTO:</b>
<div sec:authorize="isAuthenticated()" sec:authentication="principal"></div>

<br/>
<!--인증시 사용된 객체의 Username (ID)-->
<b>Authenticated username:</b>
<div sec:authorize="isAuthenticated()" sec:authentication="name"></div>

<br/>
<!--객체의 권한-->
<b>Authenticated admin role:</b>
<div sec:authorize="isAuthenticated()" sec:authentication="principal.authorities"></div>