SpringBoot 整合 Shiro 密碼登錄的實現(xiàn)代碼
導(dǎo)入依賴(pom.xml)
<!--整合Shiro安全框架-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<!--集成jwt實現(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)置過濾器
/*
* anon:無需認(rèn)證就可以訪問
* authc:必須認(rèn)證才能訪問
* user:必須擁有 記住我 功能才能用
* perms:擁有對某個資源的權(quán)限能訪問
* role:擁有某個角色權(quán)限能訪問
*/
Map<String, String> filterMap = new LinkedHashMap<>();
// 放行不需要權(quán)限認(rèn)證的接口
//放行登錄接口
filterMap.put("/login/**", "anon");
//放行用戶接口
filterMap.put("/", "anon"); // 網(wǎng)站首頁
//認(rèn)證管理員接口
filterMap.put("/administrators/**", "authc");
factoryBean.setFilterChainDefinitionMap(filterMap);
// 設(shè)置無權(quán)限時跳轉(zhuǎn)的 url
// 設(shè)置登錄的請求
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)建密碼登錄時驗證授權(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ù)庫 查詢用戶數(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會話 管理員名字
session.setAttribute("adname", username);
return "admin";
} catch (UnknownAccountException e) {
model.addAttribute("usererror", "用戶名錯誤!請重新輸入。");
return "login";
} catch (IncorrectCredentialsException ice) {
model.addAttribute("pwerror", "密碼錯誤!請重新輸入。");
return "login";
}
}
到此這篇關(guān)于SpringBoot 整合 Shiro 密碼登錄的實現(xiàn)代碼的文章就介紹到這了,更多相關(guān)SpringBoot 整合 Shiro 密碼登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot 整合 Shiro 密碼登錄與郵件驗證碼登錄功能(多 Realm 認(rèn)證)
- Springboot+Shiro記錄用戶登錄信息并獲取當(dāng)前登錄用戶信息的實現(xiàn)代碼
- 基于springboot實現(xiàn)整合shiro實現(xiàn)登錄認(rèn)證以及授權(quán)過程解析
- springboot整合shiro登錄失敗次數(shù)限制功能的實現(xiàn)代碼
- SpringBoot整合Shiro實現(xiàn)登錄認(rèn)證的方法
- SpringBoot+Shiro學(xué)習(xí)之密碼加密和登錄失敗次數(shù)限制示例
- springboot整合shiro多驗證登錄功能的實現(xiàn)(賬號密碼登錄和使用手機(jī)驗證碼登錄)
相關(guān)文章
Spring Boot 中使用 JSON Schema 校驗復(fù)雜JSO
在數(shù)據(jù)交換領(lǐng)域,JSON Schema 以其強(qiáng)大的標(biāo)準(zhǔn)化能力,為定義和規(guī)范 JSON 數(shù)據(jù)的結(jié)構(gòu)與規(guī)則提供了有力支持,下面給大家介紹Spring Boot 中使用 JSON Schema 校驗復(fù)雜JSON數(shù)據(jù)的過程,感興趣的朋友跟隨小編一起看看吧2024-08-08
MyBatis批量更新(update foreach)報錯問題
這篇文章主要介紹了MyBatis批量更新(update foreach)報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Mybatis日期格式自動轉(zhuǎn)換需要用到的兩個注解說明
這篇文章主要介紹了Mybatis日期格式自動轉(zhuǎn)換需要用到的兩個注解說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
淺談SpringSecurity注解與AOP切面執(zhí)行順序
這篇文章主要介紹了淺談SpringSecurity注解與AOP切面執(zhí)行順序,引入Spring Security后,在Controller的方法中會出現(xiàn)Spring Security的方法注解與AOP同時存在的問題,這是就會設(shè)計順序問題,需要的朋友可以參考下2023-10-10

