溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Shiro架構(gòu)和Hello World的示例分析

發(fā)布時(shí)間:2021-12-09 15:35:00 來源:億速云 閱讀:188 作者:柒染 欄目:大數(shù)據(jù)

今天就跟大家聊聊有關(guān)Shiro架構(gòu)和Hello  World的示例分析,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

Shiro  一個(gè)Apache 權(quán)限處理框架,現(xiàn)在更流行于security,能夠指定用戶的具體操作哪一個(gè)按鈕,搭配接口,通過注解實(shí)現(xiàn)。

官網(wǎng):

http://shiro.apache.org/download.html

Shiro架構(gòu)和Hello  World的示例分析

主要涉及

Shiro架構(gòu)和Hello  World的示例分析

權(quán)限管理,權(quán)限認(rèn)證,session管理,加密

支持web,緩存,并發(fā),測試,記住我

Shiro架構(gòu)

Shiro架構(gòu)和Hello  World的示例分析

Subject  當(dāng)前用戶

Security  管理subject

Realm   相當(dāng)于RealmDao

Shiro架構(gòu)和Hello  World的示例分析

Hello World

找到官網(wǎng)下載zip包。解壓后

Shiro架構(gòu)和Hello  World的示例分析

找到samples/quickstart/QuickStart.java

并將resource下面的配置復(fù)制

Shiro架構(gòu)和Hello  World的示例分析

QuickStart.java

注釋已經(jīng)寫在代碼內(nèi)

public class Quickstart {

   private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);
   public static void main(String[] args) {
       // The easiest way to create a Shiro SecurityManager with configured        // realms, users, roles and permissions is to use the simple INI config.        // We'll do that by using a factory that can ingest a .ini file and        // return a SecurityManager instance:
       // Use the shiro.ini file at the root of the classpath        // (file: and url: prefixes load from files and urls respectively):        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");        SecurityManager securityManager = factory.getInstance();
       // for this simple example quickstart, make the SecurityManager        // accessible as a JVM singleton.  Most applications wouldn't do this        // and instead rely on their container configuration or web.xml for        // webapps.  That is outside the scope of this simple quickstart, so        // we'll just do the bare minimum so you can continue to get a feel        // for things.        SecurityUtils.setSecurityManager(securityManager);
       // Now that a simple Shiro environment is set up, let's see what you can do:
       // get the currently executing user:
       //獲取當(dāng)前的subject   SecurityUtils.getSubject()        Subject currentUser = SecurityUtils.getSubject();
       // Do some stuff with a Session (no need for a web or EJB container!!!)        //獲取session        Session session = currentUser.getSession();        //放入屬性        session.setAttribute("someKey", "aValue");        //驗(yàn)證是否取到
       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:        //測試是否被認(rèn)證        if (!currentUser.isAuthenticated()) {            //用戶名密碼封裝            UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");            //記住我            token.setRememberMe(true);            try {                //執(zhí)行登錄                currentUser.login(token);                //如果沒有指定的用戶            } catch (UnknownAccountException uae) {                log.info("There is no user with username of " + token.getPrincipal());                //密碼錯(cuò)誤            } catch (IncorrectCredentialsException ice) {                log.info("Password for account " + token.getPrincipal() + " was incorrect!");                //用戶被鎖定            } 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?            //總的認(rèn)證異常處理            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:        //測試是否有某一個(gè)角色        if (currentUser.hasRole("schwartz")) {            log.info("May the Schwartz be with you!");        } else {            log.info("Hello, mere mortal.");        }
       //test a typed permission (not instance-level)        //測試用戶是否有一個(gè)行為  weild        //isPermitted   The 'schwartz' role can do anything (*) with any lightsaber:        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:        //goodguy = winnebago:drive:eagle5   更具體的行為        if (currentUser.isPermitted("winnebago:drive:eagle5")) {            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!        currentUser.logout();
       System.exit(0);    }}

實(shí)際應(yīng)用

@RequiresPermissions("risk:thirdInterface:view")  @GetMapping()  public String thirdInterface(ModelMap map, ThirdInterface thirdInterface) {
   List<ThirdInterface> thirdInterfaceList = thirdInterfaceService.selectThirdInterfaceList(thirdInterface);    map.put("list", thirdInterfaceList);
   return prefix + "/thirdInterface";  }

配合前端

var editFlag = [[${@permission.hasPermi('system:user:edit')}]];var removeFlag = [[${@permission.hasPermi('system:user:remove')}]];
formatter: function(value, row, index) {                    var actions = [];                    actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.userId + '\')"><i class="fa fa-edit"></i>編輯</a> ');                    actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="#" onclick="$.operate.remove(\'' + row.userId + '\')"><i class="fa fa-remove"></i>刪除</a> ');                    actions.push('<a class="btn btn-info btn-xs ' + resetPwdFlag + '" href="#" onclick="resetPwd(\'' + row.userId + '\')"><i class="fa fa-key"></i>重置</a>');                    return actions.join('');                }

在角色的分配操作時(shí)指定是否擁有某個(gè)按鈕的權(quán)限操作。

看完上述內(nèi)容,你們對Shiro架構(gòu)和Hello  World的示例分析有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI