Shiro权限支持多种情况验证

dep_wangsr
RuoYi 5 years ago committed by Limy
parent ef2d1476f1
commit 5aed48ae57

@ -1,8 +1,14 @@
package com.ruoyi.framework.web.service; package com.ruoyi.framework.web.service;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.security.PermissionUtils; import com.ruoyi.common.utils.StringUtils;
/** /**
* RuoYi js thymeleaf * RuoYi js thymeleaf
@ -12,38 +18,206 @@ import com.ruoyi.common.utils.security.PermissionUtils;
@Service("permission") @Service("permission")
public class PermissionService public class PermissionService
{ {
private static final Logger log = LoggerFactory.getLogger(PermissionService.class);
/** 没有权限hidden用于前端隐藏按钮 */
public static final String NOACCESS = "hidden";
private static final String ROLE_DELIMETER = ",";
private static final String PERMISSION_DELIMETER = ",";
/**
* hiddenBoolean使isPermitted
*
* @param permission
* @return
*/
public String hasPermi(String permission) public String hasPermi(String permission)
{ {
return isPermittedOperator(permission) ? "" : "hidden"; return isPermitted(permission) ? StringUtils.EMPTY : NOACCESS;
}
/**
* hasPermihiddenBoolean使isLacksPermitted
*
* @param permission
* @return
*/
public String lacksPermi(String permission)
{
return isLacksPermitted(permission) ? StringUtils.EMPTY : NOACCESS;
}
/**
* hiddenBoolean使hasAnyPermissions
*
* @param permissions PERMISSION_NAMES_DELIMETER
* @return
*/
public String hasAnyPermi(String permissions)
{
return hasAnyPermissions(permissions, PERMISSION_DELIMETER) ? StringUtils.EMPTY : NOACCESS;
} }
/**
* hiddenBoolean使isRole
*
* @param role
* @return
*/
public String hasRole(String role) public String hasRole(String role)
{ {
return hasRoleOperator(role) ? "" : "hidden"; return isRole(role) ? StringUtils.EMPTY : NOACCESS;
}
/**
* hasRolehiddenBoolean使isLacksRole
*
* @param role
* @return
*/
public String lacksRole(String role)
{
return isLacksRole(role) ? StringUtils.EMPTY : NOACCESS;
}
/**
* hiddenBoolean使isAnyRoles
*
* @param roles ROLE_NAMES_DELIMETER
* @return
*/
public String hasAnyRoles(String roles)
{
return isAnyRoles(roles, ROLE_DELIMETER) ? StringUtils.EMPTY : NOACCESS;
} }
/** /**
* *
* *
* @param permission * @param permission
* @return * @return
*/ */
private boolean isPermittedOperator(String permission) public boolean isPermitted(String permission)
{ {
return SecurityUtils.getSubject().isPermitted(permission); return SecurityUtils.getSubject().isPermitted(permission);
} }
/**
* isPermitted
*
* @param permission
* @return
*/
public boolean isLacksPermitted(String permission)
{
return isPermitted(permission) != true;
}
/**
*
*
* @param permissions PERMISSION_NAMES_DELIMETER
* @return
*/
public boolean hasAnyPermissions(String permissions)
{
return hasAnyPermissions(permissions, PERMISSION_DELIMETER);
}
/**
*
*
* @param permissions delimeter
* @param delimeter
* @return
*/
public boolean hasAnyPermissions(String permissions, String delimeter)
{
Subject subject = SecurityUtils.getSubject();
if (subject != null)
{
if (delimeter == null || delimeter.length() == 0)
{
delimeter = PERMISSION_DELIMETER;
}
for (String permission : permissions.split(delimeter))
{
if (permission != null && subject.isPermitted(permission.trim()) == true)
{
return true;
}
}
}
return false;
}
/** /**
* *
* *
* @param role * @param role
* @return * @return
*/ */
private boolean hasRoleOperator(String role) public boolean isRole(String role)
{ {
return SecurityUtils.getSubject().hasRole(role); return SecurityUtils.getSubject().hasRole(role);
} }
/**
* isRole
*
* @param role
* @return
*/
public boolean isLacksRole(String role)
{
return isRole(role) != true;
}
/**
*
*
* @param roles ROLE_NAMES_DELIMETER
* @return
*/
public boolean isAnyRoles(String roles)
{
return isAnyRoles(roles, ROLE_DELIMETER);
}
/**
*
*
* @param roles delimeter
* @param delimeter
* @return
*/
public boolean isAnyRoles(String roles, String delimeter)
{
Subject subject = SecurityUtils.getSubject();
if (subject != null)
{
if (delimeter == null || delimeter.length() == 0)
{
delimeter = ROLE_DELIMETER;
}
for (String role : roles.split(delimeter))
{
if (subject.hasRole(role.trim()) == true)
{
return true;
}
}
}
return false;
}
/** /**
* *
* *
@ -52,6 +226,26 @@ public class PermissionService
*/ */
public Object getPrincipalProperty(String property) public Object getPrincipalProperty(String property)
{ {
return PermissionUtils.getPrincipalProperty(property); Subject subject = SecurityUtils.getSubject();
if (subject != null)
{
Object principal = subject.getPrincipal();
try
{
BeanInfo bi = Introspector.getBeanInfo(principal.getClass());
for (PropertyDescriptor pd : bi.getPropertyDescriptors())
{
if (pd.getName().equals(property) == true)
{
return pd.getReadMethod().invoke(principal, (Object[]) null);
}
}
}
catch (Exception e)
{
log.error("Error reading property [{}] from principal of type [{}]", property, principal.getClass().getName());
}
}
return null;
} }
} }

Loading…
Cancel
Save