springboot 自定義許可權標籤(tld),在freemarker引用操作
第一步:引入jar包
<dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2.1-b03</version> </dependency>
第二步:自定義標籤類
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: 許可權標籤類 * @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; } }
第三步:建立tld標籤,放入在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> <!--這裡如果設為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上進行引入的話,就放在web-inf下,要不然會報找不到tld資源異常
補充知識:Springboot專案 freemarker 引入shiro 標籤
springboot整合shiro許可權過程略過
一、新增maven 依賴
<dependency> <groupId>net.mingsoft</groupId> <artifactId>shiro-freemarker-tags</artifactId> <version>0.1</version> </dependency>
二、注入FreeMarkerConfigurer,未指定templateLoaderPath時遇到過跳轉到頁面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標籤
1、session中讀取登入人資訊
<@shiro.principal/>
2、帶有loginName屬性的物件轉換為Prinipal後儲存在session
<shiro:principal property="loginName" />
3、帶有loginName屬性的Json(個人使用的是FashJson物件)轉換為Prinipal後儲存在session,使用freemarker標籤處理Json
<#assign loginInfo><@shiro.principal/></#assign>
<#assign data=loginInfo?eval>
使用者:${data.loginName!""}
4、其他shiro標籤使用
<@shiro.hasPermission name="許可權編碼">
...
</@shiro.hasPermission>
以上這篇springboot 自定義許可權標籤(tld),在freemarker引用操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。