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引用操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: 6大直播带货话术文案模板,人人都适用