在Spring Boot中出现java.lang.NoSuchMethodError: ‘org.thymeleaf.web.IWebExchange org.thymeleaf.context.IWebContext.getExchange()’错误
环境
Spring Boot 2.7.2是一个基于Gradle的项目。
错误日志
尝试将Spring Security和Thymeleaf进行集成,将认证信息显示在视图中时遇到错误。
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库。
我认为这是由于版本兼容性引起的错误。
按照下面的方式编写可以解决错误。
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的版本时发生了错误。
//バージョンを指定している
implementation group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5', version: '3.1.0.M1'
如果去掉版本指定,错误就可以解决。
implementation group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5'