【问题标题】:How do I add Thymeleaf SpringSecurityDialect to spring boot如何将 Thymeleaf SpringSecurityDialect 添加到 Spring Boot
【发布时间】:2017-06-21 08:43:05
【问题描述】:

在我的模板引擎的配置中,我想添加 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>

【问题讨论】:

    标签: spring-mvc spring-security thymeleaf template-engine


    【解决方案1】:

    这意味着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-springsecurity4 的版本应与另一个答案中建议的 thymeleaf.version 相同。

    【讨论】:

    • 谢谢。现在可以了。我必须将版本 (3.0.1.RELEASE) 添加到 thymeleaf-extras-springsecurity4 并将 thymeleaf 版本从 3.0.2.RELEASE 更改为 3.0.1.RELEASE
    【解决方案2】:

    正如@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();
    }
    

    【讨论】:

    • 谢谢,汤姆。我会修正我的答案(当然还有我的玩具项目),以便版本相同。
    猜你喜欢
    • 2017-09-10
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    相关资源
    最近更新 更多