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)簽類(lèi)
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)簽類(lèi)
* @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,則無(wú)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>
第四:頁(yè)面引用
<#assign p=JspTaglibs["/WEB-INF/tld/privilege.tld"] />
注意tld,如果不在web.xml上進(jìn)行引入的話,就放在web-inf下,要不然會(huì)報(bào)找不到tld資源異常
補(bǔ)充知識(shí):Springboot項(xiàng)目 freemarker 引入shiro 標(biāo)簽
springboot集成shiro權(quán)限過(guò)程略過(guò)
一、添加maven 依賴
<dependency> <groupId>net.mingsoft</groupId> <artifactId>shiro-freemarker-tags</artifactId> <version>0.1</version> </dependency>
二、注入FreeMarkerConfigurer,未指定templateLoaderPath時(shí)遇到過(guò)跳轉(zhuǎn)到頁(yè)面404問(wèn)題
@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屬性的對(duì)象轉(zhuǎn)換為Prinipal后保存在session
<shiro:principal property="loginName" />
3、帶有l(wèi)oginName屬性的Json(個(gè)人使用的是FashJson對(duì)象)轉(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引用操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springboot整合freemarker的踩坑及解決
- springboot整合freemarker代碼自動(dòng)生成器
- Springboot整合Freemarker的實(shí)現(xiàn)詳細(xì)過(guò)程
- SpringBoot2 整合FreeMarker實(shí)現(xiàn)頁(yè)面靜態(tài)化示例詳解
- Springboot整合freemarker 404問(wèn)題解決方案
- SpringBoot2.2.X用Freemarker出現(xiàn)404的解決
- 構(gòu)建SpringBoot+MyBatis+Freemarker的項(xiàng)目詳解
- SpringBoot整合FreeMarker的過(guò)程詳解
相關(guān)文章
Spring Cloud-Feign服務(wù)調(diào)用的問(wèn)題及處理方法
Feign 是一個(gè)聲明式的 REST 客戶端,它用了基于接口的注解方式,很方便實(shí)現(xiàn)客戶端配置。接下來(lái)通過(guò)本文給大家介紹Spring Cloud-Feign服務(wù)調(diào)用,需要的朋友可以參考下2021-10-10
強(qiáng)烈推薦IDEA提高開(kāi)發(fā)效率的必備插件
這篇文章主要介紹了強(qiáng)烈推薦IDEA提高開(kāi)發(fā)效率的必備插件,文中有非常詳細(xì)的圖文示例,對(duì)想要提高企業(yè)開(kāi)發(fā)效率的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
MyBatis-Plus實(shí)現(xiàn)邏輯刪除的示例代碼
本文主要介紹了MyBatis-Plus實(shí)現(xiàn)邏輯刪除的示例代碼,就是通過(guò)邏輯判斷的手段表示該條數(shù)據(jù)已刪除,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Java 日期時(shí)間工具包–java.time的使用
這篇文章主要介紹了Java 日期時(shí)間工具包–java.time的使用,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-04-04
Java中的Comparable接口與Comparator接口區(qū)別解析
文章介紹了Java中的Comparable接口和Comparator接口,Comparable接口定義了一個(gè)compareTo方法,用于比較對(duì)象的順序,實(shí)現(xiàn)Comparable接口的類(lèi)可以提供自然排序規(guī)則,詳細(xì)介紹了Java中的Comparable接口與Comparator接口區(qū)別,感興趣的朋友一起看看吧2025-02-02
Mybatis Plus查詢時(shí)sql字段名大小寫(xiě)報(bào)錯(cuò)的解決
這篇文章主要介紹了Mybatis Plus查詢時(shí)sql字段名大小寫(xiě)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

