Giter VIP home page Giter VIP logo

jwtdemo's Introduction

JWT Demo App

Bu Java uygulaması içinde JWT oluşturma ve doğrulama adımlarını bulabilirsiniz.

SHA-256 üretme adımı
public String doHMACSHA256(String part1AndPart2, String secretKey){
    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(new SecretKeySpec(secretKey.getBytes(), "HmacSHA256"));

    byte[] hashBytes = mac.doFinal(part1AndPart2.getBytes());
    String hash = doBASE64(hashBytes);
    return hash;
}
Base64 üretme adımı
public String doBASE64(byte[] bytes) {
    Base64.Encoder encoder = Base64.getEncoder();
    String base64 = encoder.encodeToString(bytes);
    return base64;
}

public String doBASE64(String input) {
    byte[] bytes = input.getBytes(Charset.forName("UTF-8"));
    String base64 = doBASE64(bytes);
    return base64;
}
JWT üretme adımı
public String generateJWT(){

    String HEADER = String.join("\n", Files.readAllLines(Paths.get("./header.json")));
    String PAYLOAD = String.join("\n", Files.readAllLines(Paths.get("./payload.json")));

    String PART1 = doBASE64(HEADER);
    String PART2 = doBASE64(PAYLOAD);

    String PART1_PART2 = PART1 + "." + PART2;

    String PART3 = doBASE64(doHMACSHA256(PART1_PART2, SECRET_KEY));

    String JWT_TOKEN = PART1_PART2 + "." + PART3;

    return JWT_TOKEN;
}
JWT doğrulama adımı
public boolean validateJWT(String jwt) {

    String[] parts = jwt.split("\\.");
    String PART1 = parts[0];
    String PART2 = parts[1];
    String PART3 = parts[2];

    String PART1_PART2 = PART1 + "." + PART2;

    String jwtSignature = doBASE64(doHMACSHA256(PART1_PART2, SECRET_KEY));

    return jwtSignature.equals(PART3);

}
Çalıştırma adımı
String JWT_TOKEN = generateJWT();

boolean isValid = validateJWT(JWT_TOKEN);

System.out.println("Is valid ? " + isValid);

jwtdemo's People

Contributors

rahmanusta avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.