zoukankan      html  css  js  c++  java
  • shiro学习笔记:授权管理

    shiro授权管理,通过realm进行授权,shiro框架通过realm与数据库连接,因此登录,授权,角色获取等与数据库有关的都在数据库中获得;

    授权就是给某个用户授予某个权限,当用户访问某一资源时,发送请求,判断当前用户有没有对当前资源的访问权限,如果有就放行,如果没有,报异常:

    public class MyRealm extends AuthorizingRealm {
    
        @Autowired
        private UserMapper userMapper;
        /*授权方法*/
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    //        获取用户名
            String username = principals.getPrimaryPrincipal().toString();
    //        模拟从数据库中获取到的权限,拥有add与list权限
            Set<String> permissions = new HashSet<>();
            permissions.add("emp:add");
            permissions.add("emp:list");
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            info.setStringPermissions(permissions);
            return info;
        }
    
        /*认证方法,在之前继承的方法只需要重写认证方法,现在加入了权限,可以继承AuthorizingRealm类,将认证预授权写在一个方法里*/
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    
            UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
            String username = usernamePasswordToken.getUsername();
    //        从数据库获取密码
            String truePassword = userMapper.getPasswordByUsername(username);
            ByteSource salt = ByteSource.Util.bytes(username);
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, truePassword,salt,this.getName());
            return info;
        }
    }

    controller中为方法分配权限,规定方法访问需要具备的权限

    package com.zs.controller;
    
    import org.apache.shiro.authz.annotation.RequiresPermissions;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    @Controller
    @RequestMapping("/emp")
    @ResponseBody
    public class EmpController {
        /**
         * 为请求添加权限,什么样的权限可以访问该资源
         * @RequiresPermissions("emp:add"):为当前方法添加emp:add权限,只有拥有emp:add权限才能访问该方法
         * @return
         */
        @RequestMapping("/add")
        @RequiresPermissions("emp:add")
        public String test1() {
            return "ok";
        }
    
        /**
         * 在realm的授权方法中,为给用户授权emp:update,因此无法访问本路径资源
         * @return
         */
        @RequestMapping("/update")
        @RequiresPermissions("emp:update")
        public String test2() {
            return "ok";
        }
    
        @RequestMapping("/list")
        @RequiresPermissions("emp:list")
        public String test3() {
            return "list";
        }
    
    }

    注意:这里使用注解@RequiresPermissions,在shiro中配置启用注解:

    还要再mvc配置文件中配置添加启用aop注解方式:

    再浏览器发送请求测试;

    除了这种方式,还可以使用shiro的jsp标签来进行验证:

    需要先导入标签库:

    <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
    <%--如果没有登录显示登录连接,如果登录了,显示用户名--%>
    <shiro:authenticated>
        <shiro:principal/>
    </shiro:authenticated>
    <shiro:notAuthenticated>
        <a href="/view/login.jsp">登录</a>
    </shiro:notAuthenticated>
  • 相关阅读:
    人月神话读书笔记
    读人月神话有感
    Codeforces 137D
    Codeforces 1138B
    <WFU暑假训练一> 解题报告
    Codeforces 1250B
    Codeforces 1038D
    Codeforces 1202D
    Codeforces 87B
    Codeforces 208C
  • 原文地址:https://www.cnblogs.com/Zs-book1/p/11354519.html
Copyright © 2011-2022 走看看