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

网站建设外包行业为什么中国禁止谷歌浏览器

网站建设外包行业,为什么中国禁止谷歌浏览器,一个网站开发环境是什么,安徽省建设工程网站jwt简介 以下介绍来自官网(https://jwt.io/) SON Web 令牌 (JWT) 是一种开放标准 (RFC 7519),它定义了一种紧凑且独立的方式,用于在各方之间以 JSON 对象的形式安全地传输信息。此信…

jwt简介

以下介绍来自官网(https://jwt.io/)
SON Web 令牌 (JWT) 是一种开放标准 (RFC 7519),它定义了一种紧凑且独立的方式,用于在各方之间以 JSON 对象的形式安全地传输信息。此信息可以验证和信任,因为它是经过数字签名的。JWT 可以使用密钥(使用 HMAC 算法)或使用 RSA 或 ECDSA 的公钥/私钥对进行签名**(来自官网)**。

尽管 JWT 可以加密以提供各方之间的保密性,但我们将专注于签名令牌。签名令牌可以验证其中包含的声明的完整性,而加密令牌则向其他方隐藏这些声明。当使用公钥/私钥对对令牌进行签名时,签名还证明只有持有私钥的一方才是签名的一方**(来自官网)**。

使用场景

  • 授权:这是使用 JWT 的最常见方案。用户登录后,每个后续请求都将包含 JWT,允许用户访问使用该令牌允许的路由、服务和资源。单点登录是当今广泛使用 JWT 的一项功能,因为它的开销很小,并且能够跨不同域轻松使用**(来自官网)**。

  • 信息交换:JSON Web 令牌是在各方之间安全传输信息的好方法。由于 JWT 可以签名(例如,使用公钥/私钥对),因此您可以确定发件人是他们所说的人。此外,由于签名是使用标头和有效负载计算的,因此您还可以验证内容是否未被篡改**(来自官网)**)。

jwt的组成结构

  • Header
  • Payload
  • Signature
    因此,JWT 通常如下所示。
xxxxx.yyyyy.zzzzz

Header

通常由两部分组成:令牌的类型(即 JWT)和正在使用的签名算法,例如 HMAC SHA256 或 RSA。
例如:

{"alg": "HS256","typ": "JWT"
}

然后,此JSON被Base64Url编码以形成JWT的第一部

Payload(有效载荷)

令牌的第二部分是有效负载,其中包含声明。声明是有关实体(通常是用户)和其他数据的语句。
有三种类型的声明:已注册、公共和私人声明。
已注册声明:这些是一组预定义的声明,这些声明不是必需的,但建议使用,以提供一组有用的、可互操作的声明。其中一些是:iss(发行人),exp(到期时间),sub(主题),aud(受众)等。

请注意,声明名称只有三个字符长,因为 JWT 应该是紧凑的。

公共声明:这些可以由使用 JWT 的人随意定义。但为了避免冲突,它们应该在 IANA JSON Web 令牌注册表中定义,或者定义为包含抗冲突命名空间的 URI。

专用声明:这些是创建的自定义声明,用于在同意使用它们的各方之间共享信息,既不是注册声明也不是公共声明。

示例有效负载可以是:

{"sub": "1234567890","name": "John Doe","admin": true
}

sub是预定义的声明,name,admin则是自己随便添加的一些定义
然后对有效负载进行 Base64Url 编码以形成 JSON Web 令牌的第二部分。

请注意,对于签名令牌,此信息虽然受到篡改保护,但任何人都可以读取。不要将机密信息放在 JWT 的有效负载或标头元素中,除非它已加密。

签名

要创建签名部分,您必须获取编码的标头、编码的有效负载、机密、标头中指定的算法并对其进行签名。

例如,如果要使用 HMAC SHA256 算法,将按以下方式创建签名:

HMACSHA256(base64UrlEncode(header) + "." +base64UrlEncode(payload),secret)

签名用于验证消息在此过程中未被更改,并且在使用私钥签名的令牌的情况下,它还可以验证 JWT 的发件人是否是它所说的人。

把所有东西放在一起
输出是三个由点分隔的 Base64-URL 字符串,可以在 HTML 和 HTTP 环境中轻松传递,同时与基于 XML 的标准(如 SAML)相比更加紧凑。

使用java-jwt来生成和解析token

导入maven依赖

<dependency><groupId>com.auth0</groupId><artifactId>java-jwt</artifactId><version>3.4.0</version></dependency>

这附上一段我之前写的代码

    public String getToken(Users user) {String token="";// 2*24*3600 * 1000token= JWT.create().withExpiresAt(new Date(System.currentTimeMillis() + (2*24*3600*1000))).withAudience(user.getEmployeeId())// 将 login_user 保存到 token 里面.sign(Algorithm.HMAC256(DigestUtils.md5Hex(user.getEmployeePassword()+ Constants.MD5PRIVATEKEY)));// 以 password 作为 token 的密钥return token;}

这是上面引入的依赖提供的方法
首先是 JWT.create(),create方法去调用了JWTCreator的init方法

public static JWTCreator.Builder create() {return JWTCreator.init();}

然后init方法又new了一个自己的内部类,内部类给头部(headerClaims)和载荷(payloadClaims)声明了引用地址

    /*** Initialize a JWTCreator instance.** @return a JWTCreator.Builder instance to configure.*/static JWTCreator.Builder init() {return new Builder();}/*** The Builder class holds the Claims that defines the JWT to be created.*/public static class Builder {private final Map<String, Object> payloadClaims;private Map<String, Object> headerClaims;Builder() {this.payloadClaims = new HashMap<>();this.headerClaims = new HashMap<>();}

紧接着withExpiresAt()和withAudience()实际上就是给载荷里预定义的声明赋值,withExpiresAt()就是给exp赋值,withAudience()就是给aud赋值

     /*** Add a specific Audience ("aud") claim to the Payload.** @param audience the Audience value.* @return this same Builder instance.*/public Builder withAudience(String... audience) {addClaim(PublicClaims.AUDIENCE, audience);return this;}/*** Add a specific Expires At ("exp") claim to the Payload.** @param expiresAt the Expires At value.* @return this same Builder instance.*/public Builder withExpiresAt(Date expiresAt) {addClaim(PublicClaims.EXPIRES_AT, expiresAt);return this;}

它们都调用了addClaim方法,可以看到就是在载荷的那个map中添加key和value

 private void addClaim(String name, Object value) {if (value == null) {payloadClaims.remove(name);return;}payloadClaims.put(name, value);}

前面说过也可以自定义声明添加到载荷这部分,这里也提供了多个方法,这里展示两个

   /*** Add a custom Claim value.** @param name  the Claim's name.* @param value the Claim's value.* @return this same Builder instance.* @throws IllegalArgumentException if the name is null.*/public Builder withClaim(String name, Boolean value) throws IllegalArgumentException {assertNonNull(name);addClaim(name, value);return this;}/*** Add a custom Claim value.** @param name  the Claim's name.* @param value the Claim's value.* @return this same Builder instance.* @throws IllegalArgumentException if the name is null.*/public Builder withClaim(String name, Integer value) throws IllegalArgumentException {assertNonNull(name);addClaim(name, value);return this;}

然后是sign()方法,这里这里传入了一个我们指明秘钥的算法,headerClaims.put方法给头部添加了令牌的类型(即 JWT)和正在使用的签名算法

/*** Creates a new JWT and signs is with the given algorithm** @param algorithm used to sign the JWT* @return a new JWT token* @throws IllegalArgumentException if the provided algorithm is null.* @throws JWTCreationException     if the claims could not be converted to a valid JSON or there was a problem with the signing key.*/public String sign(Algorithm algorithm) throws IllegalArgumentException, JWTCreationException {if (algorithm == null) {throw new IllegalArgumentException("The Algorithm cannot be null.");}headerClaims.put(PublicClaims.ALGORITHM, algorithm.getName());headerClaims.put(PublicClaims.TYPE, "JWT");String signingKeyId = algorithm.getSigningKeyId();if (signingKeyId != null) {withKeyId(signingKeyId);}return new JWTCreator(algorithm, headerClaims, payloadClaims).sign();}

然后new 了一个JWTCreator对象

 private JWTCreator(Algorithm algorithm, Map<String, Object> headerClaims, Map<String, Object> payloadClaims) throws JWTCreationException {this.algorithm = algorithm;try {ObjectMapper mapper = new ObjectMapper();SimpleModule module = new SimpleModule();module.addSerializer(ClaimsHolder.class, new PayloadSerializer());mapper.registerModule(module);mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);headerJson = mapper.writeValueAsString(headerClaims);payloadJson = mapper.writeValueAsString(new ClaimsHolder(payloadClaims));} catch (JsonProcessingException e) {throw new JWTCreationException("Some of the Claims couldn't be converted to a valid JSON format.", e);}}

这里可以看到这两句代码,把头部这个map和载荷这个map都转换成了json串

  headerJson = mapper.writeValueAsString(headerClaims);payloadJson = mapper.writeValueAsString(new ClaimsHolder(payloadClaims));

然后返回,在new一个JWTCreator对象后又调用了这个对象的sign()方法

 private String sign() throws SignatureGenerationException {String header = Base64.encodeBase64URLSafeString(headerJson.getBytes(StandardCharsets.UTF_8));String payload = Base64.encodeBase64URLSafeString(payloadJson.getBytes(StandardCharsets.UTF_8));String content = String.format("%s.%s", header, payload);byte[] signatureBytes = algorithm.sign(content.getBytes(StandardCharsets.UTF_8));String signature = Base64.encodeBase64URLSafeString((signatureBytes));return String.format("%s.%s", content, signature);}

可以看到首先对header也就是头部做了base64编码,然后对载荷 payload也做了base64编码

String header = Base64.encodeBase64URLSafeString(headerJson.getBytes(StandardCharsets.UTF_8));
String payload = Base64.encodeBase64URLSafeString(payloadJson.getBytes(StandardCharsets.UTF_8));

然后使用小数点连接

String content = String.format("%s.%s", header, payload);

然后使用算法对连接后的头部和载荷进行签名,最后形成签名

byte[] signatureBytes = algorithm.sign(content.getBytes(StandardCharsets.UTF_8));

然后用base64对签名进行编码,再把编码后的签名和前面的头部,载荷连接起来

String signature = Base64.encodeBase64URLSafeString((signatureBytes));return String.format("%s.%s", content, signature);

文章转载自:
http://dinncomenhaden.bpmz.cn
http://dinncocoexecutor.bpmz.cn
http://dinncopenthouse.bpmz.cn
http://dinncopocketknife.bpmz.cn
http://dinncobehoove.bpmz.cn
http://dinncoopticist.bpmz.cn
http://dinncopalaeozoology.bpmz.cn
http://dinncoreword.bpmz.cn
http://dinncotrioicous.bpmz.cn
http://dinncochromatogram.bpmz.cn
http://dinncoicescape.bpmz.cn
http://dinncoairily.bpmz.cn
http://dinncobathypelagic.bpmz.cn
http://dinncofrypan.bpmz.cn
http://dinncodishabille.bpmz.cn
http://dinncoweldless.bpmz.cn
http://dinncocalced.bpmz.cn
http://dinncorostra.bpmz.cn
http://dinncocatananche.bpmz.cn
http://dinncomural.bpmz.cn
http://dinncopastorate.bpmz.cn
http://dinncodesize.bpmz.cn
http://dinncoruminative.bpmz.cn
http://dinncohirudin.bpmz.cn
http://dinncoundernutrition.bpmz.cn
http://dinncomovingly.bpmz.cn
http://dinncolysis.bpmz.cn
http://dinncoindulgently.bpmz.cn
http://dinncoravine.bpmz.cn
http://dinncoheitiki.bpmz.cn
http://dinncopalebuck.bpmz.cn
http://dinncoirtron.bpmz.cn
http://dinncopotentiostatic.bpmz.cn
http://dinncoplush.bpmz.cn
http://dinncosollicker.bpmz.cn
http://dinncopiquada.bpmz.cn
http://dinncobujumbura.bpmz.cn
http://dinncoarmory.bpmz.cn
http://dinncodiplomat.bpmz.cn
http://dinncostacte.bpmz.cn
http://dinncogreenyard.bpmz.cn
http://dinncoshill.bpmz.cn
http://dinncocytrel.bpmz.cn
http://dinncopedobaptism.bpmz.cn
http://dinncoyawl.bpmz.cn
http://dinncofrenchman.bpmz.cn
http://dinncocarex.bpmz.cn
http://dinncoaccommodable.bpmz.cn
http://dinncoiatrochemist.bpmz.cn
http://dinncocymometer.bpmz.cn
http://dinncounanalysable.bpmz.cn
http://dinncounep.bpmz.cn
http://dinncoconverge.bpmz.cn
http://dinncodunlin.bpmz.cn
http://dinncopedigree.bpmz.cn
http://dinncosnog.bpmz.cn
http://dinncoravenna.bpmz.cn
http://dinncokiddywink.bpmz.cn
http://dinncoceleriac.bpmz.cn
http://dinncobaronage.bpmz.cn
http://dinncosenega.bpmz.cn
http://dinncocrocked.bpmz.cn
http://dinncoflagged.bpmz.cn
http://dinncodiplomatese.bpmz.cn
http://dinncolies.bpmz.cn
http://dinncobooming.bpmz.cn
http://dinncotraintime.bpmz.cn
http://dinncometalworking.bpmz.cn
http://dinncoperpetuate.bpmz.cn
http://dinncocpe.bpmz.cn
http://dinncoferingi.bpmz.cn
http://dinncosalet.bpmz.cn
http://dinncotaping.bpmz.cn
http://dinncostoke.bpmz.cn
http://dinncodemonstrable.bpmz.cn
http://dinncomillihenry.bpmz.cn
http://dinncosuperpotency.bpmz.cn
http://dinncomagdalenian.bpmz.cn
http://dinncoacerbate.bpmz.cn
http://dinncotyrtaeus.bpmz.cn
http://dinncoupstretched.bpmz.cn
http://dinncolargo.bpmz.cn
http://dinncoshrilly.bpmz.cn
http://dinncoprotease.bpmz.cn
http://dinncoglassless.bpmz.cn
http://dinncohalfpenny.bpmz.cn
http://dinncotappoon.bpmz.cn
http://dinncosanty.bpmz.cn
http://dinncokinesitherapy.bpmz.cn
http://dinncodissertate.bpmz.cn
http://dinncodistension.bpmz.cn
http://dinncocoffinite.bpmz.cn
http://dinncosequestration.bpmz.cn
http://dinncoindoctrinize.bpmz.cn
http://dinncowordplay.bpmz.cn
http://dinncocircumplanetary.bpmz.cn
http://dinncootp.bpmz.cn
http://dinncoayuntamiento.bpmz.cn
http://dinncocatchpenny.bpmz.cn
http://dinncoegalitarian.bpmz.cn
http://www.dinnco.com/news/103546.html

相关文章:

  • 腾讯新冠疫情实时动态更新数据关键词优化分析工具
  • 好看的网站设计网站郑州官网网站推广优化
  • 大连网站开发师网站建站
  • 微信端网站页面设计郴州网络推广外包公司
  • 网站建设就业方向东莞做网站推广
  • 做文字logo的网站百度网盘app下载安装手机版
  • 如何建设旅游网站seo在线短视频发布页运营
  • 网站一般用什么语言做重庆百度关键词推广
  • 专做定制网站建设北京百度seo工作室
  • 什么是网络营销师seo技术外包公司
  • node做网站怎么知道蜘蛛来过经典营销案例分析
  • 阿里云做视频网站可以吗西安霸屏推广
  • 做网站都需要哪些软硬件最新热搜新闻事件
  • 做网站背景图片怎么放百度竞价是什么意思?
  • wordpress换主题报错太原seo建站
  • 查询网站流量排名cpm广告联盟平台
  • 成都网站建设推来客熊掌号百度竞价推广方案
  • 推荐广州微信网站建设最新消息新闻
  • 中山祥云做的网站建网站有哪些步骤
  • 医疗网站建设市场营销最有效的手段
  • 网站正在建设中色俄罗斯搜索引擎推广
  • 网站建设 m.ykn.cc今天株洲最新消息
  • 连江建设局网站搜什么关键词比较刺激
  • wordpress手机适配seo搜索引擎优化公司
  • 小男孩做愛网站青岛百度关键词优化
  • seo外链优化培训网站关键词优化代理
  • 百度怎样收录网站网络广告营销方案策划
  • 乐云seo快速网站建设手机优化软件排名
  • 建设网贷网站搭建网站的步骤
  • 自己给公司做网站难不难百度普通收录