网站性能优化,WordPress建站去掉后缀,wordpress做成论坛系统,浙江建设Spring Authorization Server 替换 Shiro 指引
背景
Spring 团队正式宣布 Spring Security OAuth 停止维护#xff0c;该项目将不会再进行任何的迭代 目前 Spring 生态中的 OAuth2 授权服务器是 Spring Authorization Server 已经可以正式生产使用作为 SpringBoot 3.0 的最新…Spring Authorization Server 替换 Shiro 指引
背景
Spring 团队正式宣布 Spring Security OAuth 停止维护该项目将不会再进行任何的迭代 目前 Spring 生态中的 OAuth2 授权服务器是 Spring Authorization Server 已经可以正式生产使用作为 SpringBoot 3.0 的最新权限方案JeecgBoot springboot3_sas分支已经完成了采用Spring Authorization Server 替换 Shiro工作。
JeecgBoot SAS分支 Date: 2024-01-17技术栈 SpringBoot3 Spring Authorization Serverjdk18 源码下载
后端https://github.com/jeecgboot/jeecg-boot/tree/springboot3_sas前端https://github.com/jeecgboot/jeecgboot-vue3/tree/springboot3_sas
登录对接 jeecg 基于Spring Authorization Server扩展了四种登录实现加上默认提供的四种共计有8种登录方式额外还有OpenID Connect模式。本文不讲解授权码模式、客户端模式、刷新码模式、设备码模式、OpenID Connect模式只会讲解jeecg实际应用了的四种扩展模式其它模式请查阅Spring Authorization Server官方原文。 https://docs.spring.io/spring-authorization-server/reference/overview.html 注意OpenID Connect应当仅为认证阶段使用不可作为权限校验阶段使用。 密码模式和APP模式 密码模式在Oauth2.1协议中被放弃Spring Authorization Server并没有对该模式提供实现该实现是基于Spring Authorization Server提供的扩展入口实现的。 密码模式实现源码package org.jeecg.config.security.password; APP模式实现源码package org.jeecg.config.security.app; 密码模式与APP模式实现完全一致不过防止额外需求偏差所以进行了分开实现。 请求地址{baseUrl} /oauth2/token
请求方法POST
请求头
请求头名称请求头值AuthorizationBasic base64(clientId:clientSecret)(此处需要自行替换)Content-Typeapplication/x-www-form-urlencoded
请求参数
参数名称参数值grant_typepassword/app (password为PC端使用app为移动端使用)username用户名password密码
响应内容
参数名称参数含义access_token访问token在被限制访问的接口请求中添加Authorization: Bearer access_tokenrefersh_token刷新token用于刷新码模式获取新的access_tokenuserInfo当前登录用户信息…其它内容不作详解请查看源码
phone模式 phone模式用于手机验证码登录场景。 phone模式实现源码package org.jeecg.config.security.phone; 请求地址{baseUrl} /oauth2/token
请求方法POST
请求头
请求头名称请求头值AuthorizationBasic base64(clientId:clientSecret)(此处需要自行替换)Content-Typeapplication/x-www-form-urlencoded
请求参数
参数名称参数值grant_type固定为phonemobile手机号captcha验证码
响应内容
参数名称参数含义access_token访问token在被限制访问的接口请求中添加Authorization: Bearer access_tokenrefersh_token刷新token用于刷新码模式获取新的access_tokenuserInfo当前登录用户信息…其它内容不作详解请查看源码
social模式 任何一个用户中心端比如微信、微博、github、gitee对外提供的对接方式都是授权码模式、OpenID Connect模式最终获取到一段用户信息比如用户名、头像地址、邮箱但是其实并没有办法拿着这段信息在当前系统中访问受限资源以前都是手搓token或者其它手段来得到受限访问的权限这种方法不可靠也不安全而且也不易维护。 jeecg针对以上场景基于Spring Authorization Server扩展了social模式用于处理获取三方用户信息后再获取当前系统的访问凭证。 social模式实现源码package org.jeecg.config.security.social; 提示文档中只讲解social模式的应用不讲解从三方登录到应用social模式的全流程jeecg前后端均已实现细节请查看源码。 请求地址{baseUrl} /oauth2/token
请求方法POST
请求头
请求头名称请求头值AuthorizationBasic base64(clientId:clientSecret)(此处需要自行替换)Content-Typeapplication/x-www-form-urlencoded
请求参数
参数名称参数值grant_type固定为socialtoken可获取用户信息的凭证thirdType三方来源
响应内容
参数名称参数含义access_token访问token在被限制访问的接口请求中添加Authorization: Bearer access_tokenrefersh_token刷新token用于刷新码模式获取新的access_tokenuserInfo当前登录用户信息…其它内容不作详解请查看源码
权限校验
可用于方法或类上将基于注解的权限code针对性处理方法或当前类的所有接口进行权限拦截。
基于角色
// shiro用法
RequiresRoles(admin)// 可替换为 spring authorization server 用法
PreAuthorize(jps.requiresRoles(admin))基于权限
// shiro用法
RequiresPermissions(sys:role) // 可替换为 spring authorization server 用法
PreAuthorize(jps.requiresPermissions(sys:role))角色和权限组合使用
- PreAuthorize(jps.requiresPermissions(system:quartzJob:add) or jps.requiresRoles(admin))免登录配置
jeecg:shiro:excludeUrls: /test/jeecgDemo/demo3,/test/jeecgDemo/redisDemo/**,/jmreport/bigscreen2/**# 替换为
security:oauth2:client:ignore-urls:- /test/jeecgDemo/demo3- /test/jeecgDemo/redisDemo/**- /jmreport/bigscreen2/**升级小技巧
搜索替换为org.apache.shiro.SecurityUtilsorg.jeecg.config.security.utils.SecureUtil(LoginUser) SecurityUtils.getSubject().getPrincipal()SecureUtil.currentUser()org.apache.shiro.authz.annotation.RequiresRolesorg.springframework.security.access.prepost.PreAuthorizeorg.apache.shiro.authz.annotation.RequiresPermissionsorg.springframework.security.access.prepost.PreAuthorizeRequiresPermissionsPreAuthorize(“jps.requiresPermissions(‘xxx’)”)RequiresRolesPreAuthorize(“jps.requiresRoles(‘xxx’)”)
升级SQL
切换springboot3_sas分支的Spring Authorization Server需要执行升级sql
CREATE TABLE oauth2_registered_client (id varchar(100) NOT NULL,client_id varchar(100) NOT NULL,client_id_issued_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,client_secret varchar(200) DEFAULT NULL,client_secret_expires_at timestamp NULL DEFAULT NULL,client_name varchar(200) NOT NULL,client_authentication_methods varchar(1000) NOT NULL,authorization_grant_types varchar(1000) NOT NULL,redirect_uris varchar(1000) DEFAULT NULL,post_logout_redirect_uris varchar(1000) DEFAULT NULL,scopes varchar(1000) NOT NULL,client_settings varchar(2000) NOT NULL,token_settings varchar(2000) NOT NULL,PRIMARY KEY (id)
) ENGINEInnoDB DEFAULT CHARSETutf8mb4 COLLATEutf8mb4_general_ci;INSERT INTO oauth2_registered_client
(id,
client_id,
client_id_issued_at,
client_secret,
client_secret_expires_at,
client_name,
client_authentication_methods,
authorization_grant_types,
redirect_uris,
post_logout_redirect_uris,
scopes,
client_settings,
token_settings)
VALUES
(3eacac0e-0de9-4727-9a64-6bdd4be2ee1f,
jeecg-client,
now(),
secret,
null,
3eacac0e-0de9-4727-9a64-6bdd4be2ee1f,
client_secret_basic,
refresh_token,authorization_code,password,app,phone,social,
http://127.0.0.1:8080/jeecg-,
http://127.0.0.1:8080/,
*,
{class:java.util.Collections$UnmodifiableMap,settings.client.require-proof-key:false,settings.client.require-authorization-consent:true},
{class:java.util.Collections$UnmodifiableMap,settings.token.reuse-refresh-tokens:true,settings.token.id-token-signature-algorithm:[org.springframework.security.oauth2.jose.jws.SignatureAlgorithm,RS256],settings.token.access-token-time-to-live:[java.time.Duration,300000.000000000],settings.token.access-token-format:{class:org.springframework.security.oauth2.server.authorization.settings.OAuth2TokenFormat,value:self-contained},settings.token.refresh-token-time-to-live:[java.time.Duration,3600.000000000],settings.token.authorization-code-time-to-live:[java.time.Duration,300000.000000000],settings.token.device-code-time-to-live:[java.time.Duration,300000.000000000]});常用API
1. 获取登录用户信息
LoginUser sysUser SecureUtil.currentUser();