shiro进行权限控制的四种方式

2023-01-10 08:19:21

我们使用shiro进行权限控制 有以下几种方式
1. URL拦截权限控制:基于filter过滤器实现
我们在spring配置文件中配置shiroFilter时配置

/css/ = anon /js/ = anon /images/ = anon /validatecode.jsp = anon /login.jsp = anon /userActionlogin.action = anon /pagebasestaff.action = perms\["staff-list"\] / = authc

2. 方法注解权限控制:基于代理技术实现
我们在代码方法上用注解声明调用该方法需要什么权限。
首先要在spring配置文件中进行声明开启shiro注解:

\*

然后在方法上声明:
@RequiresPermissions(“staff-delete”)
//执行这个方法,需要当前用户具有staff-delete这个权限
public String deleteBatch(){
staffService.deleteBatch(ids);
return LIST;
}
*

3. 页面标签权限控制:页面表签技术实现
首先要在jsp页面进入表签:
<%@ taglib uri=“http://shiro.apache.org/tags” prefix=“shiro” %>
然后包裹权限控制的内容
<shiro:hasPermission name=“Permission”>
xxxxxxxxxxxxxxxx
</shiro:hasPermission>

4.代码级别权限控制:
public String edit(){

Subject subject = SecurityUtils.getSubject();
subject.checkPermission(“staff-edit”);

Staff staff = staffService.findById(model.getId());
staff.setName(model.getName());
staff.setTelephone(model.getTelephone());
staff.setHaspda(model.getHaspda());
staff.setStandard(model.getStandard());
staff.setStation(model.getStation());
staffService.update(staff);
return LIST;
}

总结:
使用shiro进行权限控制时 这四种方法并不是进行单一的使用,是相互结合的使用从而完整的进行权限控制。

一.在自定义的realm中进行权限控制

在applicationContext.xml文件中添加 /areaAction_pageQuery.action = perms[“area”]

复制代码

/css/\* = anon /images/\* = anon /js/\*\* = anon /validatecode.jsp\* = anon /userAction\_login.action = anon /areaAction\_pageQuery.action = perms\["area"\] /\*\* = authc

复制代码

此时访问areaAction_pageQuery.action是页面不会查询到数据,须要为用户授权

在自定义realm中为用户授权

复制代码

@Override
/**
* 授权方法
*/
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//为用户授权,只需将用户的权限添加info中即可
info.addStringPermission(“area”);
return info;
}

复制代码

二.使用shiro的方法注解为用户授权

1.在spring配置文件applicationContext.xml中配置开启shiro注解支持

复制代码

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
    <!-- 强制使用cglibdaili -->
    <property name="proxyTargetClass" value="true"></property>
</bean>
<!-- 配置切面 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"></property>
</bean>

复制代码

2.配置事物注解,强制使用cglib代理

<tx:annotation-driven proxy-target-class=“true” transaction-manager=“transactionManager” />

3.在service上配置注解

复制代码

1 @RequiresPermissions(“courier:delete”)
2 public void deleteBatch(String ids) {
3 //判断是否为空
4 if(StringUtils.isNoneBlank(ids)){
5 String[] idsArrays = ids.split(“,”);
6 for (String id : idsArrays) {
7 Integer courierid = Integer.parseInt(id);
8 dao.deleteCourier(courierid);
9 }
10 }
11 }

复制代码

三.使用shiro标签进行权限控制

1.在jsp页面中引入shiro标签库

<%@ taglib prefix=“shiro” uri=“http://shiro.apache.org/tags” %>

2.在页面中使用标签

复制代码

<%@ page language=“java” contentType=“text/html; charset=utf-8”
pageEncoding=“utf-8”%>
<%@ taglib prefix=“shiro” uri=“http://shiro.apache.org/tags” %>

Insert title here 看到内容就说明你已经认证成功了!
<br>

<!-- 判断当前用户是否拥有指定的权限 -->
<shiro:hasPermission name="area">
    <input value="这是判断权限的按钮">
</shiro:hasPermission>

<br>

<!-- 判断当前用户是否拥有指定的角色 -->
<shiro:hasRole name="admin">
    <input value="这是判断角色的按钮">
</shiro:hasRole>
  • 作者:普通网友
  • 原文链接:https://blog.csdn.net/m0_67402026/article/details/124023410
    更新时间:2023-01-10 08:19:21