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下

springboot 自定义权限标签(tld),在freemarker引用操作


<?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

标签:springboot,权限,freemarker
0
投稿

猜你喜欢

  • 布隆过滤器面试如何快速判断元素是否在集合里

    2022-10-17 15:55:19
  • Android权限HaloPermission详细使用

    2021-08-18 07:14:46
  • Android XRecyclerView实现多条目加载

    2021-10-15 07:32:21
  • IDEA2020.2.3中创建JavaWeb工程的完整步骤记录

    2022-03-15 05:16:02
  • Java使用Tess4J实现图像识别方式

    2022-10-07 19:24:11
  • java Swing基础教程之图形化实例代码

    2023-06-30 03:15:20
  • Java Socket 编程详解

    2022-06-13 12:06:57
  • springboot表单提交之validator校验

    2023-05-16 23:19:17
  • Mybatis初始化知识小结

    2023-11-01 13:59:27
  • Java学习-打印1-1000以内的水仙花数代码实例

    2023-05-02 03:58:45
  • Android仿google now效果的呼吸按钮

    2023-06-17 07:58:02
  • MybatisPlus多表连接查询的问题及解决方案

    2023-11-25 22:09:06
  • Java中的双重检查(Double-Check)详解

    2023-02-23 21:05:23
  • 解决IDEA中 Ctrl+ALT+V这个快捷键无法使用的情况

    2022-02-27 07:51:36
  • 平衡二叉树AVL操作模板

    2022-04-23 15:31:41
  • SpringBoot项目集成xxljob实现全纪录

    2023-01-18 17:26:21
  • 详解Java读取Jar中资源文件及示例代码

    2021-07-12 11:18:52
  • java中set接口使用方法详解

    2023-03-15 06:10:12
  • C# 构造函数如何调用虚方法

    2023-05-12 00:08:57
  • 浅谈java封装

    2023-05-05 22:48:20
  • asp之家 软件编程 m.aspxhome.com