shiro框架許可權模組細節

2021-09-10 21:52:13 字數 3116 閱讀 9836

以前登入成功,傳的是username,現在傳employee物件

//身份認證

@override

protected authenticationinfo dogetauthenticationinfo(authenticationtoken authenticationtoken) throws authenticationexception

session是從subject獲取

存在shiro的session中後,httpsession也會有值

public class usercontext 

//獲取到當前登入使用者

public static employee getuser()

}

儲存所有許可權過濾的資料都是從資料庫中獲取

@autowired

private ipermissionservice permissionservice;

public mapcreatefilterchaindefinitionmap());

filterchaindefinitionmap.put("/**", "authc");

return filterchaindefinitionmap;

}

授權部分的資料也是從資料庫中獲得的

應該拿到當前登入使用者的所有許可權

jpql關聯原則: 1.不寫on 2.關聯物件的別名.屬性

//根據使用者拿到他對應的所有許可權

@query("select distinct p.sn from employee e join e.roles r join r.permissions p where e.id = ?1")

setfindpermsbyuser(long userid);

拿到當前登入使用者,再獲取它的許可權

@override

protected authorizationinfo dogetauthorizationinfo(principalcollection principalcollection)

shiro處理沒有許可權是跳轉頁面,而我們如果是ajax請求,我們希望是返回json資料

ajax請求會有乙個請求頭:x-requested-with: xmlhttprequest

需要自定義乙個shiro的許可權過濾器

public class aisellpermissionsauthorizationfilter extends permissionsauthorizationfilter  else 

resp.getwriter().print("");

}else else }}

return false;

}}

配置許可權過濾器

entry key=「aisellperms」:確定許可權過濾器的名稱

...

@autowired

private ipermissionservice permissionservice;

public mapcreatefilterchaindefinitionmap());

...}

員工 -> 角色 -> 許可權 -> 選單

選單domain的自關連配置

需要配置雙向,但是不能讓jpa去管理一對多(我們自己管理:@transient)

雙向生成json會產生死迴圈,需要一邊進行忽略:@jsonignore

//讓它不再生成json

@manytoone(fetch = fetchtype.lazy)

@joincolumn(name = "parent_id")

@jsonignore

private menu parent;

// 臨時屬性 -> 這個欄位jpa就不管它了

@transient

private listchildren = new arraylist<>();

public inte***ce menurepository extends baserepository
根據設計只能通過員工找到子選單

需要通過子選單拿到父選單

判斷這個父選單是否已經存到集合中

如果這個菜單單沒有存起來,放到集合中

把當前這個子選單放到父選單中去

@override

public listfindloginmenu()

為這個父選單新增對應的子選單

parent.getchildren().add(child);

}return parentmenus;

}

@autowired

private imenuservice menuservice;

@responsebody

public listloginusermenu()

$('#menutree').tree({

url:'/util/loginusermenu',

...

沒有這個許可權,就不展示對應的按鍵

<%@ taglib prefix="shiro" uri="" %>

...刪除

許可權框架 shiro 授權demo

之前說了許可權認證,其實也就是登入驗證身份 這次來說說shiro的授權 shiro可以針對角色授權,或者訪問資源授權 兩者都行,但是在如今的複雜系統中,當然使用後者,如果你是小系統或者私活的話,前者即可,甚至可以不用,我懂的 好吧,上 首先新建乙個ini,登陸資訊以及許可權配置好 1 使用者2 us...

許可權框架 shiro 授權demo

之前說了許可權認證,其實也就是登入驗證身份 這次來說說shiro的授權 shiro可以針對角色授權,或者訪問資源授權 兩者都行,但是在如今的複雜系統中,當然使用後者,如果你是小系統或者私活的話,前者即可,甚至可以不用,我懂的 好吧,上 首先新建乙個ini,登陸資訊以及許可權配置好 1 使用者2 us...

shiro許可權框架與spring框架輕鬆整合

shiro是乙個許可權框架,用於管理 的許可權,大到 登入過濾,小到乙個選單或按鈕是否顯示,shiro學習起來非常簡單,以下是shiro的執行流程圖 subject為當前使用者,當它訪問系統的時候,就會經過securitymanager安全管理器,安全管理器類似乙個中轉站,它實際上會讓realm類來...