thymeleaf模板引擎和shiro架構的整合,thymeleafshiro

來源:互聯網
上載者:User

thymeleaf模板引擎和shiro架構的整合,thymeleafshiro

shiro許可權架構,前端驗證是為jsp設計的,其中的tag只能用於jsp系列的模板引擎。最近項目使用了thymeleaf作為前端模板引擎,使用HTML檔案,沒法引入shiro的tag lib,此時如果要使用shiro的話,可以引入 thymeleaf-extras-shiro.jar這個拓展包來曲線實現shiro的前端驗證。

在pom.xml中加入如下依賴:

<dependency><groupId>com.github.theborakompanioni</groupId><artifactId>thymeleaf-extras-shiro</artifactId><version>1.0.2</version></dependency>
關於版本,可以在maven倉庫中查詢:http://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro/1.0.2

引入jar包後,我們要簡單設定一下thymeleaf的引擎:

<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>


或者如果使用的是Java代碼配置的話:

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;}

然後就可以在頁面中使用thymeleaf化的shiro標籤來進行前端驗證了。

shiro本身的標籤有限,沒有hasAnyPermission標籤,我在之前的一篇文章中《自訂shiro標籤》有寫過jsp標籤的版本,其實,在這裡如果使用的是thymeleaf,也可以自訂拓展一下shiro的hasAnyPermission。

將下載下來的thymeleaf-extras-shiro.jar開啟,會有一個shiro-dialect.xml的檔案,這裡定義了thymeleaf的shiro屬性,我們也可以根據這裡面的例子進行簡單拓展。
這裡其實是在拓展thymeleaf,官方文檔中有詳細的說明和簡單例子,對照文檔,學習會更快。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.