게으른 완벽주의자의 개발자 도전기
[Spring Security] 로그인한 사용자 security data 보기 본문
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>
'Spring Boot' 카테고리의 다른 글
[shop] 검색조건을 활용하여 아이템 검색하기 (0) | 2022.10.09 |
---|---|
[shop] 날짜 (Calendar활용/localDate(참고) (0) | 2022.10.09 |
[Spring Security] 암호화 기능 테스트 (0) | 2022.09.23 |
[Spring Security] 기초 2( 회원가입, 로그인, 로그아웃) (0) | 2022.09.22 |
[Spring Security] 기초 1 (configuration) (0) | 2022.09.22 |