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

有的网站网速慢哪个网站建设平台支持花呗分期

有的网站网速慢,哪个网站建设平台支持花呗分期,乐清做网站建设公司哪家好,个人优秀网站如何去开发一个springboot starter 我们在平时用 Java 开发的时候#xff0c;在 pom.xml 文件中引入一个依赖就可以很方便的使用了#xff0c;但是你们知道这是如何实现的吗。 现在我们就来解决这一个问题#xff01; 创建 SpringBoot 项目 首先我们要做的就是把你想要给别…如何去开发一个springboot starter 我们在平时用 Java 开发的时候在 pom.xml 文件中引入一个依赖就可以很方便的使用了但是你们知道这是如何实现的吗。 现在我们就来解决这一个问题 创建 SpringBoot 项目 首先我们要做的就是把你想要给别人方便使用的内容自己的写好 我这里直接另外创建了一个 springboot 的 web 项目在这个 web 项目中我用 controller 写了几个简单的接口用于后面的调用然后再创建一个 springboot 项目这个新的 springboot 项目就是用来开发 starter 的方便使用者更好、更方便使用。 现在是具体流程 springboot 的 web 项目创建 用 IDEA 快速创建一个 springboot 项目创建方法如下 选择 spring Initializer自己写一个项目的名称语言选择 Java包管理工具选择 Maven组这个可以自己写 例如我的昵称 xwhking 就可以写 com.xwkhingjdk 选择1.8Java 选择8然后就是下一步 进入下一步后 选择 springboot 的版本我一般选择2.7左右的 然后选择开发工具 Spring Boot DevtoolsSpring COnfiguration Processor 主要用于后面我们在工程中使用的使用在 yml 中写配置时能够自动提示配置Lombok 通过使用 annotation 快速的生成主要的 getter 和 setterSpring Web 加上也没事 然后就是创建了。 创建好了以后就进入写代码环节写一个简单的 web 请求的 Demo 我这里的目录结构如下 UserController的代码如下 package com.xwhking.interface_test.controller;import com.xwhking.interface_test.entity.User; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest; import java.util.Date;import static com.xwhking.interface_test.utils.GenSign.genSign;RestController RequestMapping(/user) public class UserController {GetMapping(/name)public String getName(RequestParam String name , HttpServletRequest request){System.out.println(请求参数名字为 name);return GET 请求参数名字为 name;}GetMapping(/getOne)public User getUser(HttpServletRequest request){User user new User();user.setId(123l);user.setUsername(xwhking);user.setPassword(admin123);System.out.println(user);return user ;} }User代码 package com.xwhking.interface_test.entity;import lombok.Data;Data public class User {private Long id;private String username;private String password;Overridepublic String toString(){return User { id: id , name: username ,password: password };} }这些完成以后就可以启动这一个项目了。 可以用浏览器试试 starter开发 以同样的方式创建一个springboot项目需要特别注意的就是我们需要把 pom.xml 文件中的 build 部分代码全部去掉。 pom.xml 文件 注意这里引入了 Hutool 工具库用于后面开发并且文件里面是没有 build 模块的这里还需要注意把版本的snapshot去掉 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.10/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.xwhking/groupIdartifactIdInterfaceStarter/artifactIdversion0.0.1/versionnameInterfaceStarter/namedescriptionInterfaceStarter/descriptionpropertiesjava.version8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.16/version/dependency/dependencies /project 目录结构 client 目录 这里就是真正使用的类对对应的功能进行了封装。config 目录 这里对 client 进行配置并且把 client 包装成一个 Bean 返回utils 工具类这里用来生成签名的工具META-INF.spring.factories 这个非常重要用于别人调用的时候在写配置的之后能够进行提示与自动装配 client 代码 package com.xwhking.interfacestarter.client;import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpUtil; import lombok.Data;import java.time.LocalDateTime; import java.util.Date; import java.util.HashMap;import static com.xwhking.interfacestarter.utils.GenSign.genSign;/*** 发起请求时候一定要注意不要把secretKey直接传送只需要进行一个签名传送就好了然后后端通过同样的方式进行签名* 的生成进行对比验证身份。*/ Data public class XWHKINGClient {private String accessKey;private String secretKey;private Long userId;public XWHKINGClient(String accessKey,String secretKey,Long userId){this.userId userId;this.accessKey accessKey;this.secretKey secretKey;}public String getName(String name){//可以单独传入http参数这样参数会自动做URL编码拼接在URL中HashMapString, Object paramMap new HashMap();paramMap.put(name, name);HashMapString,String headerMap new HashMap();headerMap.put(accessKey,accessKey);headerMap.put(userId,userId.toString());headerMap.put(sign, genSign(accessKey,secretKey,userId));headerMap.put(timestamp,Long.toString(new Date().getTime()));String result1 HttpRequest.get(http://localhost:8080/user/name).addHeaders(headerMap).form(paramMap).execute().body();System.out.println(result1);return result1;}public String GetUser(){HashMapString,String headerMap new HashMap();headerMap.put(accessKey,accessKey);headerMap.put(userId,userId.toString());headerMap.put(sign, genSign(accessKey,secretKey,userId));headerMap.put(timestamp,Long.toString(new Date().getTime()));String result1 HttpRequest.get(http://localhost:8080/user/getOne).addHeaders(headerMap).execute().body();System.out.println(result1);return result1;}public static void main(String[] args) {new XWHKINGClient(xwhking,admin123,123123l).getName(XWHKING);} } 这里都加了请求头加请求头的目的是为了进行签名认证。 为什么要进行签名认证 保证安全性一个人不能随便调用如果随便调用的话自己的服务器资源会收到压迫以及资源的损失。适用于无需保存登录态。只认签名不关注用户登录态。 如何进行签名认证 通过 http request header 头传递参数 这里主要传递的参数 accessKey 调用的标识 需要复杂、 无序、无规律这里我没有实现只是简单的模拟如果需要实现的话可以使用现成的签名实现工具包例如 hutoolsecretKey 调用的密钥需要复杂、 无序、无规律该参数一定一定不能放到请求头中不然可能会被别人抓包以及可能造成泄露。用户请求的参数sign 签名由 accessKey 和 secretKey 以及 userId 等信息生成用于传递信息。然后后端通过同样的方式进行生成对比验证权限。上述问题满足了可能还抵挡不了别人的攻击例如别人用重放就可以再次调用 API 了限制重放的方法 加随机数只能使用一次服务端要保存使用过的随机数加 timestamp 时间戳检验时间戳是否过期我这里就是通过时间戳超过10s就失效。 对web中的UserController进行更新 更新就是为了校验然后如果更新了项目记得重启 package com.xwhking.interface_test.controller;import com.xwhking.interface_test.entity.User; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest; import java.util.Date;import static com.xwhking.interface_test.utils.GenSign.genSign;RestController RequestMapping(/user) public class UserController {/*** 校验签名以及其他信息注意这里的 secretKey 是模拟的一般用户的 secretKey 是需要去从数据库取出来* 然后进行验证。* param request*/private void verifyRequest(HttpServletRequest request){String sign request.getHeader(sign);String accessKey request.getHeader(accessKey);String secretKey admin123;String userId request.getHeader(userId);String requestTime request.getHeader(timestamp);long oldTime Long.parseLong(requestTime);long newTime new Date().getTime();if(newTime - oldTime 10000){throw new RuntimeException(检测到请求异常);}String newSign genSign(accessKey,secretKey,Long.parseLong(userId));if(!newSign.equals(sign)){throw new RuntimeException(签名错误);}}GetMapping(/name)public String getName(RequestParam String name , HttpServletRequest request){verifyRequest(request);System.out.println(请求参数名字为 name);return GET 请求参数名字为 name;}GetMapping(/getOne)public User getUser(HttpServletRequest request){verifyRequest(request);User user new User();user.setId(123l);user.setUsername(xwhking);user.setPassword(admin123);System.out.println(user);return user ;} } config 代码 要注意其中的注解 package com.xwhking.interfacestarter.config;import com.xwhking.interfacestarter.client.XWHKINGClient; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; Configuration ConfigurationProperties(prefix xwhking.client) Data ComponentScan public class XWHKINGClientConfig {private String accessKey;private String secretKey;private String userId;Beanpublic XWHKINGClient xwhkingClient(){return new XWHKINGClient(accessKey,secretKey,Long.parseLong(userId));} } 生成签名代码 这里直接使用了 Hutool 工具库中的 sh256 的生成方法 package com.xwhking.interfacestarter.utils;import cn.hutool.crypto.SecureUtil; import lombok.Data;Data public class GenSign {public static String genSign(String accessKey,String secretKey,Long userId){String key xwhking . accessKey . secretKey . userId;return SecureUtil.sha256(key);} } META-INF 中文件的内容 通过看内容就可以看出你需要把后面的内容进行替换写入你的地址。 org.springframework.boot.autoconfigure.EnableAutoConfiguration com.xwhking.interfacestarter.XWHKINGClientConfig到这里 Starter 就完了然后使用 maven 进行打包调用install。这里的打包会把包直接放入你的 maven 仓库打包成功就会出现 BUILD SUCCESS 在其他项目中使用 首先复制这段代码 groupIdcom.xwhking/groupId artifactIdInterfaceStarter/artifactId version0.0.1/version然后在你的其他项目中添加进依赖 dependencygroupIdcom.xwhking/groupIdartifactIdInterfaceStarter/artifactIdversion0.0.1/version /dependency刷新 maven 仓库 然后再 yml 配置中加入配置 在 config中的prefix 可以设置前缀也就是可以更改xwhking.client 然后在你的代码中使用 Test 进行测试 SpringBootTest class MainApplicationTests {Resourceprivate XWHKINGClient xwhkingClient;Testvoid testClient(){xwhkingClient.getName(xwhking) ;}}调用结果 出来了 xwhking 结果成功。 这样开发一个starter 就结束了
http://www.yutouwan.com/news/399512/

相关文章:

  • 昭通市住房和城乡建设局网站wordpress解析优化
  • 怎么做网站写书wordpress开放注册
  • 城市网站建设摘要论文潍坊市企业型网站建设
  • 印刷做网站网上接单网站设计属于什么经营范围
  • 威海网站网站建设台州seo网站管理
  • 如何做网站轮播大图清远市清城区发布
  • 电子商务网站开发教程书内代码我wordpress top主题
  • 恩施网站制作公司360建筑网官网怎么登录
  • 对php网站开发技术课程总结Nginx伪静态WordPress
  • 网站备案登记网站设计步骤详解
  • 白石桥做网站公司制作灯笼的材料
  • 查询网站空间的服务商网站死链删除
  • 搭建一个网站要多少中国世界排名
  • 企业网站的重要性网站建设交易平台
  • 安顺做网站台州响应式建站
  • 营销型网站策划建设微信小程序设计制作
  • 网站后台费用流控插件wordpress
  • 哪些网站怎么进网站制作服务好的商家
  • 网站域名要怎样规划南通建设局网站查询
  • 新东方研学网站那家公司做的扬中网站建设好么
  • 东莞市专注网站建设中江移动网站建设
  • 流媒体网站建设规划 所需设备关于医疗保障局门户网站建设
  • 手机app是用什么软件开发的长沙seo网站建设袁飞最好
  • 建论坛型网站微信公众平台公众号
  • 浚县网站建设东莞市建设工程检测中心网站
  • 青岛谷歌网站建设装饰设计公司资质
  • 建设银行网站注销春节网站怎么做
  • 将网站制作成app建设网站的分析
  • 网站模板代理电话自己做网站大概需要多少钱
  • 英选 网站开发蒲县网站建设