SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn)代碼
導(dǎo)入依賴(pom.xml)
<!--整合Shiro安全框架-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<!--集成jwt實(shí)現(xiàn)token認(rèn)證-->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.2.0</version>
</dependency>
創(chuàng)建 ShiroConfig 配置類
@Configuration
public class ShiroConfig {
/**
* ShiroFilterFactoryBean
*/
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
//設(shè)置安全管理器
factoryBean.setSecurityManager(defaultWebSecurityManager);
// 添加shiro的內(nèi)置過(guò)濾器
/*
* anon:無(wú)需認(rèn)證就可以訪問(wèn)
* authc:必須認(rèn)證才能訪問(wèn)
* user:必須擁有 記住我 功能才能用
* perms:擁有對(duì)某個(gè)資源的權(quán)限能訪問(wèn)
* role:擁有某個(gè)角色權(quán)限能訪問(wèn)
*/
Map<String, String> filterMap = new LinkedHashMap<>();
// 放行不需要權(quán)限認(rèn)證的接口
//放行登錄接口
filterMap.put("/login/**", "anon");
//放行用戶接口
filterMap.put("/", "anon"); // 網(wǎng)站首頁(yè)
//認(rèn)證管理員接口
filterMap.put("/administrators/**", "authc");
factoryBean.setFilterChainDefinitionMap(filterMap);
// 設(shè)置無(wú)權(quán)限時(shí)跳轉(zhuǎn)的 url
// 設(shè)置登錄的請(qǐng)求
factoryBean.setLoginUrl("/login/toLogin");
return factoryBean;
}
/**
* 注入 DefaultWebSecurityManager
*/
@Bean(name = "securityManager")
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("customRealm") CustomRealm customRealm) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//關(guān)聯(lián)CustomRealm
securityManager.setRealm(customRealm);
return securityManager;
}
/**
* 注入 securityManager
*/
@Bean
public CustomRealm customRealm() {
return new CustomRealm();
}
}
創(chuàng)建密碼登錄時(shí)驗(yàn)證授權(quán) CustomRealm 類
@Component
public class CustomRealm extends AuthorizingRealm {
@Autowired
AdministratorsService administratorsService;
/*
* 設(shè)置加密方式
*/
{
HashedCredentialsMatcher mather = new HashedCredentialsMatcher();
// 加密方式
mather.setHashAlgorithmName("md5");
// 密碼進(jìn)行一次運(yùn)算
mather.setHashIterations(512);
this.setCredentialsMatcher(mather);
}
/**
* 授權(quán)
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
System.out.println("————授權(quán)————doGetAuthorizationInfo————");
return null;
}
/**
* 認(rèn)證
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
System.out.println("————認(rèn)證————doGetAuthenticationInfo————");
UsernamePasswordToken userToken = (UsernamePasswordToken) token;
// 連接數(shù)據(jù)庫(kù) 查詢用戶數(shù)據(jù)
QueryWrapper<Administrators> wrapper = new QueryWrapper<>();
wrapper.eq("username", userToken.getUsername());
Administrators administrators = administratorsService.getOne(wrapper);
if (administrators == null) {
return null; // 拋出異常 UnknownAccountException
}
// 密碼認(rèn)證,shiro做
return new SimpleAuthenticationInfo("", administrators.getPassword(), "");
}
}
控制層用戶密碼登錄
//用戶名登錄
@ApiOperation(value = "管理員登錄", notes = "用戶名登錄--不進(jìn)行攔截")
@PostMapping("/doLogin")
public String doLogin(@RequestParam("username") String username,
@RequestParam("password") String password,
HttpSession session,Model model) {
// 獲取當(dāng)前的用戶
Subject subject = SecurityUtils.getSubject();
// 封裝用戶的登錄數(shù)據(jù)
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
subject.login(token);
//保存session會(huì)話 管理員名字
session.setAttribute("adname", username);
return "admin";
} catch (UnknownAccountException e) {
model.addAttribute("usererror", "用戶名錯(cuò)誤!請(qǐng)重新輸入。");
return "login";
} catch (IncorrectCredentialsException ice) {
model.addAttribute("pwerror", "密碼錯(cuò)誤!請(qǐng)重新輸入。");
return "login";
}
}
到此這篇關(guān)于SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)SpringBoot 整合 Shiro 密碼登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot 整合 Shiro 密碼登錄與郵件驗(yàn)證碼登錄功能(多 Realm 認(rèn)證)
- Springboot+Shiro記錄用戶登錄信息并獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼
- 基于springboot實(shí)現(xiàn)整合shiro實(shí)現(xiàn)登錄認(rèn)證以及授權(quán)過(guò)程解析
- springboot整合shiro登錄失敗次數(shù)限制功能的實(shí)現(xiàn)代碼
- SpringBoot整合Shiro實(shí)現(xiàn)登錄認(rèn)證的方法
- SpringBoot+Shiro學(xué)習(xí)之密碼加密和登錄失敗次數(shù)限制示例
- springboot整合shiro多驗(yàn)證登錄功能的實(shí)現(xiàn)(賬號(hào)密碼登錄和使用手機(jī)驗(yàn)證碼登錄)
相關(guān)文章
Java Swing程序設(shè)計(jì)實(shí)戰(zhàn)
今天教大家怎么用JavaSwing工具包實(shí)現(xiàn)一個(gè)程序的界面設(shè)計(jì),文中有非常詳細(xì)的代碼示例及注釋,對(duì)正在學(xué)習(xí)Java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
spring-boot中spring-boot-maven-plugin報(bào)紅錯(cuò)誤及解決
這篇文章主要介紹了spring-boot中spring-boot-maven-plugin報(bào)紅錯(cuò)誤及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
spring boot如何基于JWT實(shí)現(xiàn)單點(diǎn)登錄詳解
這篇文章主要介紹了spring boot如何基于JWT實(shí)現(xiàn)單點(diǎn)登錄詳解,用戶只需登錄一次就能夠在這兩個(gè)系統(tǒng)中進(jìn)行操作。很明顯這就是單點(diǎn)登錄(Single Sign-On)達(dá)到的效果,需要的朋友可以參考下2019-06-06
springboot項(xiàng)目訪問(wèn)圖片的3種實(shí)現(xiàn)方法(親測(cè)可用)
本文主要介紹了springboot項(xiàng)目訪問(wèn)圖片的3種實(shí)現(xiàn)方法,通過(guò)springboot項(xiàng)目訪問(wèn)除項(xiàng)目根目錄之外的其它目錄的圖片,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
淺談SpringBoot集成Redis實(shí)現(xiàn)緩存處理(Spring AOP實(shí)現(xiàn))
這篇文章主要介紹了淺談SpringBoot集成Redis實(shí)現(xiàn)緩存處理(Spring AOP實(shí)現(xiàn)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Java?Valhalla?Project項(xiàng)目介紹
這篇文章主要介紹了Java?Valhalla?Project項(xiàng)目介紹,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
SpringBoot自定義注解使用讀寫分離Mysql數(shù)據(jù)庫(kù)的實(shí)例教程
這篇文章主要給大家介紹了關(guān)于SpringBoot自定義注解使用讀寫分離Mysql數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java使用EasyExcel生成動(dòng)態(tài)表頭和多Sheet數(shù)據(jù)的Excel實(shí)例
這篇文章主要介紹了Java使用EasyExcel生成動(dòng)態(tài)表頭和多Sheet數(shù)據(jù)的Excel實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04

