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

做分析图超牛的地图网站宁波优化推广找哪家

做分析图超牛的地图网站,宁波优化推广找哪家,江西哪里有做电商网站的公司,怎样会展网站建设目录 前言1. 基本知识2. Demo3. 实战 前言 java框架 零基础从入门到精通的学习路线 附开源项目面经等(超全)【Java项目】实战CRUD的功能整理(持续更新) 1. 基本知识 JwtAccessTokenConverter 是 Spring Security OAuth2 中的一…

目录

  • 前言
  • 1. 基本知识
  • 2. Demo
  • 3. 实战

前言

  1. java框架 零基础从入门到精通的学习路线 附开源项目面经等(超全)
  2. 【Java项目】实战CRUD的功能整理(持续更新)

1. 基本知识

JwtAccessTokenConverter 是 Spring Security OAuth2 中的一个重要组件,主要用于生成和验证 JSON Web Token (JWT)

  1. JWT 的结构
    JWT 由三部分组成:头部(Header)、负载(Payload)和签名(Signature)
    头部通常包含令牌的类型(JWT)和签名算法(如 HMAC SHA256)
    负载部分包含声明(Claims),即与用户相关的信息,例如权限、过期时间等
    签名是使用头部和负载生成的,用于验证令牌的完整性

  2. 类继承关系
    JwtAccessTokenConverter 继承自 AbstractTokenConverter,并实现了 AccessTokenConverter 接口
    该类负责将 OAuth2 访问令牌与 JWT 进行转换和处理

  3. 主要方法
    encode:用于生成 JWT,接受 OAuth2AccessToken 和 OAuth2Authentication 参数
    decode:用于解析和验证 JWT,返回 OAuth2AccessToken 实例

常用的几个类如下:

  • Access Token
    OAuth2 中的访问令牌,用于授权客户端访问受保护资源
    JWT 是一种常用的访问令牌格式

  • OAuth2Authentication
    封装与用户相关的身份验证信息,包括用户的身份和权限

  • Additional Information
    JWT 中可以包含附加信息,通常用于存储用户 ID、用户名、权限等自定义字段

2. Demo

  1. 签名密钥:在 main 方法中使用 demo.setSigningKey(signingKey); 设置签名密钥,以确保生成的 JWT 是安全的

  2. 用户权限和身份验证:使用 SimpleGrantedAuthority 创建用户的角色,并通过 TestingAuthenticationToken 实现身份验证

  3. OAuth2AccessToken:创建访问令牌,附加用户 ID、用户名和权限信息

  4. JWT 生成与解码:调用 encode 方法生成 JWT,然后使用 Base64 解码打印出 JWT 的头部和负载部分

常用的xml配置文件如下:

<!-- https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2 -->
<dependency><groupId>org.springframework.security.oauth</groupId><artifactId>spring-security-oauth2</artifactId><version>2.5.0.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-jwt</artifactId><version>1.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);// 创建用户权限Set<SimpleGrantedAuthority> 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");Map<String, 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*/@Bean@ConditionalOnMissingBean(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 参数增强Map<String, 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.hkea.cn/news/925472/

相关文章:

  • 古典网站建设欣赏马鞍山网站seo
  • 商城网站建设报价方案免费建网站软件下载
  • 中国做美国酒店的网站好竞价托管收费标准
  • 网站开发与设计静态网页源代码站长之家app下载
  • 松原做网站app运营推广是干什么
  • 做简单的网站链接2024新闻热点摘抄
  • 百度网站站长环球网疫情最新
  • 颍上做网站西安seo网站关键词优化
  • 有没有兼职做设计的网站吗知名网络软文推广平台
  • 数据百度做网站好用吗米拓建站
  • 网站维护运营怎么做搜索引擎优化通常要注意的问题有
  • 圆梦科技专业网站建设恶意点击软件有哪些
  • 如何做vip电影解析网站竞价恶意点击器
  • 开发简单小程序公司深圳网站优化哪家好
  • 网站开发劣势搜索引擎排名优化
  • 桂林网站优化公司企业网络营销顾问
  • 上海外贸出口代理公司排名搜索引擎优化的主要工作有
  • 一般做企业网站需要什么资料广告咨询
  • 广州网站建设兼职网站为什么要做seo
  • 中企动力官网 网站怎么在平台上做推广
  • 教育培训网站建设方案广告宣传费用一般多少
  • 计算机网站设计论文营销排名seo
  • 源码资源国内专业seo公司
  • 丽水微信网站建设报价免费精准客源
  • 广东建设工程中标公示网站google搜索引擎优化
  • 南宁老牌网站建设公司正版google下载
  • 网站做信用认证有必要吗微信朋友圈推广平台
  • 电子政务网站建设要求百度关键词规划师
  • 博客网站开发毕设免费大数据分析网站
  • 深圳教育平台网站建设好消息疫情要结束了