国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Spring MVC整合Shiro權限控制的方法

2024-07-14 08:40:41
字體:
來源:轉載
供稿:網友

Apache Shiro 是一個功能強大且靈活的開放源代碼安全框架,可以細粒度地處理認證 (Authentication),授權 (Authorization),會話 (Session) 管理和加密 (cryptography) 等企業級應用中常見的安全控制流程。 Apache Shiro 的首要目標是易于使用和理解。 有時候安全性的流程控制會非常復雜,對開發人員來說是件很頭疼的事情,但并不一定如此。 框架就應該盡可能地掩蓋復雜性,并公開一個簡潔而直觀的 API,從而簡化開發人員的工作,確保其應用程序安全性。這次我們聊一聊如何在 Spring Web 應用中使用 Shiro 實現權限控制。

功能

Apache Shiro 是一個具有許多功能的綜合型應用程序安全框架。 下圖為 Shiro 中的最主要的幾個功能:

 

 
Spring,MVC,權限控制,Shiro

 

Shiro 的主要目標是“應用安全的四大基石” - 認證,授權,會話管理和加密:

  1. 身份驗證:也就是通常所說的 “登錄”,為了證明用戶的行為所有者。
  2. 授權:訪問控制的過程,即確定什么用戶可以訪問哪些內容。
  3. 會話管理:即使在非 Web 應用程序中,也可以管理用戶特定的會話,這也是 Shiro 的一大亮點。
  4. 加密技術:使用加密算法保證數據的安全,非常易于使用。

架構

從整體概念上理解,Shiro 的體系架構有三個主要的概念:Subject (主體,也就是用戶),Security Manager (安全管理器)和 Realms (領域)。 下圖描述了這些組件之間的關系:

 

 
Spring,MVC,權限控制,Shiro

 

這幾大組件可以這樣理解:

  1. Subject (主體):主體是當前正在操作的用戶的特定數據集合。主體可以是一個人,也可以代表第三方服務,守護進程,定時任務或類似的東西,也就是幾乎所有與該應用進行交互的事物。
  2. Security Manager (安全管理器):它是 Shiro 的體系結構的核心,扮演了類似于一把 “傘” 的角色,它主要負責協調內部的各個組件,形成一張安全網。
  3. Realms (領域):Shiro 與應用程序安全數據之間的 “橋梁”。當需要實際與用戶帳戶等安全相關數據進行交互以執行認證和授權時,Shiro 將從 Realms 中獲取這些數據。

數據準備

在 Web 應用中,對安全的控制主要有角色、資源、權限(什么角色能訪問什么資源)幾個概念,一個用戶可以有多個角色,一個角色也可以訪問多個資源,也就是角色可以對應多個權限。落實到數據庫設計上,我們至少需要建 5 張表:用戶表、角色表、資源表、角色-資源表、用戶-角色表,這 5 張表的結構如下:

用戶表:

 

id username password
1 張三 123456
2 李四 666666
3 王五 000000

 

角色表:

 

 

id rolename
1 管理員
2 經理
3 員工

 

資源表:

 

id resname
1 /user/add
2 /user/delete
3 /compony/info

 

角色-資源表:

 

id roleid resid
1 1 1
2 1 2
3 2 3

 

用戶-角色表:

 

id userid roleid
1 1 1
2 1 2
3 1 3

 

對應的 POJO 類如下:

/** * 用戶 */public class User { private Integer id; private String username; private String password; //getter & setter...}
/** * 角色 */public class Role { private String id; private String rolename;}
/** * 資源 */public class Resource { private String id; private String resname;}
/** * 角色-資源 */public class RoleRes { private String id; private String roleid; private String resid;}
/** * 用戶-角色 */public class UserRole { private String id; private String userid; private String roleid;}

Spring 與 Shiro 整合的詳細步驟,請參閱我的博客 《 Spring 應用中整合 Apache Shiro 》 。 這里補充一下:需要提前引入 Shiro 的依賴,打開mvnrepository.com,搜索 Shiro,我們需要前三個依賴,也就是 Shiro-Core、Shiro-Web 以及 Shiro-Spring,以 Maven 項目為例,在 pom.xml 中的 <dependencies> 節點下添加如下依賴:

<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version></dependency><dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.4.0</version></dependency><dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</version></dependency>

application-context.xml 中需要這樣配置 shiroFilter bean:

<!-- 配置shiro的過濾器工廠類,id- shiroFilter要和我們在web.xml中配置的過濾器一致 --><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <!-- 登錄頁面 --> <property name="loginUrl" value="/login"/> <!-- 登錄成功后的頁面 --> <property name="successUrl" value="/index"/> <!-- 非法訪問跳轉的頁面 --> <property name="unauthorizedUrl" value="/403"/> <!-- 權限配置 --> <property name="filterChainDefinitions"> <value>  <!-- 無需認證即可訪問的靜態資源,還可以添加其他 url -->  /static/** = anon  <!-- 除了上述忽略的資源,其他所有資源都需要認證后才能訪問 -->  /** = authc </value> </property></bean>

接下來就需要定義 Realm 了,自定義的 Realm 集成自 AuthorizingRealm 類:

public class MyRealm extends AuthorizingRealm { @Autowired private UserService userService; /** * 驗證權限 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { String loginName = SecurityUtils.getSubject().getPrincipal().toString(); if (loginName != null) { String userId = SecurityUtils.getSubject().getSession().getAttribute("userSessionId").toString(); // 權限信息對象,用來存放查出的用戶的所有的角色及權限 SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); // 用戶的角色集合 ShiroUser shiroUser = (ShiroUser) principalCollection.getPrimaryPrincipal();  info.setRoles(shiroUser.getRoles());  info.addStringPermissions(shiroUser.getUrlSet()); return info; } return null; } /** * 認證回調函數,登錄時調用 */ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) { String username = (String) token.getPrincipal(); User user = new User(); sysuser.setUsername(username); try { List<SysUser> users = userService.findByNames(user);  List<String> roleList= userService.selectRoleNameListByUserId(users.get(0).getId()); if (users.size() != 0) { String pwd = users.get(0).getPassword(); // 當驗證都通過后,把用戶信息放在 session 里 Session session = SecurityUtils.getSubject().getSession(); session.setAttribute("userSession", users.get(0)); session.setAttribute("userSessionId", users.get(0).getId()); session.setAttribute("userRoles", org.apache.commons.lang.StringUtils.join(roleList,","));  return new SimpleAuthenticationInfo(username,users.get(0).getPassword()); } else {  // 沒找到該用戶 throw new UnknownAccountException(); } } catch (Exception e) { System.out.println(e.getMessage()); } return null; } /** * 更新用戶授權信息緩存. */ public void clearCachedAuthorizationInfo(PrincipalCollection principals) { super.clearCachedAuthorizationInfo(principals); } /** * 更新用戶信息緩存. */ public void clearCachedAuthenticationInfo(PrincipalCollection principals) { super.clearCachedAuthenticationInfo(principals); } /** * 清除用戶授權信息緩存. */ public void clearAllCachedAuthorizationInfo() { getAuthorizationCache().clear(); } /** * 清除用戶信息緩存. */ public void clearAllCachedAuthenticationInfo() { getAuthenticationCache().clear(); } /** * 清空所有緩存 */ public void clearCache(PrincipalCollection principals) { super.clearCache(principals); } /** * 清空所有認證緩存 */ public void clearAllCache() { clearAllCachedAuthenticationInfo(); clearAllCachedAuthorizationInfo(); }}

最后定義一個用戶登錄的控制器,接受用戶的登錄請求:

@Controllerpublic class UserController { /** * 用戶登錄 */ @PostMapping("/login") public String login(@Valid User user,BindingResult bindingResult,RedirectAttributes redirectAttributes){ try {  if(bindingResult.hasErrors()){  return "login";  }  //使用權限工具進行認證,登錄成功后跳到 shiroFilter bean 中定義的 successUrl  SecurityUtils.getSubject().login(new UsernamePasswordToken(user.getUsername(), user.getPassword()));  return "redirect:index"; } catch (AuthenticationException e) {  redirectAttributes.addFlashAttribute("message","用戶名或密碼錯誤");  return "redirect:login"; } } /** * 注銷登錄 */ @GetMapping("/logout") public String logout(RedirectAttributes redirectAttributes ){ SecurityUtils.getSubject().logout(); return "redirect:login"; }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳信县| 兰溪市| 祁连县| 高邮市| 淅川县| 辰溪县| 自贡市| 福贡县| 岑溪市| 汤原县| 巍山| 青岛市| 宣威市| 汨罗市| 都匀市| 搜索| 阿城市| 星子县| 永昌县| 西宁市| 济南市| 忻州市| 平昌县| 临朐县| 沙田区| 始兴县| 乐都县| 平舆县| 鸡西市| 十堰市| 江津市| 西吉县| 林西县| 桑植县| 新化县| 玉环县| 海淀区| 济源市| 凯里市| 通州市| 那曲县|