본문 바로가기
IT개발/이슈관리

스프링부트(Spring Boot) 타임리프(Thymeleaf) sec:authorize 적용 안됨

by 시간기억자 2025. 1. 24.
반응형

진행중인 스프링 부트 프로젝트에 스프링 시큐리티를 적용 후 thymeleaf 내 'sec:authorize'를 사용해서 로그인 사용자와 비로그인 사용자에게 서로 다른 메뉴를 보여주려고 하였는데 전혀 적용되지 않았다.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <title>website</title>
</head>
<body>
<div id="header" class="wrap_header" th:fragment="header">
    <div class="top_header">
        <div class="inner_top">
            <ul class="list_top">
                <li><a href="/login" th:href="@{/login}" class="link_top">로그인</a></li>
                <li sec:authorize="isAnonymous()">
                    <a href="/roles" th:href="@{/roles}" class="link_top">roles</a>
                </li>
                <li><a href="/signup" th:href="@{/users/create}" class="link_top">회원가입</a></li>
                <li sec:authorize="isAuthenticated()">환영합니다</li>
            </ul>
        </div>
    </div>
dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-security'// 시큐리티
	implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5:3.1.1.RELEASE' //시큐리티를 사용 하기위해 사용
	implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
	implementation 'org.springframework.security:spring-security-crypto' // 암호화
	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	annotationProcessor 'org.projectlombok:lombok'
	// Spring Data JPA
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	runtimeOnly 'mysql:mysql-connector-java:8.0.32'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

 

'sec:authorize' 이게 적용되지 않다보니 로그인 사용자에게만 보여야할 '환영합니다' 메세지가 비로그인 사용자에게도 보였다.

 

아무리봐도 의존성 주입에는 문제가 없었고, thymeleaf에서 html 태그에 security 관련 설정을 해준것도 문제가 없었다.

그리고 GPT에게 물어봐도 마땅한 답이 안나왔다.

 

그러다가 아래 블로그 글을 발견했다.

 

https://velog.io/@fprh13/Spring-Boot-%ED%83%80%EC%9E%84%EB%A6%AC%ED%94%84Thymeleaf-secauthorize-%EC%A0%81%EC%9A%A9-%EC%95%88%EB%90%A8-%ED%95%B4%EA%B2%B0

 

[Spring Boot] 타임리프(Thymeleaf) sec:authorize 적용 안됨 해결

최근 API 개발만 해오다가 MVC 개발할 일이 생겨서 오랜만에 타임리프를 사용하는데 매우 상당히 뚝딱 거린다....적용이 안된다 !!sec관련 디펜던시 누락에 대한 글을 확인했다.나는 타임리프로 개

velog.io

 

 

위 블로그 글을 보고 바로 스프링 시큐리티 버전을 6으로 변경 하였다.

그랬더니 바로 적용!!

implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6' //시큐리티를 사용 하기위해 사용

반응형

댓글