使用jwt生成token

<!--        jwt相关依赖-->
        <dependency>
            <groupId>com.nimbusds</groupId>
            <artifactId>nimbus-jose-jwt</artifactId>
            <version>6.0</version>
        </dependency>
/**
     * 创建一个32-byte的密匙
     */
    private static final byte[] secret = "f924f68726924ec0a827deac0896a4ff".getBytes();
	//生成一个token
    public static String creatToken(Map<String, Object> payloadMap,byte[] secret) throws JOSEException {
   
    
        JWSHeader jwsHeader = new JWSHeader(JWSAlgorithm.HS256);

        //建立一个载荷Payload
        Payload payload = new Payload(new JSONObject(payloadMap));

        //将头部和载荷结合在一起
        JWSObject jwsObject = new JWSObject(jwsHeader, payload);

        //建立一个密匙
        JWSSigner jwsSigner = new MACSigner(secret);

        //签名
        jwsObject.sign(jwsSigner);

        //生成token
        

你可能感兴趣的:(java)