在我当前的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没有对标记进行评估,因为这两个部分都在显示。
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; } }
任何人都可以指出我在这里想念的东西吗?
确保为Thymeleaf添加以下依赖项:
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> <version>2.1.2.RELEASE</version> <scope>compile</scope> </dependency>
还要确保将其添加到 <html>
<html>
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
我也不认为这Set<IDialect>是必要的(如果我错了,请纠正我),您可以将其编写为:
Set<IDialect>
Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine engine = new SpringTemplateEngine(); engine.setTemplateResolver(templateResolver()); engine.addDialect(new SpringSecurityDialect()); return engine; }