sec:授权不在spring-boot项目上进行评估

时间:2016-05-21 19:55:38

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

在我目前的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;
  }
}

任何人都可以指出我在这里缺少的东西吗?

1 个答案:

答案 0 :(得分: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;
}