How do I add Thymeleaf SpringSecurityDialect to spring boot

安稳与你 提交于 2019-12-30 09:41:48

问题


In the configuration of my template engine I would like to add SpringSecurityDialect() like:

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

However eclipse is telling me:

The type org.thymeleaf.dialect.IExpressionEnhancingDialect cannot be resolved. It is indirectly referenced from required .class files

What does this mean and how can I fix it?

In pom.xml I have:

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

回答1:


It means that org.thymeleaf.extras:thymeleaf-extras-springsecurity4 has a dependency to org.thymeleaf:thymeleaf as you can see in the link to the repo above. Apparently you haven't provided this dependency. The class IExpressionEnhancingDialect is there. You can resolve that by adding the dependency to your project.

Since this may get a bit complicated... I'm also playing around with Spring Boot, spring security and the security dialect for thymeleaf (plus spring data with h2). Here are my gradle dependencies for reference, they may help you somehow:

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")
}

Note that I want to use thymeleaf 3 instead of 2, that is why there are some extra unpleasant tweaks in my configuration.

EDIT: The version of thymeleaf-extras-springsecurity4 should be the same as thymeleaf.version as suggested in the other answer.




回答2:


As @Lachezar already answered, you have to add those missing dependencies. But the specified version with ext['thymeleaf.version'] = '3.0.0.RELEASE should be the same as in the compile dependencies so you better use ext['thymeleaf.version'] = '3.0.1.RELEASE'.

Furthermore, please not that it is enough to just specify a bean for the security dialect without providing a bean for the template engine. With Thymeleaf on the classpath, it will automatically recognize that the bean is an instance of IDialect and adds it directly to the dialects:

@Bean
public SpringSecurityDialect springSecurityDialect() {
    return new SpringSecurityDialect();
}


来源:https://stackoverflow.com/questions/42033368/how-do-i-add-thymeleaf-springsecuritydialect-to-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!