Springboot整合Shiro的代碼實(shí)例
這篇文章主要介紹了Springboot整合Shiro的代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1、導(dǎo)入依賴
<!--shiro--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</version> </dependency>
2、創(chuàng)建ShiroRealm.java文件
(這里按照需求,只做登錄認(rèn)證這塊)
package com.hyqfx.manager.shiro;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hyqfx.manager.entity.po.SystemAdmin;
import com.hyqfx.manager.service.ISystemAdminService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
public class ShiroRealm extends AuthorizingRealm {
@Autowired
private ISystemAdminService adminService;
//授權(quán)
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
/*
//獲取登錄用戶名
String name= (String) principalCollection.getPrimaryPrincipal();
//查詢用戶名稱
User user = loginService.findByName(name);
//添加角色和權(quán)限
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
for (Role role:user.getRoles()) {
//添加角色
simpleAuthorizationInfo.addRole(role.getRoleName());
for (Permission permission:role.getPermissions()) {
//添加權(quán)限
simpleAuthorizationInfo.addStringPermission(permission.getPermission());
}
}
return simpleAuthorizationInfo;*/
return null;
}
//認(rèn)證
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//加這一步的目的是在Post請(qǐng)求的時(shí)候會(huì)先進(jìn)認(rèn)證,然后在到請(qǐng)求
if (authenticationToken.getPrincipal() == null) {
return null;
}
//獲取用戶信息
String name = authenticationToken.getPrincipal().toString();
SystemAdmin admin = adminService.selectOne(new EntityWrapper<SystemAdmin>().eq("username",name));
if (admin == null) {
return null;
} else {
//這里驗(yàn)證authenticationToken和simpleAuthenticationInfo的信息
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(name, admin.getPassword().toString(), getName());
return simpleAuthenticationInfo;
}
}
}
3、創(chuàng)建ShiroConfiguration.java文件
package com.becl.config;
import com.becl.shiro.PasswordMatcher;
import com.becl.shiro.ShiroRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class ShiroConfiguration {
//將自己的驗(yàn)證方式加入容器
@Bean
public ShiroRealm myShiroRealm() {
ShiroRealm myShiroRealm = new ShiroRealm();
myShiroRealm.setCredentialsMatcher(passwordMatcher());//裝配自定義的密碼驗(yàn)證方式
return myShiroRealm;
}
// 配置加密方式
// 配置了一下,這貨就是驗(yàn)證不過,,改成手動(dòng)驗(yàn)證算了,以后換加密方式也方便
@Bean
public PasswordMatcher passwordMatcher() {
return new PasswordMatcher();
}
//權(quán)限管理,配置主要是Realm的管理認(rèn)證
@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(myShiroRealm());
return securityManager;
}
//Filter工廠,設(shè)置對(duì)應(yīng)的過濾條件和跳轉(zhuǎn)條件
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
Map<String,String> map = new HashMap<String, String>();
//登出
map.put("/logout","logout");
//不需要認(rèn)證
map.put("/logout","anon");
map.put("/login*","anon");
map.put("/shiroError","anon");
//對(duì)所有用戶認(rèn)證
map.put("/**","authc");
//map.put("/**","anon");
//登錄
shiroFilterFactoryBean.setLoginUrl("/login");
//首頁
shiroFilterFactoryBean.setSuccessUrl("/index");
//錯(cuò)誤頁面,認(rèn)證不通過跳轉(zhuǎn)
shiroFilterFactoryBean.setUnauthorizedUrl("/shiroError");
shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
return shiroFilterFactoryBean;
}
//加入注解的使用,不加入這個(gè)注解不生效
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}
}
4、自定義Shiro的密碼比較器
package com.becl.shiro;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.mindrot.jbcrypt.BCrypt;
/**
* 自定義密碼比較器
*/
public class PasswordMatcher extends SimpleCredentialsMatcher {
@Override
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
UsernamePasswordToken utoken=(UsernamePasswordToken) token;
//獲得用戶輸入的密碼:(可以采用加鹽(salt)的方式去檢驗(yàn))
String inPassword = new String(utoken.getPassword());
String username = utoken.getUsername();
//獲得數(shù)據(jù)庫中的密碼
String dbPassword = (String) info.getCredentials();
//進(jìn)行密碼的比對(duì)
boolean flag = BCrypt.checkpw(inPassword,dbPassword);
return flag;
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java使用xfire搭建webservice服務(wù)的過程詳解
這篇文章主要介紹了java使用xfire搭建webservice服務(wù)的過程。使用xfire搭建webService的服務(wù),可以在瀏覽器訪問。對(duì)此感興趣的可以了解一下2020-07-07
淺析Bean?Searcher?與?MyBatis?Plus?區(qū)別介紹
Bean?Searcher號(hào)稱任何復(fù)雜的查詢都可以一行代碼搞定,但?Mybatis?Plus?似乎也有類似的動(dòng)態(tài)查詢功能,最近火起的?Bean?Searcher?與?MyBatis?Plus?倒底有啥區(qū)別?帶著這個(gè)問題一起通過本文學(xué)習(xí)下吧2022-05-05
SpringBoot前后端分離解決跨域問題的3種解決方案總結(jié)
前后端分離大勢所趨,跨域問題更是老生常談,下面這篇文章主要給大家介紹了SpringBoot前后端分離解決跨域問題的3種解決方案,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
SpringBoot?AOP?Redis實(shí)現(xiàn)延時(shí)雙刪功能實(shí)戰(zhàn)
本文主要介紹了SpringBoot?AOP?Redis實(shí)現(xiàn)延時(shí)雙刪功能實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
springboot配置Jackson返回統(tǒng)一默認(rèn)值的實(shí)現(xiàn)示例
在項(xiàng)目開發(fā)中,我們返回的數(shù)據(jù)或者對(duì)象沒有的時(shí)候一般直接返回的null,那么如何返回統(tǒng)一默認(rèn)值,感興趣的可以了解一下2021-07-07
Spring Boot中擴(kuò)展XML請(qǐng)求與響應(yīng)的支持詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot中擴(kuò)展XML請(qǐng)求與響應(yīng)的支持的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
解決get請(qǐng)求入?yún)NotNull驗(yàn)證不生效問題
這篇文章主要介紹了解決get請(qǐng)求入?yún)NotNull驗(yàn)證不生效問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09

