1

在我当前的 spring-boot 项目中,我对这个 html 代码有一个看法:

<div id="navbar" class="collapse navbar-collapse">
  <ul class="nav navbar-nav navbar-right" sec:authorize="isAuthenticated()">
     ...
  </ul>
  <ul class="nav navbar-nav navbar-right" sec:authorize="isAnonymous()">
     ...
  </ul>
</div>

但是当我执行应用程序时,显然sec:authorize没有评估标签,因为这两个部分都在显示。

我以这种方式在 application.properties 文件中配置 thymeleaf:

# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

我的百里香配置类是这样实现的:

@Configuration
public class Thymeleaf {
  @Bean
  public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine engine  =  new SpringTemplateEngine();
    final Set<IDialect> dialects = new HashSet<IDialect>();
    dialects.add( new SpringSecurityDialect() );
    engine.setDialects( dialects );

    return engine;
  }
}

任何人都可以指出我在这里缺少什么?

4

1 回答 1

2

确保为 Thymeleaf 添加以下依赖项:

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    <version>2.1.2.RELEASE</version>
    <scope>compile</scope>
</dependency>

还要确保将其添加到<html>

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"

我也不认为这Set<IDialect>是必要的(如果我错了,请纠正我),你可以写成:

Bean
public SpringTemplateEngine templateEngine() {
   SpringTemplateEngine engine = new SpringTemplateEngine();
   engine.setTemplateResolver(templateResolver());
   engine.addDialect(new SpringSecurityDialect());
   return engine;
}
于 2016-05-21T23:25:29.627 回答