// you will need to install via 'npm install jsonwebtoken' or in your package.jsonvar jwt = require("jsonwebtoken");var METABASE_SITE_URL = "http://127.0.0.1:3000";var METABASE_SECRET_KEY = "76e7feb6e9456f9f924a9f463d03cbcaedf72b4a68844fa88e5f05c6345d409d";var payload = { resource: { dashboard: 2 }, params: {}, exp: Math.round(Date.now() / 1000) + (10 * 60) // 10 minute expiration};var token = jwt.sign(payload, METABASE_SECRET_KEY);var iframeUrl = METABASE_SITE_URL + "/embed/dashboard/" + token + "#bordered=true&titled=true";
import com.alibaba.fastjson.JSONObject;import io.jsonwebtoken.Jwts;import io.jsonwebtoken.SignatureAlgorithm;import java.util.Base64;import java.util.Date;import java.util.Map;/** * 报表生成加密URL */public class JWTUtils { /** * 创建token */ public static String createJWT(Map<String, Object> payload, String secretKey){ try { String metaBaseEncodedSecretKey = Base64.getEncoder().encodeToString(secretKey.getBytes()); return Jwts.builder() .setHeaderParam("typ", "JWT") .setClaims(payload) .signWith(SignatureAlgorithm.HS256, metaBaseEncodedSecretKey) .setIssuedAt(new Date()) .compact(); }catch (Exception e){ e.printStackTrace(); } return null; } /** * 创建token */ public static String getTokenUrl(Integer dashboard,String secretKey){ int round = Math.round(System.currentTimeMillis() / 1000)+10*60; // 10 minute expiration JSONObject resource = new JSONObject(); resource.put("dashboard",dashboard); JSONObject payload = new JSONObject(); payload.put("resource",resource); payload.put("params",new JSONObject()); payload.put("exp",round); String token = JWTUtils.createJWT(payload,secretKey); return BI_SITE_URL + "/embed/dashboard/" + token + "#theme=night&bordered=false&titled=false"; } private static final String BI_SITE_URL = "http://127.0.0.1:3000"; public static void main(String[] args) { String secretKey = "******"; String url = getTokenUrl(2,secretKey); System.out.println(url); }}
文章有问题?点此查看未经处理的缓存