精品专区-精品自拍9-精品自拍三级乱伦-精品自拍视频-精品自拍视频曝光-精品自拍小视频

網站建設資訊

NEWS

網站建設資訊

如何在SpringMVC中使用Shiro實現權限控制

這篇文章將為大家詳細講解有關如何在Spring MVC中使用Shiro實現權限控制,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

堅守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業 ”的價值觀,專業網站建設服務10余年為成都工商代辦小微創業公司專業提供成都企業網站建設營銷網站建設商城網站建設手機網站建設小程序網站建設網站改版,從內容策劃、視覺設計、底層架構、網頁布局、功能開發迭代于一體的高端網站建設服務。

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 張表的結構如下:

用戶表:


idusernamepassword
1張三123456
2李四666666
3王五000000

角色表:


idrolename
1管理員
2經理
3員工

資源表:


idresname
1/user/add
2/user/delete
3/compony/info

角色-資源表:


idroleidresid
111
212
323

用戶-角色表:


iduseridroleid
111
212
313

對應的 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 中的 節點下添加如下依賴:


 org.apache.shiro
 shiro-core
 1.4.0


 org.apache.shiro
 shiro-web
 1.4.0


 org.apache.shiro
 shiro-spring
 1.4.0

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



 
 
 
 
 
 
 
 
 
 
  
  /static/** = anon
  
  /** = authc
 
 

接下來就需要定義 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 users = userService.findByNames(user);
  List 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();
 }
}

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

@Controller
public 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";
 }
}

關于如何在Spring MVC中使用Shiro實現權限控制就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。


文章名稱:如何在SpringMVC中使用Shiro實現權限控制
標題URL:http://m.jcarcd.cn/article/piooeg.html
主站蜘蛛池模板: 91蜜桃传媒吴梦梦 | 国产呦福利导航 | 国产免费观| 日朝欧美亚洲精品 | 最新国产精品拍自在线播放 | 日韩中文字幕a加勒 | 国产一区二区不卡 | 国产疯狂女同互磨高 | 韩国好看女 | 欧美制服丝袜在线 | 精品国产91久 | 国产精品黄在线 | 99热国产 | 日韩国产免费 | 国产性生活视频 | 日韩手机专区 | 国产精品亚洲欧 | 日本亚洲欧美风情 | 区激情校园小说 | 国产波霸爆 | 国产女主播喷水 | 91精选视频 | 日本一二区在线观看 | 国产在线观看精品 | 福利性影院 | 清纯唯美亚洲 | 国产人碰 | 日韩在线观看网站 | 日本护士毛茸茸xx | 91五月天极品 | 精品自拍视频曝光 | 国产亚洲中文字幕 | 日韩免费视频网址 | 精品国产综合区 | 国内精品专区 | 午夜福利免费院 | 国产在线视频国产 | 92午夜福 | 青青久在线视频 | 91极品女神嫩 | 日韩一品二品三品 |