JWTUtils decoder Android in Tamil (Java and Kotlin)


  •  முதலில், நீங்கள் போன் குறியீட்டை பயன்படுத்தி பயனர் அடையாளத்தை கணக்கில் உள்ளிடவும்:
Java
String token = "your_token";

Kotlin
val token = "your_token"

  • பின்னர், கீழே குறிப்பிட்ட decodeJWT() படி பயன்படுத்தி, மேலும் கீழே குறிப்பிட்ட Base64 வழியாக செயல்படுத்தப்பட்டு உள்ள குறியீட்டை பின்வரும் மாறியை மீட்கப் பயன்படுத்தலாம்:
Java

import android.util.Base64;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;

public class JWTUtils {
    public static JSONObject decodeJWT(String token) throws Exception {
        String[] split = token.split("\\.");
        String base64EncodedHeader = split[0];
        String base64EncodedBody = split[1];
        String base64EncodedSignature = split[2];

        JSONObject header = new JSONObject(getJson(base64EncodedHeader));
        JSONObject body = new JSONObject(getJson(base64EncodedBody));

        JSONObject jwt = new JSONObject();
        try {
            jwt.put("header", header);
            jwt.put("body", body);
            jwt.put("signature", base64EncodedSignature);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jwt;
    }

    private static String getJson(String strEncoded) throws UnsupportedEncodingException {
        byte[] decodedBytes = Base64.decode(strEncoded, Base64.URL_SAFE);
        return new String(decodedBytes, "UTF-8");
    }
}

Kotlin

import android.util.Base64
import org.json.JSONException
import org.json.JSONObject
import java.io.UnsupportedEncodingException

object JWTUtils {
    fun decodeJWT(token: String): JSONObject {
        val split = token.split("\\.".toRegex()).toTypedArray()
        val base64EncodedHeader = split[0]
        val base64EncodedBody = split[1]
        val base64EncodedSignature = split[2]

        val header = JSONObject(getJson(base64EncodedHeader))
        val body = JSONObject(getJson(base64EncodedBody))

        val jwt = JSONObject()
        try {
            jwt.put("header", header)
            jwt.put("body", body)
            jwt.put("signature", base64EncodedSignature)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        return jwt
    }

    @Throws(UnsupportedEncodingException::class)
    private fun getJson(strEncoded: String): String {
        val decodedBytes = Base64.decode(strEncoded, Base64.URL_SAFE)
        return String(decodedBytes, Charsets.UTF_8)
    }
}



கருத்துரையிடுக

Post a Comment (0)

புதியது பழையவை