當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Shiro 整合 SpringBoot
生活随笔
收集整理的這篇文章主要介紹了
Shiro 整合 SpringBoot
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Shiro 整合 SpringBoot
shiro主要有三大功能模塊
Subject:主體,一般指用戶。
SecurityManager:安全管理器,管理所有Subject,可以配合內部安全組件。(類似于SpringMVC中的DispatcherServlet)
Realms:用于進行權限信息的驗證,一般需要自己實現。
shiro架構圖
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-PkoyMhpN-1610439982383)(C:\Users\鄭大人\AppData\Roaming\Typora\typora-user-images\image-20200629142750768.png)]
Subject 用戶 SecurityManager 管理所有用戶 Realm 連接數據Shiro入門
Shiro 配置
按步驟:
先創建一個 Realms 類 繼承自 AuthorizingRealm 類 實現它兩個方法 授權 doGetAuthorizationInfo 和 認證 doGetAuthenticationInfo
import com.entity.User; import com.service.impl.UserServiceImpl; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.*; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.Subject; import org.springframework.beans.factory.annotation.Autowired;//自定義的UserRealm public class UserRealm extends AuthorizingRealm {@AutowiredUserServiceImpl userService;//授權@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {System.out.println("執行了==> 授權");// 創建 簡單授權信息類final SimpleAuthorizationInfo authorizationInfo= new SimpleAuthorizationInfo();//獲得當前的用戶 當前的用戶首先得被存入 簡單身份驗證信息類 構造函數的第一個參數中final Subject subject = SecurityUtils.getSubject();User currentUser = (User)subject.getPrincipal();//添加當前用戶 字符串權限authorizationInfo.addStringPermission(currentUser.getPerms());return authorizationInfo;}//認證@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {System.out.println("執行了==> 認證 ");UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;// 用戶名,密碼 從數據庫中取User user = userService.qureyUserByName(token.getUsername());if(user == null){return null; //拋出異常 UnknownAccountException 未知賬戶異常}//密碼認證 由 shiro 來完成,加密了//返回 簡單身份驗證信息類return new SimpleAuthenticationInfo(user,user.getPassword(),"");} }創建一個Shiro的配置類,里面向spring容器注入三個Bean 分別是:
登錄
@RequestMapping("/login")public String login(@RequestParam(name = "username") String username,@RequestParam(name = "password") String password,Model model){//獲取當前用戶final Subject subject = SecurityUtils.getSubject();//封裝用戶的登錄數據final UsernamePasswordToken token = new UsernamePasswordToken(username, password);try {//執行登錄的方法 ,如果沒有異常就ok了//執行登錄時,會進入Realm中認證subject.login(token);return "index";} catch (AuthenticationException e) {model.addAttribute("msg","用戶名或密碼錯誤!");return "login";}}注銷
/*** 退出用戶* @return*/@RequestMapping("/logout")public String logout(){final Subject subject = SecurityUtils.getSubject();//退出用戶subject.logout();return "index";}Shiro整合 Thymeleaf
依賴
<!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro --> <dependency><groupId>com.github.theborakompanioni</groupId><artifactId>thymeleaf-extras-shiro</artifactId><version>2.0.0</version> </dependency>整合配置
Shiro整合 Thymeleaf 需要在配置類中加入一個Bean 到 spring容器,這個類是 ShiroDialect
@Configuration public class ShiroConfig {//用來整合 thymeleaf@Beanpublic ShiroDialect getShiroDialect(){return new ShiroDialect();}//ShiroFilterFactoryBean :3@Beanpublic ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("SecurityManager")DefaultWebSecurityManager defaultWebSecurityManager){final ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();// 設置安全管理器bean.setSecurityManager(defaultWebSecurityManager);//添加shiro的內置過濾器 攔截/*** anon:無需認證即可訪問* authc:必須認證才可以訪問* user:必須擁有記住我 功能才可以用* perms:擁有對某個資源的權限才能訪問* role:擁有某個角色權限才能訪問*///攔截Map<String,String> filterMap = new LinkedHashMap<>();filterMap.put("/add","authc"); //也可支持通配符 *filterMap.put("/update","authc");//授權 perms[a:b] 必須是a ,且有權限bfilterMap.put("/add","perms[user:add]");filterMap.put("/update","perms[user:update]");//設置過濾器鏈定義圖bean.setFilterChainDefinitionMap(filterMap);//設置登錄頁面,即如果沒有權限,就會跳轉至登錄頁面bean.setLoginUrl("/toLogin");//設置未授權跳轉頁面的urlbean.setUnauthorizedUrl("/unauthorized");return bean;}//DefaultWebSecurityManager:2@Bean(name = "SecurityManager")public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){final DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();//關聯 UserRealmdefaultWebSecurityManager.setRealm(userRealm);return defaultWebSecurityManager;}//創建 realm 對象 ,需要自定義 :1@Bean(name = "userRealm")public UserRealm getRealm(){return new UserRealm();}}html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro"> <!-- shiro 整合 thymeleaf 的提示--> <head><meta charset="UTF-8"><title>首頁</title> </head> <body> <h1>首頁</h1> <p><a href="/toLogin">登錄</a></p> <p><a href="/logout">退出</a></p> <hr><div shiro:hasPermission="user:add"> <!--判斷權限--><a th:href="@{/add}">add</a> </div> <div shiro:hasPermission="user:update"><a th:href="@{/update}">update</a> </div></body> </html>總結
以上是生活随笔為你收集整理的Shiro 整合 SpringBoot的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信开发3之php模板信息推送
- 下一篇: JavaScript复习使用定时器的简易