環境
spring boot 2.7.2
gradleプロジェクト
エラーログ
SpringSecurityとThymeleafを連携させ、認証情報をViewで表示させようとしたところエラー。
There was an unexpected error (type=Internal Server Error, status=500).
'org.thymeleaf.web.IWebExchange org.thymeleaf.context.IWebContext.getExchange()'
java.lang.NoSuchMethodError: 'org.thymeleaf.web.IWebExchange org.thymeleaf.context.IWebContext.getExchange()'
at org.thymeleaf.extras.springsecurity5.util.Spring5VersionSpecificUtility.isWebMvcContext(Spring5VersionSpecificUtility.java:80)
at org.thymeleaf.extras.springsecurity5.util.SpringVersionSpecificUtils.isWebMvcContext(SpringVersionSpecificUtils.java:118)
at org.thymeleaf.extras.springsecurity5.util.SpringSecurityContextUtils.getAuthenticationObject(SpringSecurityContextUtils.java:127)
at org.thymeleaf.extras.springsecurity5.auth.AuthUtils.getAuthenticationObject(AuthUtils.java:102)
at org.thymeleaf.extras.springsecurity5.dialect.processor.AuthenticationAttrProcessor.doProcess(AuthenticationAttrProcessor.java:66)
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314)
at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205)
...
解決
原因 : 依存関係でバージョンを指定しているため。
Spring Securityのバージョンのそれぞれに対応した、thymeleaf-extras-springsecurityのライブラリを使用しなくてはならないです。
そのバージョンの整合性の関係で発生したエラーだと考えました。
以下のように記述するとエラーが解消できました。
build.gradle
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 group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
以下のようにthymeleaf-extras-springsecurityのバージョンを指定するとエラーが起こりました。
build.gradle
//バージョンを指定している
implementation group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5', version: '3.1.0.M1'
バージョンの指定をなくせばエラーは解消できました。
build.gradle
implementation group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5'