欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Shiro在springboot中快速實(shí)現(xiàn)方法

 更新時(shí)間:2023年02月07日 16:38:50   作者:落影離殤  
Apache Shiro是一個(gè)Java的安全(權(quán)限)框架,可以容易的開發(fā)出足夠好的應(yīng)用,既可以在JavaEE中使用,也可以在JavaSE中使用,這篇文章主要介紹了Shiro在springboot中快速實(shí)現(xiàn),需要的朋友可以參考下

一、shiro使用必須了解的知識(shí)

1、shiro是什么?

  • 1、Apache Shiro是一個(gè)Java的安全(權(quán)限)框架

  • 2、可以容易的開發(fā)出足夠好的應(yīng)用,既可以在JavaEE中使用,也可以在JavaSE中使用

  • 3、shiro可以完成,認(rèn)證、授權(quán)、加密、會(huì)話管理,web集成、緩存等

2、shiro架構(gòu)三個(gè)常用三大核心對(duì)象

  • Subject:用戶

  • SecurityManager:管理所有用戶

  • Readim:連接數(shù)據(jù)

3、在springboot中使用時(shí),主要可將其看作兩個(gè)模塊(請(qǐng)求過濾模塊、認(rèn)證授權(quán)模塊)

1、認(rèn)證授權(quán)模塊:在認(rèn)證授權(quán)模塊中主要包含兩個(gè)方面,分別是認(rèn)證和授權(quán)。認(rèn)證就是指對(duì)用戶登錄的情況進(jìn)行判定;授權(quán)就是指對(duì)當(dāng)前用戶所擁有的角色、權(quán)限進(jìn)行獲取并將其交給AuthoriztionInfo,使其能夠?qū)⑾嚓P(guān)信息交給Shiro
2、請(qǐng)求過濾模塊:根據(jù)當(dāng)前用戶所擁有的權(quán)限、角色等信息來進(jìn)行判斷是否具有請(qǐng)求的權(quán)限(即是否能夠請(qǐng)求當(dāng)前要訪問的地址),如果該用戶具有訪問當(dāng)前請(qǐng)求地址的權(quán)限,則放行,否則進(jìn)行攔截
3、以上是使用shiro框架進(jìn)行權(quán)限認(rèn)證攔截的最基本實(shí)現(xiàn),此外還可以通過對(duì)密碼進(jìn)行加密,登錄次數(shù)限流(redis)等功能重寫來按照自己實(shí)際業(yè)務(wù)情況進(jìn)行學(xué)習(xí)

4、依賴

<!--        后臺(tái)攔截-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.4.0</version>
        </dependency>

二、具體使用

1、編寫配置類(config)

1.1、Shiro過濾對(duì)象(ShiroFilterFactoryBean)

@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier(SecurityManager) DefaultWebSecurityManager securityManager){
   	ShiroFilterFactiryBean bean = new ShiroFilterFactoryBean()
	//關(guān)聯(lián)SecurityManager設(shè)置安全管理器	
 	bean.setSecurityManager(securityManager)
    
	//添加內(nèi)置過濾器
        /*
        	anon:無需過濾就可以訪問
            authc:必須認(rèn)證了才可訪問(登錄后才可訪問)
            user:必須擁有"記住我"功能才可訪問
            perms:擁有對(duì)某個(gè)資源的權(quán)限才可以訪問
            role:擁有某個(gè)角色權(quán)限才可訪問
        */
  	Map<String,String> filterMap = new LinkedHashMap<>();
    //攔截 
    //filterMap.put("頁面地址","內(nèi)置過濾器")
	//filterMap.put("/user/name","anon")
	//filterMap.put("/user/book","authc")
    
	//具有user:add權(quán)限時(shí)才可以訪問/user/name
    //perms中的“user:add”與數(shù)據(jù)庫中對(duì)應(yīng)權(quán)限要一致
    filterMap.put("/user/name","perms[user:add]")
    
	//授權(quán),正常情況下,沒有授權(quán)會(huì)跳轉(zhuǎn)到未授權(quán)頁面
 	bean.setUnauthorizedUrl("未授權(quán)時(shí)跳轉(zhuǎn)的頁面")  
        
  	//創(chuàng)建一個(gè)過濾器鏈(其中內(nèi)容通過Map存儲(chǔ))
 	bean.setFilterChainDefinitionMap(FilterMap); 
    //設(shè)置登錄請(qǐng)求(登錄的地址添加,當(dāng)使用"authc"時(shí),如果未登錄,則跳轉(zhuǎn)到登錄頁面)
    bean.setLoginUrl("/login")
	return bean;
}

1.2、Shiro安全對(duì)象(DefaultWebSecurity)

//@Qualifier:引入bena對(duì)象
@Bean(name="SecurityManager")
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("MyRealm") MyRealm myRealm){
    DefaultWebSecurityManager securityManager = new DefaultWebSecurotyManager();
    //關(guān)聯(lián)MyRealm
    securityManager.setRealm(myRealm);
    return securityManager;
}

1.3、創(chuàng)建realm對(duì)象(自定義)

//將自定義的realm對(duì)象交給spring
//@Bean(name="MyRealm")中name屬性不加默認(rèn)名稱為方法名
@Bean(name="MyRealm")
public MyRealm MyRealm(){
 	return new MyRealm();
}

2、創(chuàng)建realm對(duì)象

2.1、自定義realm類去繼承AuthorizingRealm類

class MyRealm extends AuthorizingRealm

2.2、重寫AuthorizingRealm中的方法

授權(quán):

project AthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals){
    //1、權(quán)限信息對(duì)象info,用來存放查出的用戶的所有的角色(role)及權(quán)限(permission)
 	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    //2、拿到當(dāng)前登錄的對(duì)象信息,通過認(rèn)證方法SimpleAuthenticationInfo(第一個(gè)參數(shù))已經(jīng)進(jìn)行存入 
    User user =(user)SecurityUtils.getSubject().getPrincipal();
    //3、將該對(duì)象的角色信息進(jìn)行存入
    // 賦予角色
	List<Role> roleList = roleService.listRolesByUserId(userId);
	for (Role role : roleList) {
		info.addRole(role.getName());
	}
    //4、設(shè)置該用戶的權(quán)限
    infO.addStringPermission(user.getPerms())
    //5、將該對(duì)象的權(quán)限信息進(jìn)行存入(permissionSet一個(gè)權(quán)限信息的集合)
    info.setStringPermissions(permissionSet);
    return info;
}

認(rèn)證:

project AuthenticationInfo doGetAuthorizationInfo(AuthenticationToken token){
    //1、拿到用戶登陸的信息
    UsernamePasswordToken userToken =(UsernamePasswordToken) token;
    //2、通過用戶名(userToken.getUsername)獲取數(shù)據(jù)庫中的對(duì)象user
    //如果獲取對(duì)象user為空則該用戶不從在,返回return null(拋出用戶不存在異常)
    if (user == null) {
            throw new UnknownAccountException("賬號(hào)不存在!");
        	//或直接 return null;
        }
    //3、密碼認(rèn)證,有shiro完成(AuthenticationInfo是一個(gè)接口,SimpleAuthenticationInfo是其接口的實(shí)現(xiàn)類)
    //也可對(duì)密碼進(jìn)行加密 如MD5 MD5鹽值
    return new SimpleAuthenticationInfo("用戶對(duì)象信息(user)","通過用戶從數(shù)據(jù)庫中獲得的用戶密碼(user.password)","")
}

3、登錄用戶的信息傳入(通過controller獲取登錄的請(qǐng)求信息)

//獲取當(dāng)前用戶
Subject subject = SecurityUtils.getSubject();
//封裝用戶的登錄數(shù)據(jù)(username:用戶登陸時(shí)傳入的賬號(hào);password:用戶登陸時(shí)傳入的密碼)
UsernamePasswordToken token = new UsernamePasswordToken(username,password);
//執(zhí)行登錄(如果有異常則登錄失敗,沒有異常則登錄成功,在Shiro中已經(jīng)為我們封裝了登錄相關(guān)的異常,直接使用即可)
try{
    subject.login(token);//執(zhí)行登錄成功后
    return "首頁"
}catch(UnknowAccountException e){//用戶名不存在
    return "login"
}catch(IncorrectCredentialsException e){//密碼不存在
    return "login"
}
注意:該方法中登錄失敗后返回的是跳轉(zhuǎn)的頁面,故不可用@ResponseBody

三、具體實(shí)現(xiàn)

1、realm實(shí)現(xiàn)

package com.lingmeng.shiro;
 
import com.lingmeng.pojo.entity.Admin;
import com.lingmeng.pojo.entity.Permission;
import com.lingmeng.pojo.entity.Role;
import com.lingmeng.pojo.resp.BaseResp;
import com.lingmeng.service.AdminService;
import com.lingmeng.service.RoleService;
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;
 
import java.util.HashSet;
import java.util.Set;
 
public class MyRealm extends AuthorizingRealm {
 
    @Autowired
    RoleService roleService;
 
    @Autowired
    AdminService adminService;
 
    //授權(quán)
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //獲取用戶信息
        Subject subject = SecurityUtils.getSubject();
        Admin admin =(Admin) subject.getPrincipal();
        //獲取用戶的權(quán)限及角色信息
        BaseResp baseResp = roleService.selectOne(admin.getUsername());
        Role role = (Role) baseResp.getData();
        //將獲取的角色及權(quán)限進(jìn)行存入
        if (role!=null){
            //角色存入
            info.addRole(role.getName());
            //權(quán)限信息進(jìn)行存入
            Set<String> perms = new HashSet<>();
            for (Permission perm : role.getPerms()) {
                perms.add(perm.getUrl());
            }
            info.setStringPermissions(perms);
        }
        return info;
    }
 
    //認(rèn)證
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //獲取登錄信息(登錄的賬號(hào))
        String username =(String)authenticationToken.getPrincipal();
//      UsernamePasswordToken userToken =(UsernamePasswordToken) authenticationToken;拿到登錄時(shí)傳入的賬號(hào)和密碼對(duì)象
        //從數(shù)據(jù)庫中查詢?cè)搶?duì)象的信息
        Admin admin = adminService.selectOne(username);
        if (admin==null){
            throw new UnknownAccountException("賬號(hào)不存在");
        }
        return new SimpleAuthenticationInfo(admin,admin.getPassword(),this.getName());
    }
}

2、controller實(shí)現(xiàn)

package com.lingmeng.controller;
 
import com.lingmeng.pojo.entity.Admin;
import com.lingmeng.pojo.resp.BaseResp;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;
 
@RestController
public class AdminController {
 
    @PostMapping("background/login")
    public BaseResp  login(@RequestBody Admin admin){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(admin.getUsername(), admin.getPassword());
        try{
            subject.login(token);
            return BaseResp.SUCCESS("登錄成功",null,null);
        }catch (UnknownAccountException e){//賬號(hào)不存在
            return BaseResp.FAIL(201,"賬號(hào)不存在");
        }catch(IncorrectCredentialsException incorrectCredentialsException){//密碼錯(cuò)誤
            return BaseResp.FAIL(201,"密碼錯(cuò)誤") ;
        }
    }
 
    @GetMapping("/background/exitLogin")
    public BaseResp exitLogin(){
        Subject subject = SecurityUtils.getSubject();
        System.out.println(subject.getPrincipal());
        try{
            subject.logout();//退出登錄
            return BaseResp.SUCCESS("退出登錄",null,null);
        }catch(Exception e){
            return BaseResp.FAIL(202,"退出失敗");
        }
    }
}

3、config實(shí)現(xiàn)

package com.lingmeng.config;
 
import com.lingmeng.shiro.MyRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.util.LinkedHashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
 
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager);
        //配置請(qǐng)求攔截并存入map中
         /*
        	anon:無需過濾就可以訪問
            authc:必須認(rèn)證了才可訪問(登錄后才可訪問)
            user:必須擁有"記住我"功能才可訪問
            perms:擁有對(duì)某個(gè)資源的權(quán)限才可以訪問
            role:擁有某個(gè)角色權(quán)限才可訪問
        */
        Map<String, String> map = new LinkedHashMap<>();
        map.put("/background/**","authc");
        map.put("background/login","anon");
 
 
        bean.setFilterChainDefinitionMap(map);
        //設(shè)置未授權(quán)跳轉(zhuǎn)地址
        bean.setUnauthorizedUrl("");
        //設(shè)置登錄地址
        bean.setLoginUrl("/background/login");
        return bean;
    }
 
    @Bean("securityManager")
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("myRealm") MyRealm myRealm){
        return new DefaultWebSecurityManager(myRealm);
    }
 
    @Bean()
    public MyRealm myRealm(){
        return new MyRealm();
    }
}

以上是一些shiro在springboot中的基本用法,希望能夠?qū)Υ蠹覍W(xué)習(xí)有所幫助(代碼中的實(shí)體,角色,權(quán)限根據(jù)自己數(shù)據(jù)庫查詢結(jié)果進(jìn)行替換即可)

到此這篇關(guān)于Shiro在springboot中快速實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot實(shí)現(xiàn)Shiro內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SPRINGMVC JSON數(shù)據(jù)交互如何實(shí)現(xiàn)

    SPRINGMVC JSON數(shù)據(jù)交互如何實(shí)現(xiàn)

    這篇文章主要介紹了SPRINGMVC JSON數(shù)據(jù)交互如何實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 圖文詳解OkHttp的超時(shí)時(shí)間

    圖文詳解OkHttp的超時(shí)時(shí)間

    HTTP是現(xiàn)代應(yīng)用常用的一種交換數(shù)據(jù)和媒體的網(wǎng)絡(luò)方式,高效地使用HTTP能讓資源加載更快,節(jié)省帶寬,OkHttp是一個(gè)高效的HTTP客戶端,下面這篇文章主要給大家介紹了關(guān)于OkHttp超時(shí)時(shí)間的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • java中Hashtable和HashMap的區(qū)別分析

    java中Hashtable和HashMap的區(qū)別分析

    java中Hashtable和HashMap的區(qū)別分析,需要的朋友可以參考一下
    2013-04-04
  • Java 根據(jù)某個(gè) key 加鎖的實(shí)現(xiàn)方式

    Java 根據(jù)某個(gè) key 加鎖的實(shí)現(xiàn)方式

    日常開發(fā)中,有時(shí)候需要根據(jù)某個(gè) key 加鎖,確保多線程情況下,對(duì)該 key 的加鎖和解鎖之間的代碼串行執(zhí)行,這篇文章主要介紹了Java 根據(jù)某個(gè) key 加鎖的實(shí)現(xiàn)方式,需要的朋友可以參考下
    2023-03-03
  • Java集合Iterator迭代的實(shí)現(xiàn)方法

    Java集合Iterator迭代的實(shí)現(xiàn)方法

    這篇文章主要介紹了Java集合Iterator迭代接口的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒家,對(duì)Java 結(jié)合iterator知識(shí)感興趣的朋友一起看看吧
    2016-08-08
  • Java實(shí)現(xiàn)的properties文件動(dòng)態(tài)修改并自動(dòng)保存工具類

    Java實(shí)現(xiàn)的properties文件動(dòng)態(tài)修改并自動(dòng)保存工具類

    這篇文章主要介紹了Java實(shí)現(xiàn)的properties文件動(dòng)態(tài)修改并自動(dòng)保存工具類,可實(shí)現(xiàn)針對(duì)properties配置文件的相關(guān)修改與保存功能,需要的朋友可以參考下
    2017-11-11
  • mapper接口注入兩種方式詳解

    mapper接口注入兩種方式詳解

    這篇文章主要介紹了mapper接口注入兩種方式詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • spring mvc實(shí)現(xiàn)文件上傳與下載功能

    spring mvc實(shí)現(xiàn)文件上傳與下載功能

    這篇文章主要為大家詳細(xì)介紹了spring mvc實(shí)現(xiàn)文件上傳與下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 解決springboot bean中大寫的字段返回變成小寫的問題

    解決springboot bean中大寫的字段返回變成小寫的問題

    這篇文章主要介紹了解決springboot bean中大寫的字段返回變成小寫的問題,具有很好的參考價(jià)值希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • struts2通過action返回json對(duì)象

    struts2通過action返回json對(duì)象

    struts2通過action返回json對(duì)象其實(shí)很簡單的,首先我們需要引入jar包,然后在寫一個(gè)簡單的action就好了,接下來通過本文給大家介紹struts2通過action返回json對(duì)象的方法,感興趣的朋友一起看看吧
    2016-09-09

最新評(píng)論