Shiro 使用 JWT Token 配置类参考
项目中使用了 Shiro 进行验证和授权,下面是 Shiro 配置类给予参考。
后来并没有使用 Shiro,感觉使用 JWT 还是自己写拦截器比较灵活,使用 Shiro 后各种地方需要魔改,虽然功能也能实现,但感觉把简单问题复杂化了,如果单单只使用 Shiro 授权这一块可以尝试。
package com.nwgdk.ums.config.shiro;
import com.nwgdk.ums.config.shiro.filter.AccessTokenFilter;
import com.nwgdk.ums.config.shiro.listener.CustomSessionListener;
import com.nwgdk.ums.config.shiro.realm.AdminRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.SessionListener;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.mgt.DefaultWebSessionStorageEvaluator;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import javax.servlet.Filter;
import java.util.*;
/**
* @author nwgdk
*/
@Configuration
@AutoConfigureAfter(ShiroLifecycleBeanPostProcessorConfiguartion.class)
public class ShiroConfiguration {
/**
* Hash迭代次数
*/
@Value("${ums.config.hash.hash-iterations}")
private Integer hashIterations;
/**
* WEB 过滤器链
*/
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean shiroFilterFactoryBean() {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
// 设置安全管理器
shiroFilterFactoryBean.setSecurityManager(securityManager());
// 注册自定义过滤器
Map<String, Filter> filterMap = new LinkedHashMap<>(8);
filterMap.put("authc", new AccessTokenFilter());
shiroFilterFactoryBean.setFilters(filterMap);
// 定义过滤链
Map<String, String> filterChains = new LinkedHashMap<>(8);
filterChains.put("/v1/admin/login", "anon");
filterChains.put("/**", "authc");
// 设置过滤器链
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChains);
return shiroFilterFactoryBean;
}
/**
* 安全管理器
*/
@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
// 设置 Session 管理器
securityManager.setSessionManager(sessionManager());
// 设置 Realm
securityManager.setRealm(adminRealm());
// 关闭 RememberMe
securityManager.setRememberMeManager(null);
// 设置自定义 Subject
securityManager.setSubjectFactory(statelessDefaultSubjectFactory());
// 设置 SubjectDao
securityManager.setSubjectDAO(defaultSubjectDAO());
return securityManager;
}
/**
* 自定义 Subject 工厂, 禁止使用 Session
*/
@Bean("subjectFactory")
public StatelessDefaultSubjectFactory statelessDefaultSubjectFactory() {
return new StatelessDefaultSubjectFactory();
}
@Bean
public DefaultSubjectDAO defaultSubjectDAO() {
DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
// 设置会话存储调度器
subjectDAO.setSessionStorageEvaluator(defaultWebSessionStorageEvaluator());
return subjectDAO;
}
/**
* 会话存储器
*/
@Bean
public DefaultWebSessionStorageEvaluator defaultWebSessionStorageEvaluator() {
DefaultWebSessionStorageEvaluator evaluator = new DefaultWebSessionStorageEvaluator();
// 禁用会话存储
evaluator.setSessionStorageEnabled(false);
return evaluator;
}
/**
* Session 管理器
*/
@Bean
public DefaultWebSessionManager sessionManager() {
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
// 设置 Cookie
sessionManager.setSessionIdCookie(simpleCookie());
// 启用 Session Id Cookie,默认启用
sessionManager.setSessionIdCookieEnabled(false);
// 设置全局超时时间,默认30分钟
sessionManager.setGlobalSessionTimeout(1800000L);
// 设置会话监听器
sessionManager.setSessionListeners(customSessionListener());
// 禁用 Session 验证调度器
sessionManager.setSessionValidationSchedulerEnabled(false);
return sessionManager;
}
/**
* 会话监听器
*/
@Bean
public Collection<SessionListener> customSessionListener() {
List<SessionListener> listeners = new ArrayList<>();
listeners.add(new CustomSessionListener());
return listeners;
}
/**
* Session Cookie
*/
@Bean
public SimpleCookie simpleCookie() {
SimpleCookie cookie = new SimpleCookie();
// Session Cookie 名称
cookie.setName("SID");
// Session 存活时间
cookie.setMaxAge(10);
// 设置 Cookie 只读
cookie.setHttpOnly(true);
return cookie;
}
/**
* 凭证匹配器
*/
@Bean("credentialsMatcher")
public HashedCredentialsMatcher hashedCredentialsMatcher() {
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
// 散列算法
hashedCredentialsMatcher.setHashAlgorithmName("md5");
// 散列次数
hashedCredentialsMatcher.setHashIterations(hashIterations);
// 使用 HEX 编码
hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
return hashedCredentialsMatcher;
}
/**
* 领域对象
*/
@Bean("adminRealm")
public AdminRealm adminRealm() {
AdminRealm adminRealm = new AdminRealm();
// 设置密码匹配器
adminRealm.setCredentialsMatcher(hashedCredentialsMatcher());
return adminRealm;
}
/**
* 开启注解 (如 @RequiresRoles, @RequiresPermissions),
* 需借助 SpringAOP 扫描使用 Shiro 注解的类,并在必要时进行安全逻辑验证
* 配置以下两个 Bean:
* DefaultAdvisorAutoProxyCreator(可选) 和 AuthorizationAttributeSourceAdvisor 即可实现此功能
*/
@Bean
@DependsOn({"lifecycleBeanPostProcessor"})
public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
advisorAutoProxyCreator.setProxyTargetClass(true);
return advisorAutoProxyCreator;
}
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager());
return authorizationAttributeSourceAdvisor;
}
}
package com.nwgdk.ums.config.shiro;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author nwgdk
*/
@Configuration
public class ShiroLifecycleBeanPostProcessorConfiguartion {
/**
* Shiro 生命周期处理器
*/
@Bean(name = "lifecycleBeanPostProcessor")
public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
}
package com.nwgdk.ums.config.shiro;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.SubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSubjectFactory;
/**
* 自定义 Subject
*
* @author nwgdk
*/
public class StatelessDefaultSubjectFactory extends DefaultWebSubjectFactory {
@Override
public Subject createSubject(SubjectContext context) {
// 禁止 Subject 创建会话
context.setSessionCreationEnabled(false);
return super.createSubject(context);
}
}
Shiro 使用 JWT Token 配置类参考的更多相关文章
- jwt token and shiro
openapi可以完全开放访问,也可以使用jwt token进行简单的认证,还可以使用shiro支持更细致的权限管理. handler.yml配置了security和shiro两个handler: s ...
- Spring Cloud OAuth2.0 微服务中配置 Jwt Token 签名/验证
关于 Jwt Token 的签名与安全性前面已经做了几篇介绍,在 IdentityServer4 中定义了 Jwt Token 与 Reference Token 两种验证方式(https://www ...
- SpringBoot整合Shiro 二:Shiro配置类
环境搭建见上篇:SpringBoot整合Shiro 一:搭建环境 Shiro配置类配置 shiro的配置主要集中在 ShiroFilterFactoryBean 中 关于权限: anon:无需认证就可 ...
- 学习Spring Boot:(十六)使用Shiro与JWT 实现认证服务
前言 需要把Web应用做成无状态的,即服务器端无状态,就是说服务器端不会存储像会话这种东西,而是每次请求时access_token进行资源访问.这里我们将使用 JWT 1,基于散列的消息认证码,使用一 ...
- ASP.NET Core 2.1 JWT token (一) - 简书
原文:ASP.NET Core 2.1 JWT token (一) - 简书 JwtBearer认证是一种标准的,通用的,无状态的,与语言无关的认证方式.Bearer验证属于HTTP协议标准验证. 如 ...
- 【SpringBoot技术专题】「权限校验专区」Shiro整合JWT授权和认证实现
本章介绍一下常用的认证框架Shiro结合springboot以及集合jwt快速带您开发完成一个认证框架机制. Maven配置依赖 <dependency> <groupId>o ...
- asp.net core使用identity+jwt保护你的webapi(二)——获取jwt token
前言 上一篇已经介绍了identity在web api中的基本配置,本篇来完成用户的注册,登录,获取jwt token. 开始 开始之前先配置一下jwt相关服务. 配置JWT 首先NuGet安装包: ...
- ASP.NET Core 实战:基于 Jwt Token 的权限控制全揭露
一.前言 在涉及到后端项目的开发中,如何实现对于用户权限的管控是需要我们首先考虑的,在实际开发过程中,我们可能会运用一些已经成熟的解决方案帮助我们实现这一功能,而在 Grapefruit.VuCore ...
- 【ASP.NET Core快速入门】(十一)应用Jwtbearer Authentication、生成jwt token
准备工作 用VSCode新建webapi项目JwtAuthSample,并打开所在文件夹项目 dotnet new webapi --name JwtAuthSample 编辑JwtAuthSampl ...
随机推荐
- Python【day 14】sorted函数、filter函数和map函数的区别
sorted函数.filter函数和map函数的区别1.作用 前者用于排序, 中者用于筛选, 后者用于返回值(不是特定的筛选或者排序)2.写法 前者 sorted(iterable,key=自定义函数 ...
- linux 环境下部署 Asp.Net Core 项目 访问 oralce 数据库
1.ASP.NET Core 是一个跨平台的高性能开源框架,可以部署到Linux上,那项目部署在Linux上有哪些好处呢? 1.linux硬件需求小,大部分版本免费,成本低. 2.linux的用户管理 ...
- Eureka源码解析系列文章汇总
先看一张图 0 这个图是Eureka官方提供的架构图,整张图基本上把整个Eureka的核心功能给列出来了,当你要阅读Eureka的源码时可以参考着这个图和下方这些文章 EurekaServer Eur ...
- redis笔记1
存储结构 字符类型 散列类型 列表类型 集合类型 有序集合 可以为每个key设置超时时间: 可以通过列表类型来实现分布式队列的操作 支持发布订阅的消息模式 提供了很多命令与redis进行交互 数据缓存 ...
- SAP MM MIGO过账报错 - 用本币计算的余额 - 之对策
SAP MM MIGO过账报错 - 用本币计算的余额 - 之对策 使用MIGO事务代码对采购订单4500000191,执行收货,系统报错: 详细错误信息如下: 用本币计算的余额 消息号 F5703 诊 ...
- Git内部原理浅析
Git独特之处 Git是一个分布式版本控制系统,首先分布式意味着Git不仅仅在服务端有远程仓库,同时会在本地也保留一个完整的本地仓库(.git/文件夹),这种分布式让Git拥有下面几个特点: 1.直接 ...
- Oracle使用命令行登录提示ERROR: ORA-01017: invalid username/password; logon denied
刚在Windows上面安装好Oracle 10g,刚开始使用PLSQLDevelop软件登录提示 not logged on ,然后使用命令行登录提示 ERROR: ORA-01017: inval ...
- 【分布式搜索引擎】Elasticsearch之如何安装Elasticsearch
在Macos上安装 一.下载安装过程 最新版本下载地址: https://www.elastic.co/cn/downloads/elasticsearch 历史版本下载地址: https://www ...
- Go语言goroutine调度器概述(11)
本文是<go调度器源代码情景分析>系列的第11篇,也是第二章的第1小节. goroutine简介 goroutine是Go语言实现的用户态线程,主要用来解决操作系统线程太“重”的问题,所谓 ...
- 题解:[ZJOI2014]璀灿光华
原题链接 OJ 题号 洛谷 3342 loj 2203 bzoj 3619 题目描述 金先生有一个女朋友没名字.她勤劳勇敢.智慧善良.金先生很喜欢她.为此,金先生用\(a^3\)块\(1 \times ...