增加OIDC协议支持

This commit is contained in:
zlt2000
2021-05-17 23:16:09 +08:00
parent c1452394ea
commit 7431a35e9c
10 changed files with 314 additions and 1 deletions
@@ -0,0 +1,74 @@
package com.central.oauth2.common.constants;
/**
* id_token属性名常量
*
* @author zlt
* @version 1.0
* @date 2021/4/23
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
public class IdTokenClaimNames {
/**
* {@code iss} - the Issuer identifier
*/
public final static String ISS = "iss";
/**
* {@code sub} - the Subject identifier
*/
public final static String SUB = "sub";
/**
* {@code aud} - the Audience(s) that the ID Token is intended for
*/
public final static String AUD = "aud";
/**
* {@code exp} - the Expiration time on or after which the ID Token MUST NOT be accepted
*/
public final static String EXP = "exp";
/**
* {@code iat} - the time at which the ID Token was issued
*/
public final static String IAT = "iat";
/**
* {@code auth_time} - the time when the End-User authentication occurred
*/
public final static String AUTH_TIME = "auth_time";
/**
* {@code nonce} - a {@code String} value used to associate a Client session with an ID Token,
* and to mitigate replay attacks.
*/
public final static String NONCE = "nonce";
/**
* {@code acr} - the Authentication Context Class Reference
*/
public final static String ACR = "acr";
/**
* {@code amr} - the Authentication Methods References
*/
public final static String AMR = "amr";
/**
* {@code azp} - the Authorized party to which the ID Token was issued
*/
public final static String AZP = "azp";
/**
* {@code at_hash} - the Access Token hash value
*/
public final static String AT_HASH = "at_hash";
/**
* {@code c_hash} - the Authorization Code hash value
*/
public final static String C_HASH = "c_hash";
}
@@ -4,15 +4,22 @@ import com.central.common.constant.SecurityConstants;
import com.central.common.utils.JsonUtil;
import com.central.common.utils.RsaUtils;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.cloud.bootstrap.encrypt.KeyProperties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaSigner;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.security.jwt.crypto.sign.SignatureVerifier;
import org.springframework.security.jwt.crypto.sign.Signer;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.stream.Collectors;
@@ -84,4 +91,18 @@ public class JwtUtils {
public static boolean checkExp(JsonNode claims) {
return checkExp(claims, System.currentTimeMillis());
}
public static Jwt encode(CharSequence content, KeyProperties keyProperties) {
KeyPair keyPair = new KeyStoreKeyFactory(
keyProperties.getKeyStore().getLocation(),
keyProperties.getKeyStore().getSecret().toCharArray())
.getKeyPair(keyProperties.getKeyStore().getAlias());
PrivateKey privateKey = keyPair.getPrivate();
Signer rsaSigner = new RsaSigner((RSAPrivateKey) privateKey);
return JwtHelper.encode(content, rsaSigner);
}
public static String encodeStr(CharSequence content, KeyProperties keyProperties) {
return encode(content, keyProperties).getEncoded();
}
}
@@ -158,4 +158,13 @@ public interface SecurityConstants {
* rsa公钥
*/
String RSA_PUBLIC_KEY = "pubkey.txt";
/**
* 获取id_token的response_type
*/
String ID_TOKEN = "id_token";
/**
* 令牌颁发者
*/
String ISS = "http://zlt2000.cn";
}