1. 程式人生 > >thymeleaf模板引擎和shiro框架的整合

thymeleaf模板引擎和shiro框架的整合

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>

引入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,官方文件中有詳細的說明和簡單例子,對照文件,學習會更快。

相關推薦

thymeleaf模板引擎shiro框架整合

shiro許可權框架,前端驗證是為jsp設計的,其中的tag只能用於jsp系列的模板引擎。最近專案使用了thymeleaf作為前端模板引擎,使用HTML檔案,沒法引入shiro的tag lib,此時如果要使用shiro的話,可以引入 thymeleaf-extras-shi

springMVCShiro框架整合使用簡單示例

一、目錄結構 首先是目錄結構如圖: 二、pom.xml檔案 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan

jdbcTemplate Thymeleaf模板引擎 查詢 到模板賦值例子

blog urn pre 模板引擎 tex ras body del xmlns 二、 jdbcTemplate 和 Thymeleaf模板引擎 最簡單輸出例子 控制器代碼 @GetMapping(value = "/test2") public String

Spring Boot 2.0 整合Thymeleaf 模板引擎

reporting 配置信息 name www. title 建模 type 引擎 suffix 本節將和大家一起實戰Spring Boot 2.0 和thymeleaf 模板引擎 1. 創建項目 2. 使用Spring Initlizr 快速創建Spring Boot

Spring Boot 整合 Thymeleaf 模板引擎

原文地址:Spring Boot 整合 Thymeleaf 模板引擎 Thymeleaf 是適用於 Web 和獨立環境的現代伺服器端 Java 模板引擎。 Thymeleaf 的主要目標是為開發帶來優雅的模板 - Thymeleaf 模板對於瀏覽器來說這些非HTML標準屬性在渲染

spring boot2.0一看就會的超詳細快速入門(六)-整合Thymeleaf模板引擎

1.理解Thymeleaf 1.1 Thymeleaf是一個適用於Web和獨立環境的伺服器端java模板引擎,能夠處理HTML、XML、JavaScript、CSS甚至純檔案,常見類似的工具有JSP、Freemarker等 1.2 Thymeleaf遵循的是一個自然模板的概念,主要目標是提供一種優雅且高度

瞭解微服務以及Springboot整合mybaits+thymeleaf模板引擎實戰D經驗分享

前言: 首先肯定大家最想知道的還是Spring boot到底是幹啥的?其實我也是初學者,雖然接觸時間不長,但是也想把自己的理解和大家做一個交流。 簡單來說,Springboot就是一個更加靈活,配置起來更簡單,廣泛應用於微服務的一個開源框架。那大家肯定還會問,到

JavaEE開發之SpringBoot整合MyBatis以及Thymeleaf模板引擎

上篇部落格我們聊了《》,從上篇部落格的內容我們不難看出SpringBoot的便捷。本篇部落格我們繼續在上篇部落格的基礎上來看一下SpringBoot是如何引入和使用MyBatis和Thymeleaf的。在之前的部落格中我們提到過Hibernate,今天部落格所引入的Mybatis所扮演的角色與Hibernat

SpringBoot學習(三)從yml檔案中獲取資料Thymeleaf 模板引擎

SpringBoot現在基本上都是使用application.yml來配置專案中的一些配置條件,在springboot中還可以通過從yml檔案中將yml中的資料直接讀取出來。 一、從yml檔案中獲取資料 在yml配置下面的資訊 book: au

springboot thymeleafshiro標籤整合

這裡用的是 thymeleaf 2.x版本的 新增依賴 <dependency> <groupId>com.github.theborakompanioni</groupId> <artifactId>t

Springboot系列:Springboot與Thymeleaf模板引擎整合基礎教程(附原始碼)

前言 由於在開發My Blog專案時使用了大量的技術整合,針對於部分框架的使用和整合的流程沒有做詳細的介紹和記錄,導致有些朋友用起來有些吃力,因此打算在接下來的時間裡做一些基礎整合的介紹,當然,可能也不會特別的基礎,但是原始碼會開放給大家,方便大家學習,此次的原始碼地址為s

Spring Boot整合Thymeleaf模板引擎

一、Thymeleaf 模板介紹 Spring Boot 推薦使用Thymeleaf 來代替傳統開發中的JSP,那麼什麼是Thymeleaf 模板引擎呢?下面就來簡單的介紹一下。 1.1什麼是Thymeleaf Thymeleaf 是一款用於渲

SpringMVC框架Shiro框架整合與簡單搭建

這篇部落格中沒有什麼高深的技術,只是一個簡單的Shiro框架的搭建...因為之前一直沒找到正確的搭建方法、或是因為報錯搭建失敗...所以我寫下了這篇。希望可以給有需要的人幫助。 我用的編輯工具是Idea。 首先,我們從三個方面來認識Shiro。 1:什麼是Shiro? Sh

Spring MVC擴展 SSM框架整合

運行 步驟 請求參數 body 控制 ice 系統工具 表示 produces 1.使用@ResponseBody 實現數據輸出 DAO層、Service層 改造Controller層 改造View層 部署運行 2.JSON數據的傳遞處理 改造Controller層

spring boot Thymeleaf模板引擎 最簡單輸出例子

test leaf att map 控制器 輸出 span blog hello spring boot Thymeleaf模板引擎 最簡單輸出例子 控制器代碼如下: @GetMapping(value = "/test")public String test(Mo

UndertowServer+SpringMVC+Thymeleaf模板引擎構建輕量級的web項目

加載 輕量 contain har rom 視圖解析 catch 輕量級 工作 這兩周需要寫一個頁面來請求另一個服務中的接口,服務器采用了超輕量級的undertow,模板引擎采用的是Thymeleaf,在尋找頁面資源位置這個地方難住了我。下面分享一下,這方面的代碼。 Spr

SpringBoot集成Thymeleaf模板引擎

lis arr type www mode html中 object erro pub 傳統的JSP+JSTL組合是已經過去了,Thymeleaf是Java服務端的模板引擎,與傳統的JSP不同,Thymeleaf可以使用瀏覽器直接打開,因為可以忽略掉拓展屬性,相當於打開原生

【Java】Spring MVC 擴展SSM框架整合

nco span con odin typealias eal nag key ping 開發web項目通常很多地方需要使用ajax請求來完成相應的功能,比如表單交互或者是復雜的UI設計中數據的傳遞等等。對於返回結果,我們一般使用JSON對象來表示,那麽Spring MVC

spring boot: thymeleaf模板引擎使用

sage homepage 順序 object tomcat fig component art format spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依賴 <!-- 添加thymeleaf的依賴 --

Spring Boot使用Thymeleaf模板引擎渲染web檢視

Spring Boot開發Web應用 原創   2018-04-03  宗野   Spring Boot Spring Boot快速入門中我們完成了一個簡單的RESTfu