Thymeleaf template engine and shiro framework integration, thymeleafshiro
Shiro permission framework. Front-end verification is designed for jsp, where tags can only be used for jsp series template engines. Recently, the project used thymeleaf as the front-end template engine. Using HTML files, we cannot introduce the shiro tag lib. If we want to use shiro, the thymeleaf-extras-shiro.jar extension package can be introduced to achieve shiro front-end verification.
Add the following dependency to pom. xml:
<dependency><groupId>com.github.theborakompanioni</groupId><artifactId>thymeleaf-extras-shiro</artifactId><version>1.0.2</version></dependency>
For the version, you can query in the maven Repository: http://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro/1.0.2
After introducing the jar package, we need to briefly set the thymeleaf engine:
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver" /> <property name="additionalDialects"> <set> <bean class="at.pollux.thymeleaf.shiro.dialect.ShiroDialect"/> </set> </property></bean>
Or, if Java code configuration is used:
public SpringTemplateEngine templateEngine() {SpringTemplateEngine templateEngine = new SpringTemplateEngine();templateEngine.setTemplateResolver(templateResolver()); Set<IDialect> additionalDialects = new HashSet<IDialect>();additionalDialects.add(new ShiroDialect());templateEngine.setAdditionalDialects(additionalDialects);return templateEngine;}
Then we can use the thymeleaf shiro tag on the page for front-end verification.
Shiro has limited tags and does not have the hasAnyPermission tag. In my previous article, "Custom shiro tag" has a version of jsp tag. In fact, if thymeleaf is used here, you can also customize hasAnyPermission of shiro.
Open the downloaded thymeleaf-extras-shiro.jar and there will be a shiro-dialect.xml file, which defines the shiro attribute of thymeleaf and we can also make a simple expansion based on the example below.
Here we are actually expanding thymeleaf. The official documentation provides detailed instructions and simple examples. The study will be faster by referring to the document.