springboot 自定义权限标签(tld),在freemarker引用操作
作者:Kirk_ 发布时间:2023-11-23 06:20:15
第一步:引入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>
来源:https://blog.csdn.net/cutterwolf/article/details/78085808


猜你喜欢
- 1.使用ASCII码判断您可以使用ASCII码来进行判断字符串中的内容是否为纯数字。步骤如下:先判断字符串是否为空的情况,保证代码运行的稳定
- 前言项目中检测人脸图片是否合法的功能,之前用的是百度的人脸识别接口,由于成本高昂不得不寻求替代方案。什么是opencv?OpenCV是一个基
- 第一步:在d盘新建android文件夹,在此文件夹中再建三个子文件夹,Android Studio 、 Android_SDK、Androi
- Java虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验、转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这个
- 一、什么是冒泡排序冒泡排序的英文是bubble sort,它是一种基础的交换排序。说到冒泡是不是就想起了快乐肥宅水呢?汽水中有许多小小的水泡
- 1、mapper.xml文件中的sql语句不提示1.1 首先,alt+enter,出现如下窗口随后的窗口选择这样在如下窗口中会增加一个upd
- 背景知识同步、异步、阻塞、非阻塞首先,这几个概念非常容易搞混淆,但NIO中又有涉及,所以总结一下。同步:API调用返回时调用者就知道操作的结
- 1.基本介绍SpringBoot 支持的 webServer: Tomcat, Jetty, or UndertowSpringBoot 应
- 比如定义了一个错误的枚举类型public enum eErrorDetailCode : int &nbs
- springboot初始化器新建项目项目结构idea工具类中初始化本地git仓库选择当前项目目录即可工具类由VCS变成了Gitadd 到缓存
- 预加载bean在springBoot启动过程中就完成创建加载在AbstractApplicationContext的refresh方法中//
- 本文实例讲述了Android编程实现仿QQ发表说说,上传照片及弹出框效果。分享给大家供大家参考,具体如下:代码很简单,主要就是几个动画而已,
- 前言WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在 WebSocket API 中,浏
- Random类介绍Random类一个用于产生 伪随机 数字的类。这里的伪随机表示有随机性但是可以基于算法模拟出随机规律。Random类的构造
- 源代码版本 : spring-webmvc-5.1.4.RELEASE概述PathMatcher是Spring的一个概念模型接口,该接口抽象
- 对Jpa Entity关系映射中mappedBy的理解mappedBy 单向关系不需要设置该属性,双向关系必须设置,避免双方都建立外键字段数
- 自动注入和@Autowire@Autowire不属于自动注入!注入方式(重要)在Spring官网上(文档),定义了在Spring中的注入方式
- 承蒙各位厚爱,我们一起每天进步一点点!(鼠标选中空白处查看答案)1、以下不属于构造方法特征的是()正确答案: D构造方法名与类名相同构造方法
- 一、对象的综述面向对象编程(OOP)具有多方面的吸引力。对管理人员,它实现了更快和更廉价的开发与维护过程。对分析与设计人员,建模处理变得更加
- 依赖<dependency> <groupId>com.baomidou</groupId> <a