springboot 实现自定义权限标签(tld)在freemarker引用

发布时间:2020-11-03 16:47:55 作者:Leah
来源:亿速云 阅读:442

这期内容当中小编将会给大家带来有关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&#63; EVAL_BODY_INCLUDE : SKIP_BODY;
  }
}


第三步:创建tld标签,放入在web-inf下

springboot 实现自定义权限标签(tld)在freemarker引用

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<!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&#63;eval>

用户:${data.loginName!""}

4、其他shiro标签使用

<@shiro.hasPermission name="权限编码">
...
</@shiro.hasPermission>

上述就是小编为大家分享的springboot 实现自定义权限标签(tld)在freemarker引用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 如何自定义JSP标签
  2. JSP中怎么自定义标签

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot freemarker

上一篇:利用PHP实现限制域名访问

下一篇:详解C++中的单例

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》