您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何在Spring boot加入shiro支持,文中通過示例代碼介紹的非常詳細(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)我們寫的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" /> <!-- 配置我們的登錄請(qǐng)求地址 --> <property name="loginUrl" value="/toLogin" /> <!-- 配置我們?cè)诘卿涰摰卿洺晒蟮奶D(zhuǎn)地址,如果你訪問的是非/login地址,則跳到您訪問的地址 --> <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是允許通過 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類處理 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類處理了,所以我們要有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; } }
登錄驗(yàn)證在doGetAuthenticationInfo方法寫入
// 獲取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(); } //上面都通過了就說明該用戶存在并且密碼相等 // 驗(yàn)證成功了 SecurityUtils.getSubject().getSession().setAttribute(Constant.SESSION_USER_KEY, user); // 返回shiro用戶信息 // token傳過來的密碼,一定要跟驗(yàn)證信息傳進(jìn)去的密碼一致,加密的密碼一定要加密后傳過來 return new SimpleAuthenticationInfo(user, user.getPwd(), getName());
如果要設(shè)置權(quán)限,就在對(duì)應(yīng)的Controllerf方法加上
@RequiresPermissions("/system/user/list")
再doGetAuthorizationInfo方法內(nèi)寫
//創(chuàng)建簡(jiǎn)單的授權(quán)信息對(duì)象 SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo(); //授予權(quán)限 simpleAuthorizationInfo.addStringPermission("/system/user/list"); return simpleAuthorizationInfo;
當(dāng)所有Controller都加了@RequiresPermissions注解后,如果訪問到?jīng)]有授權(quán)的Controller會(huì)報(bào)錯(cuò)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。