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
猜你喜欢
- 前几天网上突然出现流言:某东发生数据泄露12G,最终某东在一篇声明中没有否认,还算是勉强承认了吧,这件事对于一般人有什么影响、应该怎么做已经
- 前言春节要到了,看惯了前端各种小游戏,确实做得很好,很精致。但是我也要为后端程序员稍微做一点贡献,做一款java版本的【年兽大作战】。这个游
- 面试中会经常遇到手撕代码的情况,而求TopK的是经常遇到的题目。下面我就用Java来实现。主要通过两种方法实现,快排思想以及堆排序的思想,两
- SlidingDrawer效果想必大家也见到过,它就是1.5模拟器上进入应用程序列表的效果。下面是截图一、简介 SlidingDr
- 1.使用的是maven项目,添加依赖<!-- mybatis-plus begin --> <depend
- 有时候,我们在同一个activity里面有很多fragment,在横竖屏的时候,有些fragment要求重新加载数据,有些不需要,如何简单控
- 声明:下面的实例全部在linux下尝试,window下未尝试。有兴趣者可以试一下。文章针c初学者。c语言的强符号和弱符号是c初学者经常容易犯
- 1、Java内存模型 Java虚拟机在执行程序时把它管理的内存分为若干数据区域,这些数据区域分布情况如下图所示:程序计数器:一块较小内存区域
- 一、一级缓存二级缓存的概念解释(1)一级缓存就是Session级别的缓存,一个Session做了一个查询操作,它会把这个操作的结果放在一级缓
- 通过本篇文章主要给大家讲解了在JAVA开发中Servlet3.0异步处理遇到的问题以及处理办法,以下是具体内容:Servlet 3.0 开始
- IDEA 新手使用手册1 简介IDEA的全称是IntelliJ IDEA,这是一个java编程语言开发的集成环境。IDEA的每一个方面都是为
- mybatis plus新增(insert)数据获取主键id在我们执行insert操作的时候,往往会需要拿到新插入数据的主键id做下一步操作
- 注意是maven的webapp:选择maven下一步下一步。maven下载过慢在setting中加入镜像。 我也有疑问这是什么鬼格式,但是证
- 一、Monkey 是什么?Monkey 就是SDK中附带的一个工具。二、Monkey 测试的目的?:该工具用于进行压力测试。 然后开发人员结
- 闲来无事,用C++做了一个图书管理系统,主要有借书、还书、图书管理、用户管理等功能,主要用到的技术有容器和文件,以及类的封装#include
- java中的LIST在删除时,一般会用list.remove(o); 但这样往往会出现问题,先来看下面的这段代码:package com.d
- 最近的项目中要实现一个聊天的功能,类似于斗鱼TV的聊天室功能,与服务器端人商量后决定用WebSocket来做,但是在这之前我只知道Socke
- jar包运行时提示jar中没有主清单属性解决办法在pom文件中添加<build> &n
- 初次安装Android Studio,遇到了不少问题,这是其中的一个,分享如下,同时求各位dalao关注一下啦((*^__^*) )使用不同
- 重新认识 Java 的 System.in以前也写过不少命令行的程序,处理文件时总需要通过参数指定路径,直到今天看资料时发现了一种我自己从来