当前位置: 首页 > news >正文

怎样做网站的关键词WordPress问答插件路由

怎样做网站的关键词,WordPress问答插件路由,个人公众号做电影网站,网站ppt怎么做目录 前言1. 基本知识2. Demo3. 实战 前言 java框架 零基础从入门到精通的学习路线 附开源项目面经等#xff08;超全#xff09;【Java项目】实战CRUD的功能整理#xff08;持续更新#xff09; 1. 基本知识 JwtAccessTokenConverter 是 Spring Security OAuth2 中的一… 目录 前言1. 基本知识2. Demo3. 实战 前言 java框架 零基础从入门到精通的学习路线 附开源项目面经等超全【Java项目】实战CRUD的功能整理持续更新 1. 基本知识 JwtAccessTokenConverter 是 Spring Security OAuth2 中的一个重要组件主要用于生成和验证 JSON Web Token (JWT) JWT 的结构 JWT 由三部分组成头部Header、负载Payload和签名Signature 头部通常包含令牌的类型JWT和签名算法如 HMAC SHA256 负载部分包含声明Claims即与用户相关的信息例如权限、过期时间等 签名是使用头部和负载生成的用于验证令牌的完整性 类继承关系 JwtAccessTokenConverter 继承自 AbstractTokenConverter并实现了 AccessTokenConverter 接口 该类负责将 OAuth2 访问令牌与 JWT 进行转换和处理 主要方法 encode用于生成 JWT接受 OAuth2AccessToken 和 OAuth2Authentication 参数 decode用于解析和验证 JWT返回 OAuth2AccessToken 实例 常用的几个类如下 Access Token OAuth2 中的访问令牌用于授权客户端访问受保护资源 JWT 是一种常用的访问令牌格式 OAuth2Authentication 封装与用户相关的身份验证信息包括用户的身份和权限 Additional Information JWT 中可以包含附加信息通常用于存储用户 ID、用户名、权限等自定义字段 2. Demo 签名密钥在 main 方法中使用 demo.setSigningKey(signingKey); 设置签名密钥以确保生成的 JWT 是安全的 用户权限和身份验证使用 SimpleGrantedAuthority 创建用户的角色并通过 TestingAuthenticationToken 实现身份验证 OAuth2AccessToken创建访问令牌附加用户 ID、用户名和权限信息 JWT 生成与解码调用 encode 方法生成 JWT然后使用 Base64 解码打印出 JWT 的头部和负载部分 常用的xml配置文件如下 !-- https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2 -- dependencygroupIdorg.springframework.security.oauth/groupIdartifactIdspring-security-oauth2/artifactIdversion2.5.0.RELEASE/version /dependency dependencygroupIdorg.springframework.security/groupIdartifactIdspring-security-jwt/artifactIdversion1.1.0.RELEASE/version !-- 确保版本与 Spring Security 兼容 -- /dependency执行代码如下 package JwtDemo;import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.common.exceptions.InvalidTokenException; import org.springframework.security.oauth2.provider.OAuth2Request; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;import java.util.*;public class JwtDemo extends JwtAccessTokenConverter {public static void main(String[] args) {JwtDemo demo new JwtDemo();String signingKey your-signing-key; // 请替换为你的签名密钥demo.setSigningKey(signingKey);// 创建用户权限SetSimpleGrantedAuthority authorities new HashSet();authorities.add(new SimpleGrantedAuthority(ROLE_USER));// 创建 OAuth2RequestOAuth2Request request new OAuth2Request(null, client_id,authorities, true, null, null, null, null, null);// 创建 AuthenticationAuthentication authentication new TestingAuthenticationToken(user, password, authorities);// 创建 OAuth2AuthenticationOAuth2Authentication oauth2Authentication new OAuth2Authentication(request, authentication);// 创建 OAuth2AccessToken 实例DefaultOAuth2AccessToken token new DefaultOAuth2AccessToken(your-jwt-token);MapString, Object additionalInformation new HashMap();additionalInformation.put(user_id, 12345); // 添加用户信息additionalInformation.put(user_name, user); // 添加用户名additionalInformation.put(authorities, authorities); // 添加权限token.setAdditionalInformation(additionalInformation);// 使用 JwtDemo 生成 JWTString jwt demo.encode(token, oauth2Authentication);System.out.println(生成的 JWT: jwt);String[] chunks jwt.split(\\.);Base64.Decoder decoder Base64.getUrlDecoder();try {String header new String(decoder.decode(chunks[0]));String payload new String(decoder.decode(chunks[1]));System.out.println(Header: header);System.out.println(Payload: payload);} catch (IllegalArgumentException e) {System.err.println(解码时出错: e.getMessage());}} }// 自定义的 Authentication 实现 class TestingAuthenticationToken extends org.springframework.security.authentication.UsernamePasswordAuthenticationToken {public TestingAuthenticationToken(Object principal, Object credentials, Collection? extends GrantedAuthority authorities) {super(principal, credentials, authorities);} }截图如下 3. 实战 已Bledex源码实战为例 Configuration // 标记为配置类Spring会自动扫描并加载 ConditionalOnProperty(prefix blade.security.oauth2, name storeType, havingValue jwt, matchIfMissing true) // 当配置中 blade.security.oauth2.storeType 为 jwt 时才加载此配置类 public class JwtTokenStoreConfiguration {/*** 使用 jwtTokenStore 存储 token*/Bean // 将方法的返回值注册为 Spring 的 Beanpublic TokenStore jwtTokenStore(JwtProperties jwtProperties) {// 创建 JwtTokenStore 实例并传入 JwtAccessTokenConverterreturn new JwtTokenStore(jwtAccessTokenConverter(jwtProperties));}/*** 用于生成 jwt*/Bean // 将方法的返回值注册为 Spring 的 Beanpublic JwtAccessTokenConverter jwtAccessTokenConverter(JwtProperties jwtProperties) {JwtAccessTokenConverter accessTokenConverter new JwtAccessTokenConverter();// 设置 JWT 签名密钥accessTokenConverter.setSigningKey(jwtProperties.getSignKey());return accessTokenConverter; // 返回配置好的 JwtAccessTokenConverter}/*** 用于扩展 jwt*/BeanConditionalOnMissingBean(name jwtTokenEnhancer) // 当 Spring 容器中没有名为 jwtTokenEnhancer 的 Bean 时才加载此 Beanpublic TokenEnhancer jwtTokenEnhancer(JwtAccessTokenConverter jwtAccessTokenConverter, JwtProperties jwtProperties) {// 创建并返回自定义的 TokenEnhancerreturn new BladeJwtTokenEnhancer(jwtAccessTokenConverter, jwtProperties);}}对应的自定义类如下 AllArgsConstructor // 自动生成一个包含所有字段的构造函数 public class BladeJwtTokenEnhancer implements TokenEnhancer {private final JwtAccessTokenConverter jwtAccessTokenConverter; // JWT 访问令牌转换器private final JwtProperties jwtProperties; // JWT 配置属性Overridepublic OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {// 从身份验证对象中获取用户的详细信息BladeUserDetails principal (BladeUserDetails) authentication.getUserAuthentication().getPrincipal();// token 参数增强MapString, Object info new HashMap(16); // 创建一个新的 HashMap 用于存储附加信息info.put(TokenUtil.CLIENT_ID, TokenUtil.getClientIdFromHeader()); // 获取客户端 IDinfo.put(TokenUtil.USER_ID, Func.toStr(principal.getUserId())); // 获取用户 IDinfo.put(TokenUtil.DEPT_ID, Func.toStr(principal.getDeptId())); // 获取部门 IDinfo.put(TokenUtil.POST_ID, Func.toStr(principal.getPostId())); // 获取职位 IDinfo.put(TokenUtil.ROLE_ID, Func.toStr(principal.getRoleId())); // 获取角色 IDinfo.put(TokenUtil.TENANT_ID, principal.getTenantId()); // 获取租户 IDinfo.put(TokenUtil.OAUTH_ID, principal.getOauthId()); // 获取 OAuth IDinfo.put(TokenUtil.ACCOUNT, principal.getAccount()); // 获取账户信息info.put(TokenUtil.USER_NAME, principal.getUsername()); // 获取用户名info.put(TokenUtil.NICK_NAME, principal.getName()); // 获取昵称info.put(TokenUtil.REAL_NAME, principal.getRealName()); // 获取真实姓名info.put(TokenUtil.ROLE_NAME, principal.getRoleName()); // 获取角色名称info.put(TokenUtil.AVATAR, principal.getAvatar()); // 获取用户头像info.put(TokenUtil.LICENSE, TokenUtil.LICENSE_NAME); // 获取许可证名称((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(info); // 将附加信息设置到访问令牌中// token 状态设置if (jwtProperties.getState()) {// 如果 JWT 状态为 true则增强访问令牌OAuth2AccessToken oAuth2AccessToken jwtAccessTokenConverter.enhance(accessToken, authentication);String tokenValue oAuth2AccessToken.getValue(); // 获取增强后的 token 值String tenantId principal.getTenantId(); // 获取租户 IDString userId Func.toStr(principal.getUserId()); // 获取用户 ID// 将访问令牌存储到 JwtUtil 中以便后续管理JwtUtil.addAccessToken(tenantId, userId, tokenValue, accessToken.getExpiresIn());}return accessToken; // 返回增强后的访问令牌} }
http://www.w-s-a.com/news/534057/

相关文章:

  • 外包小程序两个相同的网站对做优化有帮助
  • 网站备案主体修改wordpress 导航图片
  • 怎么建设网站数据库用vs代码做网站
  • 运营企业网站怎么赚钱动漫制作专业概念
  • 宜春网站建设推广网络推广工作好干吗
  • 网站程序0day平顶山市做网站
  • 企业网站名称怎么写哔哩哔哩网页版官网在线观看
  • 直播网站建设书籍阿里巴巴网站建设销售
  • 肇庆企业自助建站系统郴州网站建设解决方案
  • 长沙专业做网站排名游戏开发大亨内购破解版
  • 网站推广适合女生做吗网站如何开启gzip压缩
  • 做外单阿里的网站建站平台那个好
  • 全国性质的网站开发公司关于网站开发的请示
  • 齐齐哈尔住房和城乡建设局网站生物科技公司网站模板
  • 中国建设协会官方网站前端培训的机构
  • 网站建设套餐是什么北京孤儿院做义工网站
  • 网站如何做微信支付链接做暧小视频xo免费网站
  • SEO案例网站建设重庆建站模板平台
  • 上海seo网站推广公司wordpress 小米商城主题
  • 搭建服务器做网站什么网站可以请人做软件
  • 上海建筑建材业网站迁移公家网站模板
  • 仿制别人的网站违法吗网站防火墙怎么做
  • 杨浦网站建设 网站外包公司如何进行网络推广
  • wordpress+仿站步骤超详细wordpress常用函数
  • 浙江手机版建站系统哪个好怎样黑进别人的网站
  • 企业网站搜索引擎推广方法装修网络公司
  • 网站运营优化建议wordpress 添加媒体
  • 用asp.net做网站计数器施工企业会计的内涵
  • 网站被黑咋样的网站建设 设计业务范围
  • 网站开发学哪种语言网站编辑器失效