图书馆建设网站打不开,手机开发网站怎么做,男女做暖暖插孔网站,个人网站建立步骤文章目录 前言交互流程基本流程约定公钥人人中台携带公钥获取私钥私钥生成人人中台携带私钥访问私钥验证#xff08;博客系统#xff09; 调试演示总结 前言
前天我们完成了基本的整合#xff0c;但是还没有整合到我们的业务系统#xff0c;也就是博客系统。本来昨天要搞一… 文章目录 前言交互流程基本流程约定公钥人人中台携带公钥获取私钥私钥生成人人中台携带私钥访问私钥验证博客系统 调试演示总结 前言
前天我们完成了基本的整合但是还没有整合到我们的业务系统也就是博客系统。本来昨天要搞一手的但是在练车所以就没有完成。那么今天补上差不多20号去上班实习这点时间赶紧搞完。
首先的话我们这里建好了业务表 并且我们创建好了新的服务
并且我们通过人人开源生成好了对应的后端的一个CURD和基本接口。
然后我们把对应的controller和feign接口做好对接。
如图整合好的feign组件
然后在我们的人人开源中台系统做好调用 这里的话我们可以发现是这样的流程
之后的话在我们的博客系统当中我们有这个
交互流程
那么这里我们今天要实现的就是在人人开源中台系统过来调用到我们的具体服务的api的时候要做到的一个权限验证
因为我们这是两个系统没有共同使用一个用户表这也就是意味着我们一个权限不方便公用一个。所以对应的一些服务得像第三方api一样提供。只不过这个api接口可以通过注册中心知道然后可以由open-feign调用而已。
基本流程
okey,那么在这里的话我们先简单梳理一下我们的基本流程 那么在这里我们可以解读到这几个信息
需要一个公钥接口拿到公钥需要一个申请临时密钥的接口通过这个密钥去访问服务
那么在人人中台要做
先拿到公钥通过公钥拿到私钥携带私钥去访问
对于博客系统
开发公钥接口开发私钥接口对受保护接口进行私钥验证
同时由于公钥接口是公开的得到的公钥在一定时间是一致的因此我们可以直接做个约定公钥是啥也就是可以学一下rouyi这个写死省下一个接口
所以这里的话我们在编码阶段我可以这样干。
约定公钥
我们先约定公钥。 我们可以看到在我们的博客系统当中的话有个获取私钥的接口 这里的话我们写死了就是直接指定公钥是HUTEROX。
人人中台携带公钥获取私钥
然后就是人人中台携带公钥获取私钥 这里的话我们在feign的拦截器当中实现了这个功能。我们先拿到私钥。
私钥生成
那么这个时候我们自然就要生成私钥了这个私钥由我们的博客系统生成。
package com.huterox.hlangserver.controller.innocontroller.auto;import com.huterox.common.utils.R;/*** 负责实现对内部服务调用的一个API授权签发* */
public interface AutoInnoApiService {/*** 签发临时授权码* */R SignApiAutoCode();/*** 验证签发的授权码是否通过* */boolean AcceptApiAutoCode(String signKey,String signal);
}
之后的话就是我们具体的实现类了。
package com.huterox.hlangserver.controller.innocontroller.auto.impl;
import com.alibaba.fastjson.JSON;
import com.huterox.common.sysEntity.SignApiCode;
import com.huterox.common.utils.CodeUtils;
import com.huterox.common.utils.R;
import com.huterox.hlangserver.controller.innocontroller.auto.AutoInnoApiService;
import com.huterox.hlangserver.utils.RedisTransKey;
import com.huterox.hlangserver.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;Service
public class BaseAutoInnoApiServiceImpl implements AutoInnoApiService {private RedisUtils redisUtils;public BaseAutoInnoApiServiceImpl(RedisUtils redisUtils) {this.redisUtils redisUtils;}Autowiredpublic void setRedisUtils(RedisUtils redisUtils) {this.redisUtils redisUtils;}Overridepublic R SignApiAutoCode() {//生成验证SignString signKey CodeUtils.creatCode(10);String signal CodeUtils.creatCode(20);SignApiCode signApiCode new SignApiCode();signApiCode.setSignal(signal);signApiCode.setSignKey(signKey);//设置30秒过期redisUtils.set(RedisTransKey.setServerSysKey(signKey),signApiCode,30, TimeUnit.SECONDS);return R.ok().put(sign,signApiCode);}Overridepublic boolean AcceptApiAutoCode(String signKey,String signal) {//验证传回来的签发对不对然后把这个进行删除Object o redisUtils.get(RedisTransKey.getServerSysKey(signKey));SignApiCode signApiCode JSON.parseObject(o.toString(), SignApiCode.class);redisUtils.del(RedisTransKey.getServerSysKey(signKey));return signApiCode.getSignal().equals(signal)signApiCode.getSignKey().equals(signKey);}
}
在这里的话我用了几个工具类。当然这个在先前的whitehole都提到过。这里的话就不复述了百度都能搞定。当然重要的是这个项目后期也是开源的这里只是记录一下具体的做法而已。后期可能会对代码进行优化但是基本流程是不会发生太大改变的。
人人中台携带私钥访问
之后的话就是我们的人人中台去携带我们的私钥去访问
package io.renren.config;
import com.huterox.common.sysEntity.SignApiCode;
import com.huterox.common.utils.FastJsonUtils;
import com.huterox.common.utils.R;
import com.huterox.feign.server.FeignAutoInnoApiSeverService;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;import java.util.HashMap;
import java.util.Map;Configuration
public class FeignRequestInterceptor implements RequestInterceptor {private FeignAutoInnoApiSeverService feignAutoInnoApiSeverService;public FeignRequestInterceptor(FeignAutoInnoApiSeverService feignAutoInnoApiSeverService) {this.feignAutoInnoApiSeverService feignAutoInnoApiSeverService;}Autowiredpublic void setFeignAutoInnoApiSeverService(FeignAutoInnoApiSeverService feignAutoInnoApiSeverService) {this.feignAutoInnoApiSeverService feignAutoInnoApiSeverService;}Overridepublic void apply(RequestTemplate template) {//先那到临时签证MapString, String map new HashMap();map.put(innoHeader,HUTEROX);R sign feignAutoInnoApiSeverService.getSign(map);//拿到签证去正式访问保护接口String signKey ;String signal ;int code (int) sign.get(code);if(code0){String sign1 FastJsonUtils.toJson(sign.get(sign));SignApiCode signApiCode FastJsonUtils.fromJson(sign1, SignApiCode.class);signKey signApiCode.getSignKey();signal signApiCode.getSignal();}//发送远程调用请求,填写signApiCodetemplate.header(signKey,signKey);template.header(signal,signal);}}
私钥验证博客系统
之后的话我们的博客系统就要去验证我们的密钥对不对。其实你发现这玩意不就是服务之间的验证码嘛
这里的话我们受保护的接口是这些 所以的话我们来创建一个切面来进行处理
package com.huterox.hlangserver.aspect;
import com.huterox.common.utils.R;
import com.huterox.hlangserver.controller.innocontroller.auto.AutoInnoApiService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;Aspect
Component
public class InnoAutoAspect {Pointcut(within(com.huterox.hlangserver.controller.innocontroller.api..*) annotation(org.springframework.web.bind.annotation.RequestMapping))public void verification() {}private AutoInnoApiService autoInnoApiService;public InnoAutoAspect(AutoInnoApiService autoInnoApiService) {this.autoInnoApiService autoInnoApiService;}Autowiredpublic void setAutoInnoApiService(AutoInnoApiService autoInnoApiService) {this.autoInnoApiService autoInnoApiService;}Around(verification())public R verification(ProceedingJoinPoint joinPoint) throws Throwable {HttpServletRequest request ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();String signKey request.getHeader(signKey);String signal request.getHeader(signal);if(!autoInnoApiService.AcceptApiAutoCode(signKey, signal)){return R.error();}return (R) joinPoint.proceed();}
}调试演示
okey,那么之后的话我们来演示具体流程 完整的前端怎么整合的我就不演示了以前的博客都有或者百度都可以
我们来看到这个
拿到私钥然后扩充访问
之后的话再我们那个博客系统可以看到方行。
总结
okey,以上的就是我们今天的内容当然这部分还有优化的点首先就是我们的人人中台在访问的时候我们用的是apply重写了里面的拦截。但是问题在于这个是全局通用的如果我们后面有另一个子系统那么这个时候的话就建议使用切面了。当然这里这样处理是因为这里我就只设计两个子系统上次的whitehole有9个微服务服务器顶不住。所以这次不乱搞了省点资源后面再研究研究这个网站智能助手玩玩。
ps: 有没有广州的大哥春招再联系联系狗头