如何将Thymeleaf SpringSecurityDialect添加到Spring Boot

use*_*209 5 template-engine spring-mvc spring-security thymeleaf

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

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

但是蚀告诉我:

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

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

pom.xml我有:

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

Tom*_*Tom 9

正如@Lachezar已经回答的那样,您必须添加那些缺少的依赖项。但是指定的版本ext['thymeleaf.version'] = '3.0.0.RELEASE应与编译依赖项中的版本相同,因此最好使用 ext['thymeleaf.version'] = '3.0.1.RELEASE'

此外,请不要仅仅为安全性方言指定一个bean,而不必为模板引擎提供bean就足够了。将Thymeleaf放在类路径上,它将自动识别Bean是IDialect的实例,并将其直接添加到方言中:

@Bean
public SpringSecurityDialect springSecurityDialect() {
    return new SpringSecurityDialect();
}
Run Code Online (Sandbox Code Playgroud)


Lac*_*lev 4

这意味着它org.thymeleaf.extras:thymeleaf-extras-springsecurity4具有依赖关系,org.thymeleaf:thymeleaf正如您在上面的存储库链接中看到的那样。显然你还没有提供这种依赖关系。班级IExpressionEnhancingDialect就在那里。您可以通过将依赖项添加到项目中来解决该问题。

因为这可能会变得有点复杂......我也在玩 Spring Boot、spring security 和 thymeleaf 的安全方言(加上带有 h2 的 spring data)。以下是我的 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")
}
Run Code Online (Sandbox Code Playgroud)

请注意,我想使用 thymeleaf 3 而不是 2,这就是为什么我的配置中有一些额外的令人不快的调整。

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