<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Shiro安全框架整合Spring框架連接數(shù)據(jù)庫

          共 11704字,需瀏覽 24分鐘

           ·

          2021-03-25 21:23

          Shiro框架簡介

          • Shiro框架主要架構結構
          1. Subject:任何可以與應用交互的用戶
          2. SecurityManager:相當于SpringMvc中的DispacheServlet;是Shiro的心臟,所以具體的交互都通過SecurityManager進行控制;它所管理的subject負責進行認證、授權、會話及其緩存管理
          3. Authenticator:負責Subject認證,是一個擴展點,可以自定義實現(xiàn),可以使用認證策略,即什么情況下算用戶通過了
          4. Authorizer:授權器、即訪問控制器,用來決定主體是否有權限做主體的操作,即控制著用戶能訪問應用中的哪些功能;
          5. Realm:可以有1個或者多個Realm,可以認為是安全實體數(shù)據(jù)源,即用于獲取安全實體的,可以是JDBC實現(xiàn),也可以是內(nèi)存實現(xiàn)等;由用戶提供;所以應用中都需要實現(xiàn)自己的Realm;
          6. SessionManager:管理session生命周期的組件,而Shiro并不是用在web環(huán)境中,可以用在如普通的JAVASE環(huán)境
          7. CacheManager:緩存控制器:來管理如用戶、角色,授權等緩存,因為基本上很少改變,放到緩存中后可以提高訪問的性能
          8. Cryptography:密碼模塊,Shiro提高一些常見的加密組件用于加密。

          創(chuàng)建初始的Hello Word程序

          package com.yang.helloword;

          import org.apache.shiro.SecurityUtils;
          import org.apache.shiro.authc.*;
          import org.apache.shiro.config.IniSecurityManagerFactory;
          import org.apache.shiro.mgt.SecurityManager;
          import org.apache.shiro.session.Session;
          import org.apache.shiro.subject.Subject;
          import org.apache.shiro.util.Factory;
          import org.slf4j.Logger;
          import org.slf4j.LoggerFactory;


          /**
           * Simple Quickstart application showing how to use Shiro's API.
           *
           * @since 0.9 RC2
           */

          public class Quickstart {

              private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);


              public static void main(String[] args) {
                  Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
                  SecurityManager securityManager = factory.getInstance();
              
                  SecurityUtils.setSecurityManager(securityManager);

                  // Now that a simple Shiro environment is set up, let's see what you can do:

                  // get the currently executing user:
                  // 獲取當前的 Subject. 調(diào)用 SecurityUtils.getSubject();
                  Subject currentUser = SecurityUtils.getSubject();

                  // Do some stuff with a Session (no need for a web or EJB container!!!)
                  // 測試使用 Session 
                  // 獲取 Session: Subject#getSession()
                  Session session = currentUser.getSession();
                  session.setAttribute("someKey""aValue");
                  String value = (String) session.getAttribute("someKey");
                  if (value.equals("aValue")) {
                      log.info("---> Retrieved the correct value! [" + value + "]");
                  }

                  // let's login the current user so we can check against roles and permissions:
                  // 測試當前的用戶是否已經(jīng)被認證. 即是否已經(jīng)登錄. 
                  // 調(diào)動 Subject 的 isAuthenticated() 
                  if (!currentUser.isAuthenticated()) {
                   // 把用戶名和密碼封裝為 UsernamePasswordToken 對象
                      UsernamePasswordToken token = new UsernamePasswordToken("lonestarr""vespa");
                      // rememberme
                      token.setRememberMe(true);
                      try {
                       // 執(zhí)行登錄. 
                          currentUser.login(token);
                      } 
                      // 若沒有指定的賬戶, 則 shiro 將會拋出 UnknownAccountException 異常. 
                      catch (UnknownAccountException uae) {
                          log.info("----> There is no user with username of " + token.getPrincipal());
                          return
                      } 
                      // 若賬戶存在, 但密碼不匹配, 則 shiro 會拋出 IncorrectCredentialsException 異常。 
                      catch (IncorrectCredentialsException ice) {
                          log.info("----> Password for account " + token.getPrincipal() + " was incorrect!");
                          return
                      } 
                      // 用戶被鎖定的異常 LockedAccountException
                      catch (LockedAccountException lae) {
                          log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                                  "Please contact your administrator to unlock it.");
                      }
                      // ... catch more exceptions here (maybe custom ones specific to your application?
                      // 所有認證時異常的父類. 
                      catch (AuthenticationException ae) {
                          //unexpected condition?  error?
                      }
                  }

                  //say who they are:
                  //print their identifying principal (in this case, a username):
                  log.info("----> User [" + currentUser.getPrincipal() + "] logged in successfully.");

                  //test a role:
                  // 測試是否有某一個角色. 調(diào)用 Subject 的 hasRole 方法. 
                  if (currentUser.hasRole("schwartz")) {
                      log.info("----> May the Schwartz be with you!");
                  } else {
                      log.info("----> Hello, mere mortal.");
                      return
                  }

                  //test a typed permission (not instance-level)
                  // 測試用戶是否具備某一個行為. 調(diào)用 Subject 的 isPermitted() 方法。 
                  if (currentUser.isPermitted("lightsaber:weild")) {
                      log.info("----> You may use a lightsaber ring.  Use it wisely.");
                  } else {
                      log.info("Sorry, lightsaber rings are for schwartz masters only.");
                  }

                  //a (very powerful) Instance Level permission:
                  // 測試用戶是否具備某一個行為. 
                  if (currentUser.isPermitted("user:delete:zhangsan")) {
                      log.info("----> You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
                              "Here are the keys - have fun!");
                  } else {
                      log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
                  }

                  //all done - log out!
                  // 執(zhí)行登出. 調(diào)用 Subject 的 Logout() 方法. 
                  System.out.println("---->" + currentUser.isAuthenticated());
                  
                  currentUser.logout();
                  
                  System.out.println("---->" + currentUser.isAuthenticated());

                  System.exit(0);
              }
          }


             

          如果你覺得這篇內(nèi)容對你挺有啟發(fā),我想邀請你幫我三個小忙:

          • 點個【在看】,或者分享轉發(fā),讓更多的人也能看到這篇內(nèi)容

          • 關注公眾號【園碼生活】,不定期分享原創(chuàng)&精品技術文章。

          • 公眾號內(nèi)回復:【1】。加本人微信


          歡迎評論區(qū)留下你的精彩評論~          
                   

          覺得文章不錯可以分享到朋友圈讓更多的小伙伴看到哦~

          客官!在看一下唄          


          瀏覽 49
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美性爱五月婷婷 | 小早川怜子无码 | 污污污污污网站 | 国产精品一级三级 | 天天日天天操天天干青青草超碰av |