shiro框架的三种权限控制方式

以下展示shiro框架的权限控制方式

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

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

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
      <!-- 注入shiro框架核心对象,安全管理器 -->
      <property name="securityManager" ref="securityManager"/>
<!--private String loginUrl;登录页面 private String successUrl;登录成功后跳转页面 private String unauthorizedUrl;权限不足时的提示页面-->
      <property name="loginUrl" value="/login.html"/>
      <property name="successUrl" value="/index.html"/>
      <property name="unauthorizedUrl" value="/unauthorized.html"/>
      <!-- 指定URL拦截规则 -->
      <property name="filterChainDefinitions">
                <!--authc:代表shiro框架提供的一个过滤器,这个过滤器用于判断当前用户是否已经完成认证, 如果当前用户已经认证,就放行,如果当前用户没有认证,跳转到登录页面 anon:代表shiro框架提供的一个过滤器,允许匿名访问-->
       <value> /css/* = anon /images/* = anon /js/** = anon /validatecode.jsp* = anon /userAction_login.action = anon/areaAction_pageQuery.action = perms["area"] /** = authc
      </value>
    </property>
</bean>

 此时访问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注解支持

<!-- 基于spring自动代理方式为service创建代理对象,实现权限控制 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
      <!-- 强制使用cglibdaili -->
      <property name="proxyTargetClass" value="true"></property>
</bean>
<!-- 配置切面 -->
<beanclass="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上配置注解

     @RequiresPermissions("courier:delete")
      public void deleteBatch(String ids) {
          //判断是否为空
          if(StringUtils.isNoneBlank(ids)){
              String[] idsArrays = ids.split(",");
              for (String id : idsArrays) {
                  Integer courierid = Integer.parseInt(id);
                  dao.deleteCourier(courierid);
              }
         }
     }

三.使用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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <!-- 判断当前用户是否已经认证,已认证就可以看到标签中的内容 -->
    <shiro:authenticated>
        看到内容就说明你已经认证成功了!
    </shiro:authenticated>
    <br>
    <!-- 判断当前用户是否拥有指定的权限 -->
    <shiro:hasPermission name="area">
        <input value="这是判断权限的按钮">
    </shiro:hasPermission>
    <br>
    <!-- 判断当前用户是否拥有指定的角色 -->
    <shiro:hasRole name="admin">
        <input value="这是判断角色的按钮">
    </shiro:hasRole>
</body>
</html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空宇航员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值