Shiro自定义标签

书写java类

  1. 继承org.apache.shiro.web.tags.PermissionTag类
  2. 重写showTagBody方法

代码

package com.dilinbao.core.shiro.tag;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.tags.PermissionTag;

/**   
 * @ClassName:  HasAnyPermissionTag   
 * @Description:拥有其中一个权限即可 
 * @author: rick
 * @date:   2018年1月23日 下午4:27:44  
 */
public class HasAnyPermissionTag extends PermissionTag{

    /**
     * 序列化
     */
    private static final long serialVersionUID = -6121655720312024938L;

    /**
     * 
     * @Title:  HasAllPermissionTag   
     * @Description:   构造方法
     * @param:    
     * @throws
     */
    public HasAnyPermissionTag(){

    }

    /**
     * 
     * 

Title: showTagBody

*

Description: 验证权限 * @param permissions 就是标签中name的字符串 * @return * @see org.apache.shiro.web.tags.PermissionTag#showTagBody(java.lang.String) */ @Override protected boolean showTagBody(String permissions) { boolean hasAnyPermission = false; // 获取所有权限 String[] permissionArray = permissions.split(","); // 获取当前用户 Subject currentUser = getSubject(); if(currentUser != null){ // 遍历权限 for(String permission : permissionArray){ // 判断当前用户是否拥有此权限 if(currentUser.isPermitted(permission)){ hasAnyPermission = true; break; } } } return hasAnyPermission; } }

package com.dilinbao.core.shiro.tag;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.tags.PermissionTag;

/**   
 * @ClassName:  HasAllPermissionTag   
 * @Description:拥有所有的权限才可以
 * @author: rick
 * @date:   2018年1月23日 下午4:52:44   
 */
public class HasAllPermissionTag extends PermissionTag{

    /**
     * 序列化
     */
    private static final long serialVersionUID = -6121655720312024938L;

    /**
     * 
     * @Title:  HasAllPermissionTag   
     * @Description:   构造方法
     * @param:    
     * @throws
     */
    public HasAllPermissionTag(){

    }

    /**   
     * 

Title: showTagBody

*

Description: 验证权限 * @param arg0 * @return * @see org.apache.shiro.web.tags.PermissionTag#showTagBody(java.lang.String) */ @Override protected boolean showTagBody(String permissions) { boolean hasAllPermission = false; // 获取所有权限 String[] permissionArray = permissions.split(","); // 获取当前用户 Subject currentUser = getSubject(); if(currentUser != null){ // 遍历权限 for(String permission : permissionArray){ // 判断当前用户是否拥有此权限 if(currentUser.isPermitted(permission)){ hasAllPermission = true; }else{ hasAllPermission = false; break; } } } return hasAllPermission; } }

配置shiros.tld

<tag>
    
    <name>hasAnyPermissionname>
    
    <tag-class>com.dilinbao.core.shiro.tag.HasAnyPermissionTagtag-class>
    <body-content>JSPbody-content>
    <description>has any permission is okdescription>
    <attribute>
      <name>namename>
      <required>truerequired>
      <rtexprvalue>truertexprvalue>
    attribute>
  tag>

  <tag>
    <name>hasAllPermissionname>
    <tag-class>com.dilinbao.core.shiro.tag.HasAllPermissionTagtag-class>
    <body-content>JSPbody-content>
    <description>only has all permission is okdescription>
    <attribute>
      <name>namename>
      <required>truerequired>
      <rtexprvalue>truertexprvalue>
    attribute>
  tag>

你可能感兴趣的:(JavaEE,Shiro)