如何将Thymeleaf SpringSecurityDialect添加到Spring Boot


问题内容

在我的模板引擎的配置中,我想添加SpringSecurityDialect():

@Bean
public TemplateEngine templateEngine() {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.addDialect(new SpringSecurityDialect());
    engine.setEnableSpringELCompiler(true);
    engine.setTemplateResolver(templateResolver());
    return engine;
}

但是蚀告诉我:

无法解析类型org.thymeleaf.dialect.IExpressionEnhancingDialect。从所需的.class文件间接引用它

这是什么意思,我该如何解决?

pom.xml我有:

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>

问题答案:

就像上面的仓库中的链接所示,这意味着org.thymeleaf.extras:thymeleaf-extras- springsecurity4依赖org.thymeleaf:thymeleaf。显然您没有提供此依赖性。教室IExpressionEnhancingDialect在那里。您可以通过将依赖项添加到项目中来解决该问题。

因为这可能会变得有点复杂…我还在玩Spring Boot,spring
security和thymeleaf的安全方言(以及带有h2的spring数据)。这是我的gradle依赖项以供参考,它们可能会以某种方式帮助您:

ext['thymeleaf.version'] = '3.0.1.RELEASE'
ext['thymeleaf-layout-dialect.version'] = '2.0.0'

dependencies {
    compile("org.springframework.boot:spring-boot-devtools")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.1.RELEASE")

    compile("com.h2database:h2")
}

请注意,我想使用thymeleaf 3而不是2,这就是为什么我的配置中还有一些其他不愉快的调整的原因。

编辑: 的版本thymeleaf-extras-springsecurity4thymeleaf.version与其他答案中建议的相同。