java安全架構shiro(一)

來源:互聯網
上載者:User

標籤:logo   style   system   bubuko   sys   cat   有一個   post   alt   

第一個簡單的案例 ,通過讀取.ini檔案的方式類比登陸,

1.通過Factory工廠的getInstance()方法來擷取SecurityManager的執行個體,執行個體化Factory需要一個ini檔案的路徑作為參數;

2.將上面擷取的執行個體set到 SecurityUtils這個類裡,SecurityUtils類有一個get方法來擷取Subject類

3.Subject這個類就是用來登陸和退出的,但是需要一個AuthenticationToken執行個體作為參數

上面是步驟,下面就來一步一步的介紹,最後會在最下面給出代碼


第二步我就不說了,方法是靜態,直接類名調用就好了;

現在來到第三步,現在應該是得到subjiect的執行個體了,需要登入,但是需要一個token執行個體

 

 

現在token也有了,然後我們進行登入;

 

這裡給出一個.ini檔案的基本樣本,把這個檔案所在的路徑填入第一步執行個體化工廠時的參數裡即可

 

最後給出簡單案例的代碼

package com;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.AuthenticationToken;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.config.IniSecurityManagerFactory;import org.apache.shiro.mgt.SecurityManager;import org.apache.shiro.subject.Subject;import org.apache.shiro.util.Factory;/** * 測試shiro第一個案例 * */public class App {    public static void main( String[] args )    {        {            /*執行個體化工廠*/            Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");           /*擷取執行個體*/            SecurityManager instance = factory.getInstance();            /*設定參數*/            SecurityUtils.setSecurityManager(instance);            /*擷取登入執行個體*/            Subject subject = SecurityUtils.getSubject();            /*執行個體化token,傳入登入的使用者和密碼*/            AuthenticationToken token = new UsernamePasswordToken("admin1", "admin");            try {                /*進行登入*/                subject.login(token);                /*退出登入*/                subject.logout();            }catch (Exception e){                System.out.println("登入錯誤!!");            }        }    }}

 

 

 


 

 

java安全架構shiro(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.