华为网站开发流程,做帖子网站,2100000000级超变传奇,博客系统做网站为什么 C 端系统不能直接暴露自增 ID#xff1f;在后端系统中#xff0c;我们习惯使用数据库自增 ID#xff0c;并习惯性的直接返回给C端交互使用#xff0c;例如#xff1a;登录接口在登录成功后返回的用户基础信息 {userId: 100,username: import org.springframework.stereotype.Service; import java.util.Arrays; Service public class HashidsService { //Bean单例不存在线程安全问题 private final Hashids hashids new Hashids(); public String encode(int code) { return hashids.encode(code); } public String encode(long code) { return hashids.encode(code); } public long decode(String decoded) { long[] decodes hashids.decode(decoded); if (decodes.length 0) { throw new IllegalArgumentException(非法ID); } return decodes[0]; } public String encodeArr(int[] codes) { long[] codeArr Arrays.stream(codes).asLongStream().toArray(); return hashids.encode(codeArr); } public String encodeArr(long[] codes) { return hashids.encode(codes); } public long[] decodeArr(String decoded) { long[] decodes hashids.decode(decoded); if (decodes.length 0) { throw new IllegalArgumentException(非法ID); } return decodes; } }加盐加盐混淆防止别人用同样库解你 IDsalt 一旦上线 绝对不能改# application.yml hashids: salt: kjsdfiaosudkskldjfa #混淆用的盐 min-length: 8 #最小长度查看代码package com.ks.demo.uc.hashids; import org.hashids.Hashids; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.util.Arrays; /** * 加盐混淆 * * salt的作用 * 防止别人用同样库解你 ID * salt 一旦上线 绝对不能改 */ Service public class HashidsSaltService { Value(${hashids.salt}) private String salt; Value(${hashids.min-length}) private int minLength; //new在Value注入之前 //解决方案后构造器在构造器的入参使用Value使用ConfigurationProperties单独注入 //private Hashids hashidsSalt new Hashids(salt); private Hashids hashidsSalt null; private Hashids hashidsMinLen null; PostConstruct public void init() { hashidsSalt new Hashids(salt); hashidsMinLen new Hashids(salt, minLength); } public String encode(int code) { return hashidsSalt.encode(code); } public String encode(long code) { return hashidsSalt.encode(code); } public long decode(String decoded) { long[] decodes hashidsSalt.decode(decoded); if (decodes.length 0) { throw new IllegalArgumentException(非法ID); } return decodes[0]; } public String encodeArr(int[] codes) { long[] codeArr Arrays.stream(codes).asLongStream().toArray(); return hashidsSalt.encode(codeArr); } public String encodeArr(long[] codes) { return hashidsSalt.encode(codes); } public long[] decodeArr(String decoded) { long[] decodes hashidsSalt.decode(decoded); if (decodes.length 0) { throw new IllegalArgumentException(非法ID); } return decodes; } public String encodeMinLen(int code) { return hashidsMinLen.encode(code); } public String encodeMinLen(long code) { return hashidsMinLen.encode(code); } public long decodeMinLen(String decoded) { long[] decodes hashidsMinLen.decode(decoded); if (decodes.length 0) { throw new IllegalArgumentException(非法ID); } return decodes[0]; } public String encodeMinLenArr(int[] codes) { long[] codeArr Arrays.stream(codes).asLongStream().toArray(); return hashidsMinLen.encode(codeArr); } public String encodeMinLenArr(long[] codes) { return hashidsMinLen.encode(codes); } public long[] decodeMinLenArr(String decoded) { long[] decodes hashidsMinLen.decode(decoded); if (decodes.length 0) { throw new IllegalArgumentException(非法ID); } return decodes; } }自定义参与编码的字符字符集规则至少 16 个字符不允许重复字符# application.yml hashids: salt: kjsdfiaosudkskldjfa #混淆用的盐 min-length: 8 #最小长度 #至少 16 个字符不允许重复字符 #参与编码的字符可以剔除调0/O/o,1/I/l等字符增强可读性 base-char: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz