《基於SpringBoot+Shiro的權限管理實現》論文筆記


《基於SpringBoot+Shiro的權限管理實現》論文筆記

一、基本信息

  • 標題:基於SpringBoot+Shiro的權限管理實現

  • 來源:成都大學信息工程與科學學院,成都,610100

  • 關鍵字:Shiro 框架;SpringBoot 框架;權限管理

二、研究內容

  1. Shiro 概述
    • Apache Shiro 是一個擁有許多功能的綜合性的程序安全框架, Shiro 提供了一個干凈而直觀的API, 它干凈利落地處理身份認證、授權、企業會話管理和加密。Shiro 易於使用和理解, 同時功能十分強大, 能驗證用戶來核實他們的身份、對用戶執行訪問控制、判斷用戶是否被分配了一個確定的安全角色、判斷用戶是否被允許做某事。
    • Shiro 當中的Session 功能是在相應應用中創建會話編程范式, 其與容器是相互獨立的關系, 因此Shiro 支持在任何環境下使用Session API, 即使沒有Web 或EJB容器。Shiro 還能在身份驗證訪問控制期間或在會話的生命周期, 對事件作出反應。
    • Apache Shiro 的架構有3 個主要的概念: Subject,SecurityManager 和Realms。其中Subject 是一個比較抽象的概念, 通常我們會將Subject 對象理解為一個任何可以與應用交互的“用戶”, 但它也有可能是一個三方程序, 可以理解為任何與系統交互的“東西” 都是Subject。在Shiro 框架中通過Subject 完成登錄、退出、校驗權限、獲得Session 等。SecurityManager 是Shiro 的心臟, 是Shiro 中最核心的組件; 所有具體的安全操作都通過SecurityManager 進行控制; SecurityManager 管理着所有Subject, 並且關於Subject 的所有操作都由SecurityManager進行交互。Realms 擔當Shiro 和應用程序的安全數據之間的“橋梁” 或“連接器”, 用於用戶認證和授權; Realms 本質上是一個特定安全的DAO: 它封裝了數據源的連接詳細信息, 使Shiro 所需的相關的數據可用。當配置Shiro 時, 必須指定至少一個Realm 用來進行身份驗證和授權。SecurityManager 可以配置多個Realms, 但至少需要配置一個。
    1. 配置pom 文件
      <! --集成shiro-->
      <dependency>
      <group|d> org.apache.shiro </group|d>
      <artifact|d> shiro-spring </artifact|d>
      <version> 1.4.0 </version>
      </dependency>
      
    2. 自定義Reaml
@Component
public class MyShiroUserRealm extends AuthorizingRealm {
   @Autowired
   UserServiceImpl userService;

   /**
    * 用於授權
    *
    * @param principa|s
    * @return 授權信息
    */
   @Override
   protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
       UserInforMation userInfo = (UserInforMation) principals.getPrimaryPrincipal();
//用戶權限列表
       Set<String> userPermsSet = userService.getUserPermissions(userInfo);
       SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
       authorizationInfo.setStringPermissions(userPermsSet);
       return info;
   }

   /**
    * 用於認證
    *
    * @param token
    * @return 認證信息
    * @throws AuthenticationException
    */
   @Override
   protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
       //用戶信息獲取
       String userNameInput = (String) token.getPrincipal();
       String passwordInput = new String((char[]) token.getCredentials());
       //查詢用戶信息
       UserInforMation user = userService.findld(userNameInput);
       //用戶不存在
       if (user == null) {
           throw new UnknownAccountException("用戶賬號不存在! ");
       }
       //密碼錯誤
       if (!passwordInput.equals(user.getPassword())) {
           throw new IncorrectCredentialsException("賬號用戶名或者密碼錯誤! ");
       }
       //賬號被注銷
       if (user.getState().equals("0")) {
           throw new LockedAccountException("賬戶已被注銷! ");
       }
       System.out.println("用戶登陸成功! ");
       SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), user.getName());
       return info;
   }
}

三、結論

  • 通過在SpringBoot 項目中使用Shiro 安全框架為項目 提供了安全訪問控制的功能,同時實現了用戶身份認證、授權、會話管理等功能。任何程序都需要安全控制,將 程序的安全控制交給Shiro,利用Shiro 簡單易用的特點 配合SpringBoot 快速便捷開發,減少了為項目安全控制 編寫大量重復代碼的工作.

四、參考文獻

  • [1]王杉文.基於SpringBoot+Shiro的權限管理實現[J].電腦編程技巧與維護,2019,(9):160-161,173.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM