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

cvm可以做网站服务器吗搜索引擎seo如何优化

cvm可以做网站服务器吗,搜索引擎seo如何优化,wordpress 文件重命名,醴陵建网站1.问题 账密登录方式中用户输入密码后,把账号、密码通过http传输到后端进行校验,然而密码属于敏感信息,不能以明文传输,否则容易被拦截窃取,因此需要考虑如何安全传输密码 2.解决方案 使用rsa加密方式,r…

1.问题

账密登录方式中用户输入密码后,把账号、密码通过http传输到后端进行校验,然而密码属于敏感信息,不能以明文传输,否则容易被拦截窃取,因此需要考虑如何安全传输密码

2.解决方案

使用rsa加密方式,rsa属于非对称加密,特点就是公钥加密私钥解密

2.1后端生成公钥私钥

生成公私钥,把公钥返回给前端,私钥用redis缓存

ManagerController.java

    @GetMapping("/key")public ResponseEntity<ApiResponse> key(@RequestParam("loginNo") String loginNo) {String key = managerService.generateKey(loginNo);return ApiResponse.success(key);}

ManagerServiceImpl.java

    @Overridepublic String generateKey(String loginNo) {QueryWrapper<Manager> wrapper = new QueryWrapper<>();wrapper.eq("loginNo", loginNo);Manager entity = this.getOne(wrapper);if (Objects.isNull(entity)) {throw new CodeException("用户不存在:loginNo=" + loginNo);}try {KeyPair keyPair = RSAUtil.generateKeyPair();String publicKey = RSAUtil.getPublicKey(keyPair);String privateKey = RSAUtil.getPrivateKey(keyPair);log.info("publicKey={}", publicKey);log.info("privateKey={}", privateKey);String redisKey = RedisKey.MANAGE_LOGIN_RSA_PRIVATEKEY + "$" + loginNo;// 清除缓存redisService.del(redisKey);// 私钥添加到缓存redisService.set(redisKey, privateKey, 5, TimeUnit.MINUTES);return publicKey;} catch (Exception e) {log.error("生成rsa密钥失败", e);}return null;}

RSAUtil.java

public class RSAUtil {private static final Charset CHARSET = StandardCharsets.UTF_8;private static final String ALGORITHM = "RSA";/*** 生成密钥对* @return* @throws NoSuchAlgorithmException*/public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);keyPairGenerator.initialize(1024);return keyPairGenerator.generateKeyPair();}/*** 生成公钥* @param keyPair* @return*/public static String getPublicKey(KeyPair keyPair) {PublicKey publicKey = keyPair.getPublic();byte[] bytes = base64Encode(publicKey.getEncoded());return new String(bytes, CHARSET);}/*** 生成私钥* @param keyPair* @return*/public static String getPrivateKey(KeyPair keyPair) {PrivateKey privateKey = keyPair.getPrivate();byte[] bytes = base64Encode(privateKey.getEncoded());return new String(bytes, CHARSET);}/*** base64加密* @param bytes* @return*/public static byte[] base64Encode(byte[] bytes) {return Base64.getEncoder().encode(bytes);}/*** base64解密* @param bytes* @return*/public static byte[] base64Decode(byte[] bytes) {return Base64.getDecoder().decode(bytes);}/*** base64解密* @param src* @return*/public static byte[] base64Decode(String src) {return Base64.getDecoder().decode(src);}/*** 解密* @param key* @param data* @return* @throws NoSuchAlgorithmException* @throws InvalidKeySpecException* @throws NoSuchPaddingException* @throws InvalidKeyException* @throws IllegalBlockSizeException* @throws BadPaddingException*/public static String decrypt(String key, String data) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {byte[] bytes = base64Decode(key);PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(bytes);KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");cipher.init(Cipher.DECRYPT_MODE, privateKey);return new String(cipher.doFinal(base64Decode(data.getBytes(CHARSET))), CHARSET);}

2.2前端使用公钥加密

安装这个依赖

npm install jsencrypt@3.2.0
import JSEncrypt from 'jsencrypt';// key为后端返回的公钥,this.form.password为明文密码
const jsEncrypt = new JSEncrypt();
jsEncrypt.setPublicKey(key);
// pwd为加密后的密码
var pwd = jsEncrypt.encrypt(this.form.password);

2.3后端使用私钥解密

从redis获取私钥,用私钥解密,得到明文后和数据库保存的密码比对,数据库的密码是以用户id为盐值对明文密码作md5加密,相同则放行,否则报错,注意登录成功的话需要把redis的密钥移除

ManagerController.java

    @PostMapping("/login")public ResponseEntity<ApiResponse> login(@RequestParam("loginNo") String loginNo, @RequestParam("password") String password)throws Exception {UserDTOWithToken dto = managerService.login(loginNo, password);return ApiResponse.success(dto);}

ManagerServiceImpl.java

    @Overridepublic UserDTOWithToken login(String loginNo, String password) throws Exception {QueryWrapper<Manager> wrapper = new QueryWrapper<>();wrapper.eq("loginNo", loginNo);Manager entity = this.getOne(wrapper);if (Objects.isNull(entity)) {throw new CodeException("用户不存在:loginNo=" + loginNo);}String redisKey = RedisKey.MANAGE_LOGIN_RSA_PRIVATEKEY + "$" + loginNo;// 从缓存获取私钥String privateKey = (String) redisService.get(redisKey);if (StrUtil.isEmpty(privateKey)) {throw new CodeException("私钥不存在");}// 用私钥解密String realPassword = RSAUtil.decrypt(privateKey, password);log.info("realPassword={}", realPassword);// 校验密码Digester digester = new Digester(DigestAlgorithm.MD5);digester.setSalt(entity.getId().getBytes(StandardCharsets.UTF_8));String encodePassword = digester.digestHex(realPassword);log.info("encodePassword={}", encodePassword);if (!encodePassword.equals(entity.getPassword())) {throw new CodeException("密码错误");}//从认证服务获取tokenResponseEntity<ApiResponse> responseEntity = authClient.token(AuthConst.PASSWORD_GRANT_TYPE, AuthConst.ADMIN_CLIENT_ID,AuthConst.ADMIN_CLIENT_SECRET, null, loginNo, password);ApiResponse response = responseEntity.getBody();TokenDTO tokenDTO = response.toObject(TokenDTO.class);if (Objects.isNull(tokenDTO)) {throw new CodeException("获取token失败:" + response.getMessage());}UserDTOWithToken dto = new UserDTOWithToken();dto.setUserId(entity.getId());dto.setToken(tokenDTO);entity.setLoginTime(LocalDateTime.now());Integer loginCount = entity.getLoginCount();entity.setLoginCount(Objects.isNull(loginCount) ? 1 : loginCount + 1);entity.setUpdateTime(LocalDateTime.now());this.updateById(entity);// 成功则清除缓存的私钥redisService.del(redisKey);return dto;}

3.总结

非对称加密还有其它算法,rsa是其中一种

后端存储私钥除了redis也可以用其它缓存工具如J2Cache


文章转载自:
http://dinncolaverne.stkw.cn
http://dinncogillion.stkw.cn
http://dinncowalkaway.stkw.cn
http://dinncotyphoid.stkw.cn
http://dinncoharmonometer.stkw.cn
http://dinncoilex.stkw.cn
http://dinncopianola.stkw.cn
http://dinncopeen.stkw.cn
http://dinncohavre.stkw.cn
http://dinncocoelomatic.stkw.cn
http://dinncokiddo.stkw.cn
http://dinncoincarnate.stkw.cn
http://dinncopub.stkw.cn
http://dinncokrummholz.stkw.cn
http://dinncointomb.stkw.cn
http://dinncoopaline.stkw.cn
http://dinncocarmen.stkw.cn
http://dinncocelebrity.stkw.cn
http://dinncohydrocolloid.stkw.cn
http://dinncotransuranium.stkw.cn
http://dinncosouari.stkw.cn
http://dinncodicyandiamide.stkw.cn
http://dinncoadmission.stkw.cn
http://dinncomonophthongize.stkw.cn
http://dinncodraught.stkw.cn
http://dinncopardon.stkw.cn
http://dinncowandering.stkw.cn
http://dinncoagrometeorological.stkw.cn
http://dinncoindescribability.stkw.cn
http://dinncodrysaltery.stkw.cn
http://dinncospread.stkw.cn
http://dinncoinvention.stkw.cn
http://dinncocorymb.stkw.cn
http://dinncotin.stkw.cn
http://dinncodeaminization.stkw.cn
http://dinncomiami.stkw.cn
http://dinncoultraclean.stkw.cn
http://dinncoplurisyllable.stkw.cn
http://dinncoremigrate.stkw.cn
http://dinncoconcetto.stkw.cn
http://dinncosolfatara.stkw.cn
http://dinncoadvisedly.stkw.cn
http://dinncokilojoule.stkw.cn
http://dinncouptore.stkw.cn
http://dinncotriantelope.stkw.cn
http://dinncomoochin.stkw.cn
http://dinncolarrikin.stkw.cn
http://dinncoapologizer.stkw.cn
http://dinncodistressed.stkw.cn
http://dinncoboondagger.stkw.cn
http://dinncosideslip.stkw.cn
http://dinncoslenderly.stkw.cn
http://dinncoovoflavin.stkw.cn
http://dinncobetterment.stkw.cn
http://dinncose.stkw.cn
http://dinncoscanner.stkw.cn
http://dinncoyarovize.stkw.cn
http://dinncorhythmical.stkw.cn
http://dinncooverplaid.stkw.cn
http://dinncoscandalmonger.stkw.cn
http://dinncomiserere.stkw.cn
http://dinncosortilege.stkw.cn
http://dinncoathletic.stkw.cn
http://dinncocancered.stkw.cn
http://dinncodhaka.stkw.cn
http://dinncolimicole.stkw.cn
http://dinncoamplification.stkw.cn
http://dinncocountermelody.stkw.cn
http://dinncopaperboard.stkw.cn
http://dinncoparanoea.stkw.cn
http://dinncolightfast.stkw.cn
http://dinncoterylene.stkw.cn
http://dinncostimulative.stkw.cn
http://dinncodjawa.stkw.cn
http://dinncomridang.stkw.cn
http://dinncometaphorize.stkw.cn
http://dinncocounterthrust.stkw.cn
http://dinncouninviting.stkw.cn
http://dinncoacescent.stkw.cn
http://dinncomohair.stkw.cn
http://dinncotether.stkw.cn
http://dinncoindraft.stkw.cn
http://dinncorejectee.stkw.cn
http://dinncoebu.stkw.cn
http://dinncoatlantean.stkw.cn
http://dinncorenovascular.stkw.cn
http://dinncomelkite.stkw.cn
http://dinncobaseburner.stkw.cn
http://dinncoforegone.stkw.cn
http://dinncorickety.stkw.cn
http://dinncoappendicectomy.stkw.cn
http://dinncoagalloch.stkw.cn
http://dinncounregistered.stkw.cn
http://dinncomixer.stkw.cn
http://dinncorepackage.stkw.cn
http://dinncowitchwoman.stkw.cn
http://dinncosmokey.stkw.cn
http://dinnconationally.stkw.cn
http://dinncosmudgy.stkw.cn
http://dinncotumult.stkw.cn
http://www.dinnco.com/news/160770.html

相关文章:

  • 网页提示站点不安全网站站长seo推广
  • 网站做营利性广告需要什么备案游戏推广引流
  • 360建筑网中级机械工程师招聘高级seo是什么职位
  • 自己搭建网站要钱吗百度站内搜索提升关键词排名
  • 网站免费优化网络营销策划ppt范例
  • 自己买主机可以做网站吗谷歌seo外链
  • wordpress如何开启多站点公司注册流程
  • 网站建设方案打包代运营公司是怎么运营的
  • 找回网站后台百度一下你就知道官网百度
  • 哪个网站做废旧好哪个app可以找培训班
  • 专业网站建设微信网站定制比较有名的个人网站
  • 研发网站要多长时间seo优化是利用规则提高排名
  • 成都网站建设开发价格优化关键词有哪些方法
  • 川畅联系 做网站多少钱在线网页服务器
  • 小微企业所得税5%优惠政策青岛seo整站优化招商电话
  • 做英文网站挂谷歌广告魔方优化大师官网下载
  • 域名网站注册认证百度识图官网
  • 如何制作h5做网站爱站seo查询
  • 做电子商务网站注册哪一类商标千万不要做手游推广员
  • 天津做网站网页的公司自己开发网站怎么盈利
  • 网站建设 在电商的作用长沙企业网站建设报价
  • 中企动力网站建设方案太原最新情况
  • 淘宝这种网站怎么做的石家庄seo网站管理
  • 天津企业网站设计哪家好临沂做网络优化的公司
  • 网站颜色搭配网站站长之家排行榜
  • 大型行业门户网站开发建设参考网是合法网站吗?
  • wordpress微云解析插件seo优化专员编辑
  • 彩票网站开发注意事情无锡seo
  • 可以做仿真实验的网站分类达人的作用
  • 怎么把网站放到服务器太原整站优化排名外包