如何在Spring boot加入shiro支持
這篇文章主要介紹了如何在Spring boot加入shiro支持,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
在項(xiàng)目添加依賴
<!-- shiro spring. --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</version> </dependency> <!-- shiro core --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version> </dependency>
配置文件目錄下新建spring文件夾,在文件夾內(nèi)新建spring-shiro.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- ref對(duì)應(yīng)我們寫(xiě)的realm myRealm -->
<property name="realm" ref="AuthRealm" />
<!-- 使用下面配置的緩存管理器 -->
<!-- <property name="cacheManager" ref="shiroEncacheManager" /> -->
</bean>
<!-- 安全認(rèn)證過(guò)濾器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 調(diào)用我們配置的權(quán)限管理器 -->
<property name="securityManager" ref="securityManager" />
<!-- 配置我們的登錄請(qǐng)求地址 -->
<property name="loginUrl" value="/toLogin" />
<!-- 配置我們?cè)诘卿涰?yè)登錄成功后的跳轉(zhuǎn)地址,如果你訪問(wèn)的是非/login地址,則跳到您訪問(wèn)的地址 -->
<property name="successUrl" value="/" />
<!-- 如果您請(qǐng)求的資源不再您的權(quán)限范圍,則跳轉(zhuǎn)到/403請(qǐng)求地址 -->
<property name="unauthorizedUrl" value="/html/403.html" />
<property name="filterChainDefinitions">
<value>
<!-- anon是允許通過(guò) authc相反 -->
/statics/**=anon
/login=anon
/** = authc
</value>
</property>
</bean>
<!-- 保證實(shí)現(xiàn)了Shiro內(nèi)部lifecycle函數(shù)的bean執(zhí)行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- AOP式方法級(jí)權(quán)限檢查 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
</beans>
在主入口加載spring-shiro.xml
@ImportResource({ "classpath:spring/spring-shiro.xml" })
在登錄Controller內(nèi)更改成
//構(gòu)造登錄參數(shù)
UsernamePasswordToken token = new UsernamePasswordToken(name, pwd);
try {
//交給Realm類(lèi)處理
SecurityUtils.getSubject().login(token);
} catch (UnknownAccountException uae) {
map.put("msg", "未知用戶");
return "login";
} catch (IncorrectCredentialsException ice) {
map.put("msg", "密碼錯(cuò)誤");
return "login";
} catch (AuthenticationException ae) {
// unexpected condition? error?
map.put("msg", "服務(wù)器繁忙");
return "login";
}
return "redirect:/toIndex";
看5行就知道登錄交給了Realm類(lèi)處理了,所以我們要有Realm類(lèi)
在可以被主入口掃描到的地方新建AuthRealm類(lèi)并且繼承AuthorizingRealm,重寫(xiě)doGetAuthenticationInfo(登錄邏輯),重寫(xiě)doGetAuthorizationInfo(授權(quán)邏輯)
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
public class AuthRealm extends AuthorizingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
// TODO Auto-generated method stub
return null;
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// TODO Auto-generated method stub
return null;
}
}
登錄驗(yàn)證在doGetAuthenticationInfo方法寫(xiě)入
// 獲取Token
UsernamePasswordToken token2 = (UsernamePasswordToken) token;
//獲取用戶名
String userName = token2.getUsername();
//獲取密碼
String pwd = new String(token2.getPassword());
//下面我使用的是MyBatis-puls3.0
//查詢條件對(duì)象
QueryWrapper<User> queryWrapper = new QueryWrapper();
//查詢?cè)撚脩?
queryWrapper.eq("name", userName).or().eq("phone", userName);
//查詢
User user = iUserService.getOne(queryWrapper);
//查回的對(duì)象為空
if (CommonUtil.isBlank(user)) {
//拋出未知的賬戶異常
throw new UnknownAccountException();
}
//查回的對(duì)象密碼和輸入密碼不相等
if (!CommonUtil.isEquals(user.getPwd(), pwd)) {
//拋出憑證不正確異常
throw new IncorrectCredentialsException();
}
//上面都通過(guò)了就說(shuō)明該用戶存在并且密碼相等
// 驗(yàn)證成功了
SecurityUtils.getSubject().getSession().setAttribute(Constant.SESSION_USER_KEY, user);
// 返回shiro用戶信息
// token傳過(guò)來(lái)的密碼,一定要跟驗(yàn)證信息傳進(jìn)去的密碼一致,加密的密碼一定要加密后傳過(guò)來(lái)
return new SimpleAuthenticationInfo(user, user.getPwd(), getName());
如果要設(shè)置權(quán)限,就在對(duì)應(yīng)的Controllerf方法加上
@RequiresPermissions("/system/user/list")
再doGetAuthorizationInfo方法內(nèi)寫(xiě)
//創(chuàng)建簡(jiǎn)單的授權(quán)信息對(duì)象
SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo();
//授予權(quán)限
simpleAuthorizationInfo.addStringPermission("/system/user/list");
return simpleAuthorizationInfo;
當(dāng)所有Controller都加了@RequiresPermissions注解后,如果訪問(wèn)到?jīng)]有授權(quán)的Controller會(huì)報(bào)錯(cuò)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring Security和Shiro的相同點(diǎn)與不同點(diǎn)整理
- Spring與Shiro整合及加載權(quán)限表達(dá)式問(wèn)題
- 基于springboot實(shí)現(xiàn)整合shiro實(shí)現(xiàn)登錄認(rèn)證以及授權(quán)過(guò)程解析
- SpringBoot + Shiro前后端分離權(quán)限
- shiro整合springboot前后端分離
- Spring boot整合shiro+jwt實(shí)現(xiàn)前后端分離
- spring boot整合shiro安全框架過(guò)程解析
- shiro與spring集成基礎(chǔ)Hello案例詳解
- SpringBoot Shiro授權(quán)實(shí)現(xiàn)過(guò)程解析
- Springboot整合Shiro的代碼實(shí)例
- Spring Boot 整合 Shiro+Thymeleaf過(guò)程解析
- Spring Boot集成Shiro實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限的完整步驟
- SpringBoot中Shiro緩存使用Redis、Ehcache的方法
- SpringBoot2.0整合Shiro框架實(shí)現(xiàn)用戶權(quán)限管理的示例
- Spring Boot 自定義 Shiro 過(guò)濾器無(wú)法使用 @Autowired問(wèn)題及解決方法
- SpringBoot整合Shiro兩種方式(總結(jié))
- Spring配置shiro時(shí)自定義Realm中屬性無(wú)法使用注解注入的解決辦法
- Spring Boot2開(kāi)發(fā)之Spring Boot整合Shiro兩種詳細(xì)方法
相關(guān)文章
Spring MVC的優(yōu)點(diǎn)與核心接口_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Spring MVC的優(yōu)點(diǎn)與核心接口,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Java設(shè)計(jì)模式中的策略模式詳細(xì)解析
這篇文章主要介紹了Java設(shè)計(jì)模式中的策略模式詳細(xì)解析,所謂策略模式,指的是做某一件事時(shí)有多種選擇(即策略),且不同的策略之間相互獨(dú)立,而且無(wú)論使用哪種策略,得到的結(jié)果都是相同的,需要的朋友可以參考下2023-12-12
springboot登錄攔截器+ThreadLocal實(shí)現(xiàn)用戶信息存儲(chǔ)的實(shí)例代碼
ThreadLocal 為變量在每個(gè)線程中創(chuàng)建了一個(gè)副本,這樣每個(gè)線程都可以訪問(wèn)自己內(nèi)部的副本變量,這篇文章主要介紹了springboot登錄攔截器+ThreadLocal實(shí)現(xiàn)用戶信息存儲(chǔ)的實(shí)例代碼,需要的朋友可以參考下2024-03-03
Mybatis中一對(duì)多(collection)和一對(duì)一(association)的組合查詢使用
這篇文章主要介紹了Mybatis中一對(duì)多(collection)和一對(duì)一(association)的組合查詢使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Windows 10上JDK環(huán)境安裝配置圖文教程
這篇文章主要為大家詳細(xì)介紹了Windows 10上JDK環(huán)境安裝配置圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Spring中@Import的各種用法以及ImportAware接口詳解
這篇文章主要介紹了Spring中@Import的各種用法以及ImportAware接口詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

