【问题标题】:sec:authorize not being evaluated on spring-boot projectsec:authorize 未在 spring-boot 项目上进行评估
【发布时间】:2016-09-18 22:34:36
【问题描述】:

在我当前的 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配置类是这样实现的:

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

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

【问题讨论】:

    标签: spring spring-security spring-boot thymeleaf dialect


    【解决方案1】:

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

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

    还要确保将其添加到&lt;html&gt;

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

    我也不认为Set&lt;IDialect&gt;是必要的(如果我错了请纠正我),你可以写成:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      相关资源
      最近更新 更多