Spingboot的Shiro的配置
Config
import org.apache.shiro.cache.ehcache.EhCacheManager;
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.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;
import javax.annotation.Resource;
import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Configuration
public class ShiroConfig {
/***
* 具體的驗證規則實現類
*/
@Resource
ShiroRealm shiroRealm;
@Bean
public EhCacheManager ehCacheManager() {
return null;
}
@Bean
public LoginFilterShiro loginFilterShiro() {
return new LoginFilterShiro();
}
@Bean
public FilterRegistrationBean delegatingFilterProxy() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
DelegatingFilterProxy proxy = new DelegatingFilterProxy();
proxy.setTargetFilterLifecycle(true);
proxy.setTargetBeanName("shiroFilter");
filterRegistrationBean.setFilter(proxy);
return filterRegistrationBean;
}
/***
* 許可權管理
* @return
*/
@Bean
public SecurityManager securityManager() {
log.info("----------------載入shiro許可權管理器---------------");
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
defaultWebSecurityManager.setRealm(shiroRealm);
return defaultWebSecurityManager;
}
/***
* Shiro過濾器,用於過濾相關請求
* @param securityManager
* @return
*/
@Bean("shiroFilter")
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
log.info("----------------載入shiro許可權過濾器---------------");
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setLoginUrl("/account/unauth");
shiroFilterFactoryBean.setSecurityManager(securityManager);
Map<String, Filter> filterMap = new HashMap<>();
filterMap.put("authc", new LoginFormAuthenticationFilter());
shiroFilterFactoryBean.setFilters(filterMap);
Map<String, String> pathMap = new HashMap<>();
pathMap.put("/js/**", "anon");
pathMap.put("/images/**", "anon");
pathMap.put("/plugins/**", "anon");
pathMap.put("/webjars/**", "anon");
pathMap.put("/account/login", "anon");
pathMap.put("/swagger-ui.html", "anon");
pathMap.put("/swagger-resources/**", "anon");
pathMap.put("/v2/**", "anon");
//
pathMap.put("/appsvr/**", "anon");
pathMap.put("/**", "authc");
shiroFilterFactoryBean.setFilterChainDefinitionMap(pathMap);
return shiroFilterFactoryBean;
}
/***
* Shiro 用於生效註解
* @param securityManager
* @return
*/
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
log.info("----------------載入SourceAdvisor---------------");
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}
}
Realm
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class ShiroRealm extends AuthorizingRealm {
@Autowired
private LoginService loginService;
@Autowired
private BusUserService busUserService;
@Autowired
private BusUserRoleService busUserRoleService;
@Autowired
private BaseRoleService baseRoleService;
@Value("${shiroRealm.BIAuthentic}")
private Boolean authentic;
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String userName = (String) super.getAvailablePrincipal(principalCollection);
log.info("登入驗證,使用者資訊----{}", userName);
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.addStringPermission("authc");
Subject subject=SecurityUtils.getSubject();
List<String> roleList = (List<String>) subject.getSession().getAttribute("roleCodeList");
simpleAuthorizationInfo.addRoles(roleList);
return simpleAuthorizationInfo;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
log.info("------------------Shiro身份認證-----------------");
BILoginToken token = (BILoginToken) authenticationToken;
if (null == token) {
throw new AuthenticationException();
}
String userName = token.getUsername();
String password = String.valueOf(token.getPassword());
String validCode = token.getValidCode();
String requestId = token.getRequestId();
log.info("token中的userName:" + userName + " validCode:" + validCode + " requestId:" + requestId);
//查詢使用者角色關係表
BusUserRole busUserRole = new BusUserRole();
busUserRole.setUserId(busUser.getId());
EntityWrapper<BusUserRole> busUserRoleEntityWrapper = new EntityWrapper<>(busUserRole);
List<BusUserRole> busUserRoleList = busUserRoleService.selectList(busUserRoleEntityWrapper);
List<String>roleCodeList=new ArrayList<>();
List<String> roleIdList = new ArrayList<>();
if (0 < busUserRoleList.size()) {
for (BusUserRole temp : busUserRoleList) {
roleIdList.add(temp.getRoleId());
}
}
log.info("使用者的角色Id為:" + JsonUtil.objectToJson(roleIdList));
//查詢角色列表
List<BaseRole> baseRoleList = baseRoleService.getRoleByRoleIds(roleIdList);
log.info("查詢到的角色列表為:" + JsonUtil.objectToJson(baseRoleList));
List<Integer> roleList = new ArrayList<>();
for (BaseRole temp : baseRoleList) {
String roleCode = temp.getId();
if (RoleEnum.ROLE_CODE_OPERATION.getDesc().equals(roleCode)) {
roleList.add(1);
}
if (RoleEnum.ROLE_CODE_SALE.getDesc().equals(roleCode)) {
roleList.add(2);
}
if (RoleEnum.ROLE_CODE_ADMIN.getDesc().equals(roleCode)) {
roleList.add(0);
}
roleCodeList.add(temp.getRoleCode());
}
//token返回賦值
token.setBaseRoleList(roleList);
token.setUsername(busUser.getUsername());
token.setUm(busUser.getUm());
token.setId(busUser.getId());
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userName, password, ByteSource.Util.bytes(userName), getName());
Subject subject = SecurityUtils.getSubject();
subject.getSession().setAttribute("userInfo", busUser);
subject.getSession().setAttribute("roleList", baseRoleList);
subject.getSession().setAttribute("roleCodeList",roleCodeList);
return simpleAuthenticationInfo;
}
}
相關文章
- Spingboot 讀取 yml 配置檔案裡的引數值boot
- springboot Shiro 配置類Spring Boot
- springboot 整合 Shiro 配置類Spring Boot
- Shiro許可權管理框架(一):Shiro的基本使用框架
- 關於shiro安全框架和shiro的認證流程框架
- shiro多realm配置免密碼登陸密碼
- SpingBoot:整合Elasticsearch7.2.0bootElasticsearch
- 最簡明的Shiro教程
- Shiro Filter的設計概念Filter
- Shiro的原理及Web搭建Web
- 走進shiro,構建安全的應用程式---shiro修仙序章
- [翻譯-Shiro]-整合Apache Shiro到基於Spring的應用ApacheSpring
- SpingBoot @Scheduled定時任務boot
- springboot+shiro+jwt+vue配置全攻略Spring BootJWTVue
- Shiro中的Remember me設定REM
- shiro教程(2): shiro介紹
- 開啟IDEA工具的service執行spingboot啟動類Ideaboot
- SpingBoot_學習筆記整合boot筆記
- [翻譯-Shiro]-Apache Shiro 簡介Apache
- [翻譯-Shiro]-Apache Shiro 框架解析Apache框架
- 簡單的整合 shiro + SpringMVC 例子SpringMVC
- Shiro(認證的執行流程Authentication)
- Shiro中principal和credential的區別
- 【Shiro】4.Springboot整合ShiroSpring Boot
- Shiro 教程
- Shiro【授權、整合Spirng、Shiro過濾器】過濾器
- Shiro系列教程之一Shiro簡介
- [翻譯-Shiro]-Apache Shiro Java認證指南ApacheJava
- [翻譯-Shiro]-Apache Shiro Java 授權指南ApacheJava
- [翻譯-Shiro]-Apache Shiro Java註解列表ApacheJava
- Shiro入門這篇就夠了【Shiro的基礎知識、回顧URL攔截】
- 基於shiro的自定義註解的擴充套件套件
- web開發安全框架中的Apache Shiro的應用Web框架Apache
- Android Spingboot 實現SSE通訊案例Androidboot
- Linux上java-jar Spingboot專案LinuxJavaJARboot
- Apache Shiro 快速入門教程,shiro 基礎教程Apache
- [翻譯-Shiro]-10分鐘教會你Apache ShiroApache
- shiro 整合MybatisMyBatis