I've searched the web for a solution, but I can't figure this out.
I'm using Spring Boot with Maven and have the
spring-boot-starter-thymeleaf dependency in my pom.xml file.
I'm trying to show the logout button, using Thymeleaf, only if the user is logged in, but this code doesn't seem to work:
It keeps showing this error:
There was an unexpected error (type=Internal Server Error, status=500).
Exception evaluating SpringEL expression: "#authorization.expression('isAuthenticated()')"
and the line number where I have the "if" condition with thymeleaf.
How can I resolve this ?
解决方案
You could use the sec:authorize="isAuthenticated()", but you might have to add the dependencies etc for this. I used:
org.thymeleaf.extras
thymeleaf-extras-springsecurity4
2.1.2.RELEASE
compile
Also, make sure to add the SpringSecurityDialect so that isAuthenticated and other similar expressions can be evaluated by Spring. ex:
Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
engine.addDialect(new SpringSecurityDialect());
return engine;
}
To the html page itself, add the following to the tag to make sure the sec: tag is recognized:
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"