http://blog.csdn.net/mingzhnglei/article/details/51119836

 

下面贴上自己项目中的一个小小的example

import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSObject;
import com.nimbusds.jose.JWSSigner;
import com.nimbusds.jose.Payload;
import com.nimbusds.jose.crypto.MACSigner;

import net.minidev.json.JSONObject;



/**
 * Created by Fly0116 on 2016/4/9 0009.
 * json web token example
 */
public class Example {

    static String DUOSHUO_SHORTNAME = "test";
    static String DUOSHUO_SECRET = "3d990d2276917dfac04467df11fff26d";

    public static void main(String [] args){

        JSONObject userInfo = new JSONObject();

        userInfo.put("short_name", DUOSHUO_SHORTNAME);//必须项
        userInfo.put("user_key", "1");//必须项
        userInfo.put("name", "网站用户A");//可选项

        Payload payload = new Payload(userInfo);

        JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);
        header.setContentType("jwt");


        // Create JWS object
        JWSObject jwsObject = new JWSObject(header, payload);

        // Create HMAC signer
        JWSSigner signer = new MACSigner(DUOSHUO_SECRET.getBytes());

        try {
            jwsObject.sign(signer);
        } catch (JOSEException e) {
            System.err.println("Couldn't sign JWS object: " + e.getMessage());
            return;
        }
        // Serialise JWS object to compact format
        String token = jwsObject.serialize();
        System.out.println("Serialised JWS object: " + token);
        //示例输出结果为eyJhbGciOiJIUzI1NiIsImN0eSI6Imp3dCJ9.eyJ1c2VyX2tleSI6IjEiLCJuYW1lIjoi572R56uZ55So5oi3QSIsInNob3J0X25hbWUiOiJ0ZXN0In0.NXKDXwXThzFkyfl_k_-p6mfM5cpOFppvfdIjrjEq14I
    }
}

 

http://blog.leapoahead.com/2015/09/07/user-authentication-with-jwt/

 

JSON Web Token - 在Web应用间安全地传递信息

http://blog.leapoahead.com/2015/09/06/understanding-jwt/

 

相关文章:

  • 2021-08-12
  • 2021-03-01
  • 2018-07-16
  • 2022-03-09
  • 2021-07-12
  • 2021-06-15
  • 2021-07-16
猜你喜欢
  • 2021-11-05
  • 2021-10-18
  • 2021-12-07
  • 2021-05-23
  • 2021-10-31
  • 2021-05-31
相关资源
相似解决方案