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

 更新时间:2020年09月18日 13:12:16   作者:Kirk_  
这篇文章主要介绍了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引用操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot2线程池定义使用方法解析

    SpringBoot2线程池定义使用方法解析

    这篇文章主要介绍了SpringBoot2线程池定义使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Java的Volatile实例用法及讲解

    Java的Volatile实例用法及讲解

    在本篇文章里小编给大家整理了关于Java的Volatile知识点相关内容,有需要的朋友们可以跟着学习下。
    2019-09-09
  • 通过实例解析spring对象生命周期

    通过实例解析spring对象生命周期

    这篇文章主要介绍了通过实例解析spring对象生命周期,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Spring Aop 如何获取参数名参数值

    Spring Aop 如何获取参数名参数值

    这篇文章主要介绍了Spring Aop 如何获取参数名参数值的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Spring中存取Bean的相关注解举例详解

    Spring中存取Bean的相关注解举例详解

    这篇文章主要给大家介绍了关于Spring中存取Bean的相关注解,在没有使用注解获取对象之前,我们需要在配置文件中通过添加bean来将对象存储到Spring容器中,这对于我们来说是比较麻烦的,需要的朋友可以参考下
    2023-10-10
  • IDEA 配合 Dockerfile 部署 SpringBoot 工程的注意事项

    IDEA 配合 Dockerfile 部署 SpringBoot 工程的注意事项

    这篇文章主要介绍了IDEA 配合 Dockerfile 部署 SpringBoot 工程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • java如何更改数据库中的数据

    java如何更改数据库中的数据

    这篇文章主要介绍了java如何更改数据库中的数据,修改数据库是数据库操作必不可少的一部分,使用Statement接口中的excuteUpdate()方法可以修改数据表中的数据,感兴趣的朋友跟随小编一起看看吧
    2021-11-11
  • 基于Java实现Redis多级缓存方案

    基于Java实现Redis多级缓存方案

    这篇文章主要介绍了Redis多级缓存方案分享,传统缓存方案、多级缓存方案、JVM本地缓存,举例说明这些方案,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-03-03
  • spring是如何实现声明式事务的

    spring是如何实现声明式事务的

    这篇文章主要介绍了spring是如何实现声明式事务的,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • SpringBoot2.x 整合 AntiSamy防御XSS攻击的简单总结

    SpringBoot2.x 整合 AntiSamy防御XSS攻击的简单总结

    本文主要对SpringBoot2.x集成AntiSamy防御XSS攻击进行简单总结,其中SpringBoot使用的2.4.5版本,通过示例代码给大家介绍的非常详细,需要的朋友参考下吧
    2021-08-08

最新评论