반응형

build.gradle에
implementation 'org.springframework.boot:spring-boot-starter-security' /* security */
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
의존성 추가 하고 thymeleaf 사용하는 방법은 아래와 같다.
아 참고로
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
이 부분도 html에 추가 해줘야 된다!
<!-- 인증되지 않은(로그인하지 않은) 사용자에게 보임 -->
<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('ADMIN')">ROLE_ADMIN 권한이 있습니다.</div>
<!-- ROLE_SUB_ADMIN 권한을 가지고 있다면 보임 -->
<div sec:authorize="hasRole('SUB_ADMIN')">ROLE_SUB_ADMIN 권한이 있습니다.</div>
<!-- ROLE_USER 권한을 가지고 있다면 보임 -->
<div sec:authorize="hasRole('USER')">ROLE_USER 권한이 있습니다.</div>
<!-- ROLE_ADMIN 혹은 ROLE_SUB_ADMIN 권한을 가지고 있다면 보임 -->
<div sec:authorize="hasAnyRole('ADMIN, SUB_ADMIN')">ROLE_ADMIN 혹은 ROLE_SUB_ADMIN 권한이 있습니다.</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>
끗!
반응형
'공부 > SpringBoot & Spring' 카테고리의 다른 글
| [Thymeleaf] 문자열 조합(합치기) (1) | 2022.07.05 |
|---|---|
| Spring Boot Embeded Tomcat 내장 톰캣 application.properties 설정 (0) | 2022.06.27 |
| Mybatis Mapper 1064(42000) error (0) | 2022.06.23 |
| Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 오류 (0) | 2022.06.22 |
| has an unsupported return type 오류 (0) | 2022.06.17 |