sec:authorize 未在 spring-引导项目上进行评估

sec:authorize not being evaluated on spring-boot project

在我当前的 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

我的 thymeleaf 配置 class 是这样实现的:

@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;
  }
}

谁能指出我在这里遗漏了什么?

确保为 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;
}