如何在Spring boot加入shiro支持
這篇文章主要介紹了如何在Spring boot加入shiro支持,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
在項目添加依賴
<!-- 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對應(yīng)我們寫的realm myRealm -->
<property name="realm" ref="AuthRealm" />
<!-- 使用下面配置的緩存管理器 -->
<!-- <property name="cacheManager" ref="shiroEncacheManager" /> -->
</bean>
<!-- 安全認(rèn)證過濾器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 調(diào)用我們配置的權(quán)限管理器 -->
<property name="securityManager" ref="securityManager" />
<!-- 配置我們的登錄請求地址 -->
<property name="loginUrl" value="/toLogin" />
<!-- 配置我們在登錄頁登錄成功后的跳轉(zhuǎn)地址,如果你訪問的是非/login地址,則跳到您訪問的地址 -->
<property name="successUrl" value="/" />
<!-- 如果您請求的資源不再您的權(quán)限范圍,則跳轉(zhuǎn)到/403請求地址 -->
<property name="unauthorizedUrl" value="/html/403.html" />
<property name="filterChainDefinitions">
<value>
<!-- anon是允許通過 authc相反 -->
/statics/**=anon
/login=anon
/** = authc
</value>
</property>
</bean>
<!-- 保證實現(xiàn)了Shiro內(nèi)部lifecycle函數(shù)的bean執(zhí)行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- AOP式方法級權(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類處理
SecurityUtils.getSubject().login(token);
} catch (UnknownAccountException uae) {
map.put("msg", "未知用戶");
return "login";
} catch (IncorrectCredentialsException ice) {
map.put("msg", "密碼錯誤");
return "login";
} catch (AuthenticationException ae) {
// unexpected condition? error?
map.put("msg", "服務(wù)器繁忙");
return "login";
}
return "redirect:/toIndex";
看5行就知道登錄交給了Realm類處理了,所以我們要有Realm類
在可以被主入口掃描到的地方新建AuthRealm類并且繼承AuthorizingRealm,重寫doGetAuthenticationInfo(登錄邏輯),重寫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;
}
}
登錄驗證在doGetAuthenticationInfo方法寫入
// 獲取Token
UsernamePasswordToken token2 = (UsernamePasswordToken) token;
//獲取用戶名
String userName = token2.getUsername();
//獲取密碼
String pwd = new String(token2.getPassword());
//下面我使用的是MyBatis-puls3.0
//查詢條件對象
QueryWrapper<User> queryWrapper = new QueryWrapper();
//查詢該用戶
queryWrapper.eq("name", userName).or().eq("phone", userName);
//查詢
User user = iUserService.getOne(queryWrapper);
//查回的對象為空
if (CommonUtil.isBlank(user)) {
//拋出未知的賬戶異常
throw new UnknownAccountException();
}
//查回的對象密碼和輸入密碼不相等
if (!CommonUtil.isEquals(user.getPwd(), pwd)) {
//拋出憑證不正確異常
throw new IncorrectCredentialsException();
}
//上面都通過了就說明該用戶存在并且密碼相等
// 驗證成功了
SecurityUtils.getSubject().getSession().setAttribute(Constant.SESSION_USER_KEY, user);
// 返回shiro用戶信息
// token傳過來的密碼,一定要跟驗證信息傳進(jìn)去的密碼一致,加密的密碼一定要加密后傳過來
return new SimpleAuthenticationInfo(user, user.getPwd(), getName());
如果要設(shè)置權(quán)限,就在對應(yīng)的Controllerf方法加上
@RequiresPermissions("/system/user/list")
再doGetAuthorizationInfo方法內(nèi)寫
//創(chuàng)建簡單的授權(quán)信息對象
SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo();
//授予權(quán)限
simpleAuthorizationInfo.addStringPermission("/system/user/list");
return simpleAuthorizationInfo;
當(dāng)所有Controller都加了@RequiresPermissions注解后,如果訪問到?jīng)]有授權(quán)的Controller會報錯。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring Security和Shiro的相同點與不同點整理
- Spring與Shiro整合及加載權(quán)限表達(dá)式問題
- 基于springboot實現(xiàn)整合shiro實現(xiàn)登錄認(rèn)證以及授權(quán)過程解析
- SpringBoot + Shiro前后端分離權(quán)限
- shiro整合springboot前后端分離
- Spring boot整合shiro+jwt實現(xiàn)前后端分離
- spring boot整合shiro安全框架過程解析
- shiro與spring集成基礎(chǔ)Hello案例詳解
- SpringBoot Shiro授權(quán)實現(xiàn)過程解析
- Springboot整合Shiro的代碼實例
- Spring Boot 整合 Shiro+Thymeleaf過程解析
- Spring Boot集成Shiro實現(xiàn)動態(tài)加載權(quán)限的完整步驟
- SpringBoot中Shiro緩存使用Redis、Ehcache的方法
- SpringBoot2.0整合Shiro框架實現(xiàn)用戶權(quán)限管理的示例
- Spring Boot 自定義 Shiro 過濾器無法使用 @Autowired問題及解決方法
- SpringBoot整合Shiro兩種方式(總結(jié))
- Spring配置shiro時自定義Realm中屬性無法使用注解注入的解決辦法
- Spring Boot2開發(fā)之Spring Boot整合Shiro兩種詳細(xì)方法
相關(guān)文章
在mybatis 中使用if else 進(jìn)行判斷的操作
這篇文章主要介紹了在mybatis 中使用if else 進(jìn)行判斷的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java實現(xiàn)byte[]轉(zhuǎn)List的示例代碼
byte,即字節(jié),由8位的二進(jìn)制組成。在Java中,byte類型的數(shù)據(jù)是8位帶符號的二進(jìn)制數(shù)。List?是一個接口,它繼承于Collection的接口。它代表著有序的隊列。本文將介紹如何通過java實現(xiàn)byte[]轉(zhuǎn)List,需要的可以參考一下2022-01-01
Java多線程中wait?notify等待喚醒機(jī)制詳解
這篇文章主要介紹了Java多線程中wait?notify等待喚醒機(jī)制,由于線程之間是搶占式執(zhí)行的,因此線程的執(zhí)行順序難以預(yù)知,但是實際開發(fā)中有時候我們希望合理的協(xié)調(diào)多個線程之間的執(zhí)行先后順序,所以這里我們來介紹下等待喚醒機(jī)制,需要的朋友可以參考下2024-10-10
JDBC數(shù)據(jù)源連接池配置及應(yīng)用
這篇文章主要介紹JDBC建立數(shù)據(jù)庫連接的兩種方式,使用配置數(shù)據(jù)源的方式連接數(shù)據(jù)庫,效率更高,推薦使用,希望能給大家做一個參考。2016-06-06
Debian 7 和 Debian 8 用戶安裝 Java 8的方法
Oracle Java 8 穩(wěn)定版本近期已發(fā)布,有很多新的特征變化。其中,有功能的程序支持通過“Lambda項目 ”,收到了一些安全更新和界面改進(jìn)上的bug修復(fù),使得開發(fā)人員的工作更容易。2014-03-03

