android 中使用jwt token(json web token)--java

來源:互聯網
上載者:User

標籤:

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    }}

 

八幅漫畫理解使用JSON Web Token設計單點登入系統

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

 

JSON Web Token - 在Web應用間安全地傳遞資訊

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

 

android 中使用jwt token(json web token)--java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.