单点登录

解决的问题:
解决的是在多个系统中频繁登录,登录一次,其他系统共用
在这里插入图片描述

整个登录过程中,最关键的点是什么?
token的安全性
token是识别客户端身份的唯一标识,如果加密不够严密,被人伪造那就完蛋了。
采用何种方式加密才是安全可靠的呢?

我们将采用JWT + RSA非对称加密

token我设置30分钟,一天下来,一个人最多请求48次,而tomcat可以处理并发请求据官方测试是150。所以这是可以的。
在这里插入图片描述

授权中心的主要职责:

  • 用户鉴权:
    • 接收用户的登录请求,通过用户中心的接口进行校验,通过后生成JWT
    • 使用私钥生成JWT并返回

在这里插入图片描述

因为是微服务,所以每个功能都是一个服务。
为了能在其他的微服务中使用common
auth不需要被其他服务依赖,但是可以被远程访问,
所以如图所示,这样搭建

在这里插入图片描述

工具类:
在这里插入图片描述

编写JWT工具

理论介绍:http://blog.damonare.cn/2017/12/31/RSA算法详解/#more
1,RSA工具类:RsaUtils

public class RsaUtils {
/**
 * 从文件中读取公钥
 *
 * @param filename 公钥保存路径,相对于classpath
 * @return 公钥对象
 * @throws Exception
 */
public static PublicKey getPublicKey(String filename) throws Exception {
    byte[] bytes = readFile(filename);
    return getPublicKey(bytes);
}

/**
 * 从文件中读取密钥
 *
 * @param filename 私钥保存路径,相对于classpath
 * @return 私钥对象
 * @throws Exception
 */
public static PrivateKey getPrivateKey(String filename) throws Exception {
    byte[] bytes = readFile(filename);
    return getPrivateKey(bytes);
}

/**
 * 获取公钥
 *
 * @param bytes 公钥的字节形式
 * @return
 * @throws Exception
 */
public static PublicKey getPublicKey(byte[] bytes) throws Exception {
    X509EncodedKeySpec spec = new X509EncodedKeySpec(bytes);
    KeyFactory factory = KeyFactory.getInstance("RSA");
    return factory.generatePublic(spec);
}

/**
 * 获取密钥
 *
 * @param bytes 私钥的字节形式
 * @return
 * @throws Exception
 */
public static PrivateKey getPrivateKey(byte[] bytes) throws Exception {
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bytes);
    KeyFactory factory = KeyFactory.getInstance("RSA");
    return factory.generatePrivate(spec);
}

/**
 * 根据密文,生存rsa公钥和私钥,并写入指定文件
 *
 * @param publicKeyFilename  公钥文件路径
 * @param privateKeyFilename 私钥文件路径
 * @param secret             生成密钥的密文
 * @throws IOException
 * @throws NoSuchAlgorithmException
 */
public static void generateKey(String publicKeyFilename, String privateKeyFilename, String secret) throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    SecureRandom secureRandom = new SecureRandom(secret.getBytes());
    keyPairGenerator.initialize(1024, secureRandom);
    KeyPair keyPair = keyPairGenerator.genKeyPair();
    // 获取公钥并写出
    byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
    writeFile(publicKeyFilename, publicKeyBytes);
    // 获取私钥并写出
    byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
    writeFile(privateKeyFilename, privateKeyBytes);
}

private static byte[] readFile(String fileName) throws Exception {
    return Files.readAllBytes(new File(fileName).toPath());
}

private static void writeFile(String destPath, byte[] bytes) throws IOException {
    File dest = new File(destPath);
    if (!dest.exists()) {
        dest.createNewFile();
    }
    Files.write(dest.toPath(), bytes);
}


/**
 * 测试RSAUtils工具类的使用
 *
 * @param args
 */
public static void main(String[] args
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值