Thymeleaf template engine and shiro framework integration, thymeleafshiro

Source: Internet
Author: User

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.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.