07 保护页面元素和方法

前面我们已经了解到在xml中通过配置的方式对访问页面或者api进行不同层次的鉴权。这一节我们了解一下shiro中对页面元素和类中方法的访问鉴权。

1、前提约束

2、方法保护的操作步骤

  • 确保spring-mvc.xml或者applicationContext-shiro.xml中包含以下内容:
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
        <property name="proxyTargetClass" value="true" />
    </bean>
    <!--shiro生命周期-->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

注意:如果这三个bean是与父容器一起加载,则相关注解要放在service层;如果是与子容器一起加载,则相关注解要放在controller层。本文中的例子是按后者加载。

  • 在controller层中加入以下方法:
    @RequestMapping(value = "/user/query", method = RequestMethod.GET)
    @ResponseBody
    @RequiresRoles("user")
    public JSONObject queryList() {
       JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 200);
        return jsonObject;
    }


    @RequestMapping(value = "/user/add", method = RequestMethod.GET)
    @RequiresPermissions("user:add")
    public String userAdd(User user) {
        return "redirect:/user/query";
    }
  • 在MyRealm.java中重写授权方法,内容如下:
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //下面这一句话保证了可以访问userAdd接口,这里的“user:add”是硬编码,实际使用中要去数据库查询
        authorizationInfo.addStringPermission("user:add");
        //下面这一句话保证了可以访问queryList接口,这里的“admin”是硬编码,实际使用中要去数据库查询
        authorizationInfo.addRole("admin");
        //如果方法上面的注解已经表明需要某种权限或者角色,但授权逻辑没有赋予该权限或角色,则进入未授权逻辑。
        return authorizationInfo;
    }

3 页面元素保护的操作步骤

  • 创建一个index.jsp,内容如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<body>
<h2>Hello World!</h2>
<shiro:hasRole name="admin">
    只有登录者具有admin角色,我才会显示
</shiro:hasRole>
<br/>
<shiro:hasRole name="system">
    只有登录者具有system角色,我才会显示
</shiro:hasRole>
<br/>
怎么都会显示
<br/>
<shiro:hasPermission name="user:add">
    只有登录者具有user:add权限,我才会显示
</shiro:hasPermission>
<br/>
<shiro:hasPermission name="user:delete">
    只有登录者具有user:delete,我才会显示
</shiro:hasPermission>

</body>
</html>
  • 在MyRealm.java中重写授权方法,内容如下:
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //下面这一句话保证了可以展示需要“user:add”权限的元素,这里的“user:add”是硬编码,实际使用中要去数据库查询
        authorizationInfo.addStringPermission("user:add");
        //下面这一句话保证了可以展示需要“admin”角色的元素,,这里的“admin”是硬编码,实际使用中要去数据库查询
        authorizationInfo.addRole("admin");
        //如果标签已经表明需要某种权限或者角色,但授权逻辑没有赋予该权限或角色,则不显示该元素。
        return authorizationInfo;
    }

以上就是通过shiro对应页面元素和类中方法进行鉴权访问的过程。

posted @ 2020-03-18 20:35  张力的程序园  阅读(163)  评论(0)    收藏  举报