package com.zhangmeng.oauth.dto;

import com.alibaba.fastjson.JSON;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaSigner;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;

import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 转身的背影在心底里沉沦
 * @date 2021年9月18日12:23:40
 * @version 1.0
 */
public class JwtToken {
    public static String adminJwt(){
        //证书文件路径
        String key_location="mystyle-cloud.jks";
        //秘钥库密码
        String key_password="mystyle-cloud";
        //秘钥密码
        String keypwd = "mystyle-cloud";
        //秘钥别名
        String alias = "mystyle-cloud";

        //访问证书路径
        ClassPathResource resource = new ClassPathResource(key_location);

        //创建秘钥工厂
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(resource,key_password.toCharArray());

        //读取秘钥对(公钥、私钥)
        KeyPair keyPair = keyStoreKeyFactory.getKeyPair(alias,keypwd.toCharArray());

        //获取私钥
        RSAPrivateKey rsaPrivate = (RSAPrivateKey) keyPair.getPrivate();

        //定义Payload9(载荷,给生成的令牌加权限)
        Map<String, Object> tokenMap = new HashMap<>();
        // key 就是解析后的authorities, value是权限 信息可以有多个
        tokenMap.put("authorities", new String[]{"admin"});

        //生成Jwt令牌
        Jwt jwt = JwtHelper.encode(JSON.toJSONString(tokenMap), new RsaSigner(rsaPrivate));

        //取出令牌
        return jwt.getEncoded();
    }
}