springboot 自定義權(quán)限標(biāo)簽(tld),在freemarker引用操作
第一步:引入jar包
<dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2.1-b03</version> </dependency>
第二步:自定義標(biāo)簽類
import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletRequest; import org.jasig.cas.client.authentication.AttributePrincipal; import org.springframework.web.servlet.tags.RequestContextAwareTag; import com.goodidea.sso.dto.PrivilegesDto; import com.goodidea.sso.dto.ResourcesDto; import com.goodidea.sso.service.PrivilegesService; /** * * @ClassName: PrivilegeTag * @Description: 權(quán)限標(biāo)簽類 * @author lsg * @date 2017年9月12日 下午5:36:01 * */ public class PrivilegeTag extends RequestContextAwareTag{ private static final long serialVersionUID = 534416848523276042L; private String menuAlias; private String priAlias; public String getMenuAlias() { return menuAlias; } public void setMenuAlias(String menuAlias) { this.menuAlias = menuAlias; } public String getPriAlias() { return priAlias; } public void setPriAlias(String priAlias) { this.priAlias = priAlias; } @Override protected int doStartTagInternal() { // TODO Auto-generated method stub boolean result = false; try { HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal(); Map<String, Object> attributes = principal.getAttributes(); String username=(String) attributes.get("username"); PrivilegesService privilegesService= (PrivilegesService)this.getRequestContext().getWebApplicationContext().getBean("privilegesServiceImpl"); Set<ResourcesDto> dto = privilegesService.findResourcesByUsername(username); for (ResourcesDto resourcesDto : dto) { if(this.menuAlias.equals(resourcesDto.getAlias())){ for (PrivilegesDto pdto : resourcesDto.getPrivileges()) { if(this.priAlias.equals(pdto.getAlias())){ result = true; } } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); result =false; } return result? EVAL_BODY_INCLUDE : SKIP_BODY; } }
第三步:創(chuàng)建tld標(biāo)簽,放入在web-inf下
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>privilege</shortname> <tag> <name>privilege</name> <tagclass>com.goodidea.sso.core.PrivilegeTag</tagclass> <bodycontent>empty</bodycontent> <!--這里如果設(shè)為empty,則無body--> <attribute> <name>menuAlias</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>priAlias</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
第四:頁面引用
<#assign p=JspTaglibs["/WEB-INF/tld/privilege.tld"] />
注意tld,如果不在web.xml上進(jìn)行引入的話,就放在web-inf下,要不然會報(bào)找不到tld資源異常
補(bǔ)充知識:Springboot項(xiàng)目 freemarker 引入shiro 標(biāo)簽
springboot集成shiro權(quán)限過程略過
一、添加maven 依賴
<dependency> <groupId>net.mingsoft</groupId> <artifactId>shiro-freemarker-tags</artifactId> <version>0.1</version> </dependency>
二、注入FreeMarkerConfigurer,未指定templateLoaderPath時遇到過跳轉(zhuǎn)到頁面404問題
@Bean public FreeMarkerConfigurer freeMarkerConfigurer() throws IOException, TemplateException { FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); freeMarkerConfigurer.setTemplateLoaderPath("classpath:templates/"); freemarker.template.Configuration configuration = freeMarkerConfigurer.createConfiguration(); configuration.setDefaultEncoding("UTF-8"); //這里可以添加其他共享變量 比如sso登錄地址 configuration.setSharedVariable("shiro", new ShiroTags()); freeMarkerConfigurer.setConfiguration(configuration); return freeMarkerConfigurer; }
三、shiro標(biāo)簽
1、session中讀取登錄人信息
<@shiro.principal/>
2、帶有l(wèi)oginName屬性的對象轉(zhuǎn)換為Prinipal后保存在session
<shiro:principal property="loginName" />
3、帶有l(wèi)oginName屬性的Json(個人使用的是FashJson對象)轉(zhuǎn)換為Prinipal后保存在session,使用freemarker標(biāo)簽處理Json
<#assign loginInfo><@shiro.principal/></#assign>
<#assign data=loginInfo?eval>
用戶:${data.loginName!""}
4、其他shiro標(biāo)簽使用
<@shiro.hasPermission name="權(quán)限編碼">
...
</@shiro.hasPermission>
以上這篇springboot 自定義權(quán)限標(biāo)簽(tld),在freemarker引用操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java 自動裝箱與拆箱的實(shí)現(xiàn)原理
本篇文章主要介紹了詳解Java 自動裝箱與拆箱的實(shí)現(xiàn)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04java實(shí)現(xiàn)附件預(yù)覽(openoffice+swftools+flexpaper)實(shí)例
本篇文章主要介紹了java實(shí)現(xiàn)附件預(yù)覽(openoffice+swftools+flexpaper)實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10Java中的MapStruct知識點(diǎn)總結(jié)
這篇文章主要介紹了Java中的MapStruct知識點(diǎn)總結(jié),MapStruct是一個Java注解處理器,用于生成類型安全的映射代碼,它可以自動處理源對象和目標(biāo)對象之間的映射,減少了手動編寫重復(fù)的映射代碼的工作量,需要的朋友可以參考下2023-10-10JDK都出到14了,你有什么理由不會函數(shù)式編程(推薦)
這篇文章主要介紹了JDK都出到14了,你有什么理由不會函數(shù)式編程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Java基于Lock的生產(chǎn)者消費(fèi)者模型示例
這篇文章主要介紹了Java基于Lock的生產(chǎn)者消費(fèi)者模型,結(jié)合實(shí)例形式分析了java基于鎖機(jī)制的生產(chǎn)者消費(fèi)者模型相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2018-08-08MyBatis中使用分頁插件PageHelper實(shí)現(xiàn)分頁功能
分頁是經(jīng)常使用的功能,本文主要介紹了Mybatis中處理特殊SQL處理邏輯,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06