Springboot整合Shiro之授權

  • 2019 年 12 月 2 日
  • 筆記

版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接:https://dpb-bobokaoya-sm.blog.csdn.net/article/details/103326945

  本文我們來介紹SpringBoot整合Shiro來實現授權的操作

一、註解的使用方式

1.配置類修改

  在許可權校驗的時候,我們一般在控制中通過註解來實現許可權的校驗,但我們需要放開註解的使用,在配置文件中的使用方式如下:

對應的在配置類中的設置如下:

/**   * 開啟授權註解使用方式   * @param manager   * @return   */  @Bean  public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager manager){      AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();      advisor.setSecurityManager(manager);      return advisor;  }

2.自定義realm方法

在自定義的realm中實現授權方法

**   * 授權的方法   * @param principalCollection   * @return   */  @Override  protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {      User user = (User) principalCollection.getPrimaryPrincipal();      System.out.println("授權的帳號是:" + user.getUsername());      // 模擬許可權      SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();      info.addRole("role1");      return info;  }

3.業務處理

我們創建UserController,對相關的不同方法設置不同的訪問許可權,如下

package com.dpb.springboot41shiro.controller;    import org.apache.shiro.authz.annotation.Logical;  import org.apache.shiro.authz.annotation.RequiresRoles;  import org.springframework.stereotype.Controller;  import org.springframework.web.bind.annotation.RequestMapping;    /**   * @program: springboot-41-shiro   * @description:   * @author: 波波烤鴨   * @create: 2019-11-29 21:37   */  @Controller  @RequestMapping("/user")  public class UserController {        @RequiresRoles(value = {"role1"},logical = Logical.AND)      @RequestMapping("/query")      public String query(){          System.out.println("用戶查詢...");          return "/user";      }        @RequiresRoles(value = {"role2"},logical = Logical.AND)      @RequestMapping("/update")      public String update(){          System.out.println("用戶更新...");          return "/user";      }  }

4.測試

訪問』/user/query』有許可權,訪問』/user/update』就沒有許可權。登錄成功後訪問

5.自定義異常類

當沒有訪問許可權時,我們是直接通過系統error頁面來提示的,我們可以通過自定義的異常頁面來提示,用戶體驗會更好些,之前在配置文件中的配置方式是:

那麼對於的在java配置類中。

/**   * 全局異常處理器   * @return   */  @Bean  public SimpleMappingExceptionResolver simpleMappingExceptionResolver(){      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("exception");     // Default is "exception"      return resolver;  }

效果:

二、Shiro標籤的使用

  除了通過註解來驗證許可權,我們在頁面中對許可權校驗的時候通過shiro標籤庫會更方便些,所以我們來看下SpringBoot中結合Thymeleaf來使用shiro標籤庫要怎麼使用

1.添加對應的依賴

  使用shiro的標籤庫我們需要單獨在引入如下的依賴。

<dependency>      <groupId>com.github.theborakompanioni</groupId>      <artifactId>thymeleaf-extras-shiro</artifactId>      <version>2.0.0</version>  </dependency>

2.配置類修改

在配置類中我們需要註冊 ShiroDialect對象

//用於thymeleaf模板使用shiro標籤  @Bean  public ShiroDialect shiroDialect() {      return new ShiroDialect();  }

3.在頁面中配置及使用

我們需要在頁面頭部引入此

xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"

Shiro便簽的使用

<!DOCTYPE html>  <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"        xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" >  <head>      <meta charset="UTF-8">      <title>Title</title>  </head>  <body>      <h1>用戶管理</h1>      <span shiro:hasRole="role1">許可權1</span><br>      <span shiro:hasRole="role2">許可權2</span><br>      <shiro:authenticated>          已登錄      </shiro:authenticated>      <shiro:principal property="username"></shiro:principal>      <shiro:guest>遊客</shiro:guest>  </body>  </html>

4.測試

登錄後訪問如下:

說明標籤庫起作用了!