1. 程式人生 > 實用技巧 >Shiro初步學習

Shiro初步學習

感謝作者:本文來源http://www.iocoder.cn/Shiro/xiaoqiyiye/intro/

  • SecurityManager :安全管理,Shiro最核心的元件,Shiro通過SecurityMananger 來管理內部元件的例項,初始化例項。並通過他來提供安全管理的各種服務
  • Authenticator:認證器,認證AuthenticationToken是否有效
  • Authorizer:授權器,處理角色和許可權。
  • subject:當前操縱主體,表示當前操作的使用者
  • SubjectContext:Subject 上下文資料物件
  • AuthenticationToken:認證token資訊,(使用者名稱和密碼)
  • ThreadContext:執行緒上線問物件,負責繫結物件當前執行緒

在學習和使用Shiro過程中,我們知道SecurityManager介面時Shireo中最合性的介面,我們就沿著這個介面進行分析下面的程式碼是SecurityManager介面的定義:

    

public interface SecurityManager extends Authenticator, Authorizer, SessionManager {
                         //認證器          //授權器       
    /**
     * 登入
     */
    Subject login(Subject subject, AuthenticationToken authenticationToken) throws AuthenticationException;

    
/** * 登出 */ //操作主題當前使用者 void logout(Subject subject); /** * 建立Subject */ Subject createSubject(SubjectContext context); }

在SecurityManager 中有三個方法,一個是登陸,等處,建立Object 。通常我們是這麼使用的,

  1. 首先建立Subject物件,
  2. 然後通過呼叫login方法傳入認證資訊token對登陸進行認證。
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token 
= new UsernamePasswordToken("zhang", "123"); subject.login(token);

SecurityUtils 分析

在Shiro中提供了一個方便使用的工具類SecurityUtils,SecurityUtils核心功能是獲取SecurityManager ,以及獲取subject