在SpringBoot2中使用Apache Shiro實現登錄身份驗證和訪問權限控制

作為一個Apache Shiro小白,最近跟著“純潔的微笑”和“冷豪”等的博客,學習了一下Apache Shiro。將自己的一些簡單理解記錄下來,希望對你有所幫助。

Apache Shiro在我工作項目中主要用于登陸身份驗證訪問權限控制。工作項目用的是SpringMVC框架,最近學習SpringBoot2,我就在SpringBoot2中來驗證一下Apache Shiro。以下Apache Shiro簡稱Shiro。

一、Shiro登陸架構

下面是Shiro的用戶登陸架構圖,我們根據箭頭來看一下流程。

image.png

1、Token:使用用戶的登錄信息創建令牌

UsernamePasswordToken token = new UsernamePasswordToken(username, password, true);

我們要先通過用戶名和密碼,生成一個token,token是一個用戶令牌,用于在登陸的時候,Shiro來驗證用戶是否有合法的身份。

2、Subject:執行登陸動作(login)

Subject subject = SecurityUtils.getSubject(); // 獲取Subject單例對象
subject.login(token); // 登陸

再通過Subject來執行登陸操作,將token發送給Security Manager,讓他來驗證這個token。Subject中文翻譯是主題。你可以理解為它是一個用戶,是User的抽象概念。

3、Realm:自定義代碼實現登陸身份驗證和訪問權限控制

先來看看Realm,你從上圖可以看出,Realm在Shiro方框的外面。圖片很形象,因為這一部分恰恰是需要我們自己去實現的。需要我們來設計如何驗證登錄用戶的身份(role),和這個用戶是否具有訪問某個URL的權限(permission)。前者使用AuthenticationInfo(驗證)實現,后者使用AuthorizationInfo(授權)實現。

4、Security Manager:Shiro架構的核心

Security Manager,是Shiro架構的核心,簡單來說,它根據我們自定義的Realm,去完成驗證和授權工作


如果這部分沒有看懂,建議先根據下面的“與SpringBoot2集成”部分,搭建一個demo,在項目中直觀體驗一下了,再回來看。

二、與SpringBoot2集成

注:以下內容是根據“純潔的微笑”大神的《springboot整合shiro-登錄認證和權限管理》一文,將其中SpringBoot1.5升級到2.1,針對2.1做了相應修改,同時針對一些知識點延伸學習。博文地址:http://www.ityouknow.com/springboot/2017/06/26/springboot-shiro.html

建議你手動搭建一個demo,這樣能更深入了解。

pom依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--web核心-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--thymeleaf模板-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--HTML掃描器和標簽補償器,補充thymeleaf對html的嚴格檢驗-->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
        <!--Apache Shiro-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.4.0</version>
        </dependency>
        <!--使用Spring Data JPA和Hibernate-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--用于MySQL的JDBC Type 4驅動程序-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--熱部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!--可使用注解自動生成getter、setter等方法-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
        </dependency>
</dependencies>

<!--為使用熱部署,配置<build></build>-->
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
</build>

配置文件

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springboot_shiro?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: 12345678
    driver-class-name: com.mysql.cj.jdbc.Driver

  thymeleaf:
    cache: false #禁用模板引擎編譯的緩存結果。由熱部署來實現,更改代碼后,使用Ctrl+F9(IDEA)更新
    mode: LEGACYHTML5 #避免thymeleaf對html文件的嚴格校驗(如檢查標簽必須對稱等)
  
  #使用jpa技術,運行實體代碼自動生成數據表
  jpa:
    database: mysql
    show-sql: true
    hibernate:
      ddl-auto: update
    properties:
        hibernate:
          dialect: org.hibernate.dialect.MySQL5Dialect

server:
  port: 9090

數據庫設計

使用基于角色的訪問控制(Role-Based Access Control)---RBAC 來實現數據庫設計,用戶依賴角色,角色依賴權限。這樣設計結構清晰,管理方便。建立三張表:user_info,sys_role,sys_permission。使用sys_user_role關聯用戶和角色,使用sys_role_permission關聯角色和權限,不使用外鍵。

使用jpa技術,運行實體代碼自動生成數據表。

用戶信息實體。@Getter、@Setter注解用于提供讀寫屬性。因為有getCredentialsSalt(),所以不使用@Data注解。

@Entity
@Getter
@Setter
public class UserInfo implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)//GenerationType.IDENTITY避免生成hibernate_sequence表
    private Integer uid;
    @Column(unique = true)
    private String username;//帳號
    private String name;//名稱(昵稱或者真實姓名,不同系統不同定義)
    private String password; //密碼;
    private String salt;//加密密碼的鹽
    private byte state;//用戶狀態,0:創建未認證(比如沒有激活,沒有輸入驗證碼等等)--等待驗證的用戶 , 1:正常狀態,2:用戶被鎖定.

    @ManyToMany(fetch = FetchType.EAGER)//立即從數據庫中進行加載數據;
    @JoinTable(name = "SysUserRole", joinColumns = {@JoinColumn(name = "uid")}, inverseJoinColumns = {@JoinColumn(name = "roleId")})
    private List<SysRole> roleList;// 一個用戶具有多個角色

    /**
     * 密碼鹽,重新對鹽重新進行了定義,用戶名+salt,這樣就更加不容易被破解
     *
     * @return
     */
    public String getCredentialsSalt() {
        return this.username + this.salt;
    }
}

角色實體。使用@Data注解,為類提供讀寫屬性, 此外還提供了 equals()、hashCode()、toString() 方法。上面用戶信息實體和角色實體會根據@JoinTable注解生成sys_user_role表。

@Entity
@Data
public class SysRole {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id; // 編號
    private String role; // 角色標識程序中判斷使用,如"admin",這個是唯一的:
    private String description; // 角色描述,UI界面顯示使用
    private Boolean available = Boolean.FALSE; // 是否可用,如果不可用將不會添加給用戶

    // 用戶 - 角色關系定義;
    @ManyToMany
    @JoinTable(name = "SysUserRole", joinColumns = {@JoinColumn(name = "roleId")}, inverseJoinColumns = {@JoinColumn(name = "uid")})
    private List<UserInfo> userInfos;// 一個角色對應多個用戶
    
    //角色 -- 權限關系:多對多關系;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "SysRolePermission", joinColumns = {@JoinColumn(name = "roleId")}, inverseJoinColumns = {@JoinColumn(name = "permissionId")})
    private List<SysPermission> permissions;
}

權限實體。同理,角色實體和權限實體,通過@JoinTable注解生成sys_role_permission表

@Entity
@Data
public class SysPermission {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;//主鍵.
    private String name;//名稱.
    @Column(columnDefinition = "enum('menu','button')")
    private String resourceType;//資源類型,[menu|button]
    private String url;//資源路徑.
    private String permission; //權限字符串,menu例子:role:*,button例子:role:create,role:update,role:delete,role:view
    private Long parentId; //父編號
    private String parentIds; //父編號列表
    private Boolean available = Boolean.FALSE;

    @ManyToMany
    @JoinTable(name = "SysRolePermission", joinColumns = {@JoinColumn(name = "permissionId")}, inverseJoinColumns = {@JoinColumn(name = "roleId")})
    private List<SysRole> roles;
}

數據庫數據:

INSERT INTO `user_info` (`uid`,`username`,`name`,`password`,`salt`,`state`) VALUES ('1', 'admin', '管理員', 'd3c59d25033dbf980d29554025c23a75', '8d78869f470951332959580424d4bf4f', 0);
INSERT INTO `sys_permission` (`id`,`available`,`name`,`parent_id`,`parent_ids`,`permission`,`resource_type`,`url`) VALUES (1,0,'用戶管理',0,'0/','userInfo:view','menu','userInfo/userList');
INSERT INTO `sys_permission` (`id`,`available`,`name`,`parent_id`,`parent_ids`,`permission`,`resource_type`,`url`) VALUES (2,0,'用戶添加',1,'0/1','userInfo:add','button','userInfo/userAdd');
INSERT INTO `sys_permission` (`id`,`available`,`name`,`parent_id`,`parent_ids`,`permission`,`resource_type`,`url`) VALUES (3,0,'用戶刪除',1,'0/1','userInfo:del','button','userInfo/userDel');
INSERT INTO `sys_role` (`id`,`available`,`description`,`role`) VALUES (1,0,'管理員','admin');
INSERT INTO `sys_role` (`id`,`available`,`description`,`role`) VALUES (2,0,'VIP會員','vip');
INSERT INTO `sys_role` (`id`,`available`,`description`,`role`) VALUES (3,1,'test','test');
INSERT INTO `sys_role_permission` VALUES ('1', '1');
INSERT INTO `sys_role_permission` (`permission_id`,`role_id`) VALUES (2,1);
INSERT INTO `sys_role_permission` (`permission_id`,`role_id`) VALUES (3,2);
INSERT INTO `sys_user_role` (`role_id`,`uid`) VALUES (1,1);

三、配置Shiro

Apache Shiro 核心通過 Filter 來實現,就好像SpringMvc 通過DispachServletqu去實現。

Filter和Interceptor的區別:

Filter是過濾器,Interceptor是攔截器。前者基于回調函數實現,必須依靠容器支持。因為需要容器裝配好整條FilterChain并逐個調用。后者基于代理實現,屬于AOP的范疇。

在Shrio中實現登陸身份驗證和訪問權限控制有三種方式:

  • 1、完全使用注解來實現登陸身份驗證和訪問權限控制
  • 2、完全使用URL配置來實現登陸身份驗證和訪問權限控制
  • 3、使用URL配置來實現登陸身份驗證、使用注解來實現訪問權限控制

第3種方式最靈活,所以用第三種。

1、使用URL配置來實現登陸身份驗證

要實現當用戶在瀏覽器地址訪問項目URL時,Shiro會攔截所有的請求,再根據配置的ShrioFilter過濾器來進行下一步操作。原理:Spring容器會將所有的Filter交給ShiroFilter管理。

@Configuration
public class ShiroConfig {
   
    @Bean
    public ShiroFilterFactoryBean shiroFilter() {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager());
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        // 以下過濾器按順序判斷
        // 配置不會被攔截的鏈接,一般是排除前端文件(anon:指定的url可以匿名訪問)
//        filterChainDefinitionMap.put("/static/**", "anon");
        //配置退出 過濾器,其中的具體的退出代碼Shiro已經替我們實現了
        filterChainDefinitionMap.put("/logout", "logout");
        //authc:所有url都必須認證通過才可以訪問;
        filterChainDefinitionMap.put("/**", "authc");

        //當項目訪問其他沒有通過認證的URL時,會默認跳轉到/login,如果不設置默認會自動尋找Web工程根目錄下的"/login.jsp"頁面
        shiroFilterFactoryBean.setLoginUrl("/login");
        //登錄成功后要跳轉的鏈接
        shiroFilterFactoryBean.setSuccessUrl("/index");
        //當用戶訪問沒有權限的URL時,跳轉到未授權界面
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }
    
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm());
        return securityManager;
    }
    
    @Bean
    public MyShiroRealm myShiroRealm() {
        MyShiroRealm myShiroRealm = new MyShiroRealm();
        myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
    }
}

authc更深層次含義:指定url需要form表單登錄,默認會從請求中獲取username、password等參數并嘗試登錄,如果登錄不了就會跳轉到loginUrl配置的路徑

在Realm中實現AuthenticationInfo(登陸身份驗證)

doGetAuthenticationInfo():用于驗證token的User是否具有合法的身份,即檢驗賬號密碼是否正確,每次用戶登錄的時候都會調用。

public class MyShiroRealm extends AuthorizingRealm {
    @Autowired
    UserService userService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //獲取登錄的用戶名
        String username = (String) authenticationToken.getPrincipal();
        //根據用戶名在數據庫中查找此用戶
        //實際項目中,這里可以根據實際情況做緩存,如果不做,Shiro自己也是有時間間隔機制,2分鐘內不會重復執行該方法
        UserInfo userInfo = userService.findByUsername(username);
        if (userInfo == null) {
            return null;
        }
        //根據salt來驗證token中的密碼是否跟從數據庫查找的密碼匹配,匹配則登錄成功。getName()設置當前Realm的唯一名稱,可自定義
        return new SimpleAuthenticationInfo(
                userInfo,
                userInfo.getPassword(),
                ByteSource.Util.bytes(userInfo.getCredentialsSalt()),//鹽
                getName());
    }
}

驗證密碼原理

先了解兩個算法,散列算法與加密算法。

兩者都是將一個Object變成一串無意義的字符串,不同點是經過散列的對象無法復原,是一個單向的過程。例如,對密碼的加密通常就是使用散列算法,因此用戶如果忘記密碼只能通過修改而無法獲取原始密碼。但是對于信息的加密則是正規的加密算法,經過加密的信息是可以通過秘鑰解密和還原。

在這里,我們將用戶的密碼使用散列算法(MD5)加密后保存到數據庫。加密的時候就使用了salt,salt中文翻譯是鹽,你可以將他看成一個鑰匙。

因為散列算法加密是單項的,不能還原。那我們如何來驗證密碼呢,這時候也需要使用salt。我們將token中的明文密碼,采用生成密文密碼時一樣的方式,通過salt再加密一次,對比兩個加密后的密碼。最后的驗證是SimpleAuthenticationInfo去實現的。

如何散列加密

//newPassword(密文密碼):d3c59d25033dbf980d29554025c23a75
String newPassword = new SimpleHash("MD5",//散列算法:這里使用MD5算法
        "123456",//明文密碼
        ByteSource.Util.bytes("admin8d78869f470951332959580424d4bf4f"),//salt:用戶名 + salt
        2//散列的次數,相當于MD5(MD5(**))
).toHex();

//生成一個32位數的salt
byte[] saltByte = new byte[16];
SecureRandom random = new SecureRandom();
random.nextBytes(saltByte);
String salt = Hex.encodeToString((saltByte));   

如何驗證密碼

配置hashedCredentialsMatcher(憑證匹配器),讓SimpleAuthorizationInfo知道如何驗證密碼:

@Bean
public HashedCredentialsMatcher hashedCredentialsMatcher() {
    HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
    hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法
    hashedCredentialsMatcher.setHashIterations(2);//散列的次數
    return hashedCredentialsMatcher;
}

2、使用注解來實現訪問權限控制

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
    /**
     * 用戶查詢;
     * @return
     */
    @RequestMapping("/userList")
    @RequiresPermissions("userInfo:view")//訪問的權限
    public String userList(){
        return "userInfo";
    }

    /**
     * 用戶添加;
     * @return
     */
    @RequestMapping("/userAdd")
    @RequiresPermissions("userInfo:add")//新增的權限
    public String userAdd(){
        return "userInfoAdd";
    }

    /**
     * 用戶刪除;
     * @return
     */
    @RequestMapping("/userDel")
    @RequiresPermissions("userInfo:del")//刪除的權限
    public String userDel(){
        return "userInfoDel";
    }
}

在Relm中實現AuthorizationInfo(訪問權限控制)

如果項目只需要Apache Shiro用于登陸驗證,那么就不用使用AuthorizationInfo,只需要返回一個null。

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    return null;
}

doGetAuthorizationInfo():當用戶訪問帶有@RequiresPermissions注解的URL時,會調用此方法驗證是否有權限訪問。

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("權限配置-->MyShiroRealm.doGetAuthorizationInfo()");
        UserInfo userInfo = (UserInfo) principalCollection.getPrimaryPrincipal();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        //獲取當前用戶的角色與權限,讓simpleAuthorizationInfo去驗證
        for (SysRole sysRole : userInfo.getRoleList()) {
            simpleAuthorizationInfo.addRole(sysRole.getRole());
            for (SysPermission sysPermission : sysRole.getPermissions()) {
                simpleAuthorizationInfo.addStringPermission(sysPermission.getPermission());
            }
        }
        return simpleAuthorizationInfo;
}

代碼開啟注解

使用Shrio注解,需要在ShrioConfig中使用AuthorizationAttributeSourceAdvisor開啟

@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
}

自定義異常處理

當沒有訪問權限時,會拋出異常,需要自定義異常處理,將沒有權限的異常重定向到403頁面

@Bean
    public SimpleMappingExceptionResolver
    createSimpleMappingExceptionResolver() {
        System.out.println("自定義異常處理");
        SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
        Properties mappings = new Properties();
        mappings.setProperty("UnauthorizedException", "403");//授權異常處理
        resolver.setExceptionMappings(mappings);  // None by default
        resolver.setDefaultErrorView("error");    // No default
        resolver.setExceptionAttribute("ex");     // Default is "exception"
        return resolver;
    }

同理,可以使用@RequiresRoles("admin")注解來驗證角色(身份)

在前端頁面使用Shiro標簽時也會觸發權限控制,請看:http://www.lxweimin.com/p/6786ddf54582

其他兩種驗證和授權請看:https://juejin.im/entry/5ad95ef26fb9a07a9f01185a

也可直接編代碼測試:

Boolean isPermitted = SecurityUtils.getSubject().isPermitted("***");//是否有什么權限
Boolean hasRole = SecurityUtils.getSubject().hasRole("***");//是否有什么角色

登陸實現

前端登陸頁面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
錯誤信息:<h4 th:text="${msg}"></h4>
<form action="" method="post">
    <p>賬號:<input type="text" name="username" value="admin"/></p>
    <p>密碼:<input type="text" name="password" value="123456"/></p>
    <p><input type="submit" value="登錄"/></p>
</form>
</body>
</html>

登陸接口:

@Controller
public class LoginController {

    @RequestMapping("/login")
    public String toLogin(HttpServletRequest request, Map<String, Object> map) {
        System.out.println("HomeController.login()");
        // 登錄失敗從request中獲取shiro處理的異常信息。
        // shiroLoginFailure:就是shiro異常類的全類名.
        String exception = (String) request.getAttribute("shiroLoginFailure");
        System.out.println("exception=" + exception);
        String msg = "";
        if (exception != null) {
            if (UnknownAccountException.class.getName().equals(exception)) {
                System.out.println("UnknownAccountException -- > 賬號不存在:");
                msg = "UnknownAccountException -- > 賬號不存在:";
            } else if (IncorrectCredentialsException.class.getName().equals(exception)) {
                System.out.println("IncorrectCredentialsException -- > 密碼不正確:");
                msg = "IncorrectCredentialsException -- > 密碼不正確:";
            } else if ("kaptchaValidateFailed".equals(exception)) {
                System.out.println("kaptchaValidateFailed -- > 驗證碼錯誤");
                msg = "kaptchaValidateFailed -- > 驗證碼錯誤";
            } else {
                msg = "else >> "+exception;
                System.out.println("else -- >" + exception);
            }
        }
        map.put("msg", msg);
        return "login";
    }

    @RequestMapping({"/","/index"})
    public String index(){
        return "index";
    }
}

你可能發現了,登陸沒有用文章開頭的Subject執行登陸動作,而是直接使用action=""的表單登錄。
為什么action=""呢,這是因為設置了"/**", "authc",當用戶沒有登陸時,所有url(/**)都會被重定向到/login,而action=""或者"/login"將不被攔截,doGetAuthenticationInfo()驗證表單中的賬號密碼。

使用Subject執行登陸動作

那如何使用Subject執行登陸動作呢,需要使用user過濾器。當沒有登錄時,user跟authc一樣,會攔截所有的url。

//user:需要已登錄或“記住我”的用戶才能訪問;
filterChainDefinitionMap.put("/**", "user");

當使用Post請求的/login登陸時,將使用Subject執行登陸動作,doGetAuthenticationInfo()方法驗證

<form action="/login" method="post">
    <p>賬號:<input type="text" name="username" value="admin"/></p>
    <p>密碼:<input type="text" name="password" value="123456"/></p>
    <p><input type="submit" value="登錄"/></p>
</form>

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@RequestParam(value = "username") String userName,
                    @RequestParam(value = "password") String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        return "index";
}

項目源碼

其余未貼出代碼請查看項目源碼https://github.com/DeppWang/SpringBoot-Demo

總結

通過ShiroFilter配置的過濾器,Shiro攔截所有未過濾的url。如果未登陸,跳轉到登陸頁(loginUrl)。直接使用表單,或者使用subject.login()登陸時。在doGetAuthenticationInfo()中驗證。驗證通過,跳轉到成功頁(successUrl)。

當訪問需要訪問權限的url時,從數據庫查詢當前用戶的權限,最后交給Shiro框架去驗證。在doGetAuthorizationInfo()中實現。

參考資料

springboot整合shiro-登錄認證和權限管理:http://www.ityouknow.com/springboot/2017/06/26/springboot-shiro.html
30分鐘學會如何使用Shiro:http://www.cnblogs.com/learnhow/p/5694876.html
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容