亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

基于spring boot 2和shiro實(shí)現(xiàn)身份驗(yàn)證案例

瀏覽:68日期:2023-09-09 14:07:15

Shiro是一個(gè)功能強(qiáng)大且易于使用的Java安全框架,官網(wǎng):https://shiro.apache.org/。

主要功能有身份驗(yàn)證、授權(quán)、加密和會(huì)話管理。

其它特性有Web支持、緩存、測(cè)試支持、允許一個(gè)用戶用另一個(gè)用戶的身份進(jìn)行訪問(wèn)、記住我。

基于spring boot 2和shiro實(shí)現(xiàn)身份驗(yàn)證案例

Shiro有三個(gè)核心組件:Subject,SecurityManager和 Realm。

基于spring boot 2和shiro實(shí)現(xiàn)身份驗(yàn)證案例

Subject:即當(dāng)前操作“用戶”,“用戶”并不僅僅指人,也可以是第三方進(jìn)程、后臺(tái)帳戶或其他類似事物。

SecurityManager:安全管理器,Shiro框架的核心,通過(guò)SecurityManager來(lái)管理所有Subject,并通過(guò)它來(lái)提供安全管理的各種服務(wù)。

Realm:域,充當(dāng)了Shiro與應(yīng)用安全數(shù)據(jù)間的“橋梁”或者“連接器”。也就是說(shuō),當(dāng)對(duì)用戶執(zhí)行認(rèn)證(登錄)和授權(quán)(訪問(wèn)控制)驗(yàn)證時(shí),Shiro會(huì)從應(yīng)用配置的Realm中查找用戶及其權(quán)限信息。當(dāng)配置Shiro時(shí),必須至少指定一個(gè)Realm,用于認(rèn)證和(或)授權(quán)。

Spring Boot 中整合Shiro,根據(jù)引入的依賴包shiro-spring和shiro-spring-boot-web-starter(當(dāng)前版本都是1.4.2)不同有兩種不同方法。

方法一:引入依賴包shiro-spring

1、IDEA中創(chuàng)建一個(gè)新的SpringBoot項(xiàng)目,pom.xml引用的依賴包如下:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.2</version> </dependency>

2、創(chuàng)建Realm和配置shiro

(1)創(chuàng)建Realm

package com.example.demo.config;import org.apache.shiro.authc.*;import org.apache.shiro.authz.AuthorizationInfo;import org.apache.shiro.realm.AuthorizingRealm;import org.apache.shiro.subject.PrincipalCollection;public class MyRealm extends AuthorizingRealm { /**權(quán)限信息,暫不實(shí)現(xiàn)*/ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { return null; } /**身份認(rèn)證:驗(yàn)證用戶輸入的賬號(hào)和密碼是否正確。*/ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { //獲取用戶輸入的賬號(hào) String userName = (String) token.getPrincipal(); //驗(yàn)證用戶admin和密碼123456是否正確 if (!'admin'.equals(userName)) { throw new UnknownAccountException('賬戶不存在!'); } SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName, '123456', getName()); return authenticationInfo; //實(shí)際項(xiàng)目中,上面賬號(hào)從數(shù)據(jù)庫(kù)中獲取用戶對(duì)象,再判斷是否存在 /*User user = userService.findByUserName(userName); if (user == null) { throw new UnknownAccountException('賬戶不存在!'); } SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user,user.getPassword(), getName()); return authenticationInfo; */ }}

(2)配置Shiro

package com.example.demo.config;import org.apache.shiro.spring.web.ShiroFilterFactoryBean;import org.apache.shiro.web.mgt.DefaultWebSecurityManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.LinkedHashMap;import java.util.Map;@Configurationpublic class ShiroConfig { @Bean MyRealm myRealm() { return new MyRealm(); } @Bean DefaultWebSecurityManager securityManager() { DefaultWebSecurityManager manager = new DefaultWebSecurityManager(); manager.setRealm(myRealm()); return manager; } @Bean ShiroFilterFactoryBean shiroFilterFactoryBean() { ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean(); bean.setSecurityManager(securityManager()); //如果不設(shè)置默認(rèn)會(huì)自動(dòng)尋找Web工程根目錄下的'/login.jsp'頁(yè)面 bean.setLoginUrl('/login'); //登錄成功后要跳轉(zhuǎn)的鏈接 bean.setSuccessUrl('/index'); //未授權(quán)界面 bean.setUnauthorizedUrl('/403'); //配置不會(huì)被攔截的鏈接 Map<String, String> map = new LinkedHashMap<>(); map.put('/doLogin', 'anon'); map.put('/**', 'authc'); bean.setFilterChainDefinitionMap(map); return bean; }}

3、控制器測(cè)試方法

package com.example.demo.controller;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.UnknownAccountException;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.subject.Subject;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class LoginController { @GetMapping('/login') public String login() { return '登錄頁(yè)面...'; } @PostMapping('/doLogin') public String doLogin(String userName, String password) { Subject subject = SecurityUtils.getSubject(); try { subject.login(new UsernamePasswordToken(userName, password)); return '登錄成功!'; } catch (UnknownAccountException e) { return e.getMessage(); } catch (AuthenticationException e) { return '登陸失敗,密碼錯(cuò)誤!'; } } //如果沒(méi)有先登陸,訪問(wèn)會(huì)跳到/login @GetMapping('/index') public String index() { return 'index'; } @GetMapping('/403') public String unauthorizedRole(){ return '沒(méi)有權(quán)限'; }}

方法二:引入依賴包shiro-spring-boot-web-starter

1、pom.xml中刪除shiro-spring,引入shiro-spring-boot-web-starter

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-web-starter</artifactId> <version>1.4.2</version> </dependency>

2、創(chuàng)建Realm和配置shiro

(1)創(chuàng)建Realm,代碼和方法一的一樣。

(2)配置Shiro

package com.example.demo.config;import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;import org.apache.shiro.web.mgt.DefaultWebSecurityManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class ShiroConfig { @Bean MyRealm myRealm() { return new MyRealm(); } @Bean DefaultWebSecurityManager securityManager() { DefaultWebSecurityManager manager = new DefaultWebSecurityManager(); manager.setRealm(myRealm()); return manager; } @Bean ShiroFilterChainDefinition shiroFilterChainDefinition() { DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition(); definition.addPathDefinition('/doLogin', 'anon'); definition.addPathDefinition('/**', 'authc'); return definition; }}

(3)application.yml配置

shiro: unauthorizedUrl: /403 successUrl: /index loginUrl: /login

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 久久66热re国产毛片基地 | 亚洲高清国产拍精品影院 | 老黄色片| 国产精品情人露脸在线观看 | 天堂黄网 | 特毛片 | 亚洲影视精品 | 亚洲一区二区三区在线观看蜜桃 | 日韩精品一 | 久草在线视频播放 | 久久婷婷色香五月综合激情 | 欧美嘿咻| 国产精品高清一区二区三区不卡 | 日韩中文字幕一在线 | 97久久久久 | 丁香婷婷成人 | 一级做性色a爰片久久毛片 一级做性色a爰片久久毛片免费 | 日韩一级影院 | 欧美毛片aaaaa片久久久久 | 欧美成人h版整片合集 | 国产人妖一区二区 | 97se狠狠狠狠狼亚洲综合网 | 亚洲精品中文一区不卡 | 免费在线看黄 | 成人免费xxx色视频 成人免费短视频 | 精品日产一区二区三区手机 | 日本韩国一级片 | 国产毛片在线高清视频 | 逼逼好嫩视频 | a一级视频 | 久久我们这里只有精品国产4 | 免费国产视频在线观看 | 日韩欧美一区二区不卡 | 激情综合网婷婷 | 日韩3级 | 欧美黄一片 | 日本无卡αv免费视频 | 欧美日韩国产高清 | 国产成 人 色综合 亚洲 | 亚洲精品久久久久综合91 | 精品在线免费播放 |