관리 메뉴

제뉴어리의 모든것

Spring Security와 Thymeleaf 기능을 사용하는 HTML 에서 사용시 주의사항 본문

Spring Boot/Spring Security

Spring Security와 Thymeleaf 기능을 사용하는 HTML 에서 사용시 주의사항

제뉴어리맨 2022. 9. 23. 11:33

상황

현재 내 프로젝트가 Spring Boot 이고,

보안 기능으로는 Spring Security 를 쓰고,

SSR 환경에서 Thymeleaf를 쓰고,

그리고 프로젝트내에 여러 HTML 문서 중 아래와 같은 문서가 있다고 생각을 해보자.

 

 

문제의 HTML 문서

<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
    <body>
        <div align="right" th:fragment="header">
            <a href="/members/register" class="text-decoration-none">회원가입</a> |

            <span sec:authorize="isAuthenticated()"> <!-- 1 -->
                <sapn sec:authorize="hasRole('USER')"> <!-- 2 -->
                    <a href="/members/my-page" class="text-decoration-none"> 마이 페이지 | </a>

                </sapn>


                <a href="/logout" class="text-decoration-none"> 로그아웃 | </a>
                <span th:text="${#authentication.name}">홍길동</span>님 <!-- 3 -->
            </span>

            <a href="/auths/login-form" class="text-decoration-none">로그인</a> |
            <a href="/members/my-page" class="text-decoration-none">마이페이지</a>
        </div>
    </body>
</html>

 

보이는가

위에 보면 주석으로 1 ~ 3 까지 표시를 해두었다.

해당 부분은 Thymeleaf를 사용하여 Spring Security 기능을 사용한다고 볼 수 있다.

이렇게, Thymeleaf를 사용하여 Spring Security 기능을 쓰고 싶다면!

그리고 혹시 해당 부분에서 문제가 생긴다면!! 

예를 들어, 인증된 유저에게만 보여야 하는 태그 부분이 아무에게나 다 보인다거나

아니면 authentication.name 에서 name을 찾을 수 없다고 하든지 같은 것들 말이다.

 

아래의 것들만 확인을 해보면 해결이 될것이다.

 

build.gradle

plugins {
	id 'org.springframework.boot' version '2.7.2'
	id 'io.spring.dependency-management' version '1.0.12.RELEASE'
	id 'java'
}

group = 'com.codestates'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-validation'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:3.1.0'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	compileOnly 'org.projectlombok:lombok'
	runtimeOnly 'com.h2database:h2'
	annotationProcessor 'org.projectlombok:lombok'
	implementation 'org.mapstruct:mapstruct:1.5.2.Final'
	annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.2.Final'
	implementation 'com.google.code.gson:gson'


	implementation 'org.springframework.boot:spring-boot-starter-security'
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
}

tasks.named('test') {
	useJUnitPlatform()
}

위에 보면 

dependencies { } 영역에서

한 3,4 줄 띄어져 있는 부분의 의존성들 부분이다.

implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'

1번째 녀석 : 해당 프로젝트에서 Spring Security를 사용하기 위한 의존성

2번째 녀석 : 해당 프로젝트에서 Thymeleaf를 사용하기 위한 의존성

3번째 녀석 : Thymeleaf에서 Spring Security를 사용하기 위함 의존성 (이거 안해주면 위에서 예를 든 문제 상황들이 발생)

 

html 부분

위에 있는 HTML 문서 부분에서 아래 부분만 유념하면 된다.

<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

위와 같이 꼭! 꼭! xml name space를 추가한다.