Home > front end >  Client-side validation step done after sign with apple
Client-side validation step done after sign with apple

Time:09-15

Want to so much why, use the tools to change the configuration parameters, a direct getUserInfo () method can be finished, lazy people is such a play, not nonsense, directly on the code:
This is a utility class:
Package com. ZJTX. Util.
The import com. Alibaba. Fastjson. JSONArray;
The import com. Alibaba. Fastjson. JSONObject;
The import com. Auth0. JWK. JWK;
The import com. ZJTX. Dto. AppleReturnTokenDTO;
The import com. ZJTX. Util. Exception. ServiceException;
The import IO. Jsonwebtoken. *;
The import org.apache.com mons. Codec. Binary. Base64;
The import org.apache.com mons. Lang3. StringUtils;
The import org. Slf4j. Logger;
The import org. Slf4j. LoggerFactory;
The import org. Springframework. Beans. Factory. The annotation. Autowired;
The import org. Springframework. HTTP. HttpEntity;
The import org. Springframework. HTTP. HttpHeaders;
The import org. Springframework. HTTP. MediaType;
The import org. Springframework. HTTP. ResponseEntity;
The import org. Springframework. Util. LinkedMultiValueMap;
The import org. Springframework. Util. MultiValueMap;
The import org. Springframework. Web. Client. RestTemplate;

Import the Java. Math. BigInteger;
Import the Java. Security. KeyFactory;
Import the Java. Security. PrivateKey;
Import the Java. Security. PublicKey;
Import the Java. Security. Spec. PKCS8EncodedKeySpec;
Import the Java. Security. Spec. RSAPublicKeySpec;
Import the Java. Util. HashMap;
import java.util.Map;

/* *
* apple login tools
* @ author WangDeyu (Wang Deyu)
* @ the date 2020-09-08 16:10:22
* */
Public class AppleThirdUtils {

The @autowired
The static RestTemplate RestTemplate;

Private static final Logger Logger=LoggerFactory. GetLogger (AppleThirdUtils. Class);

/* *
* client_id (application id from apple registration application for)
* */
Private static final String APPLICATION_ID="";

/* *
* key key (get) from TXT file
* */
Private static final String SECRET_KEY="";

/* *
* p8 document for kid
* */
Private static final String FILE_KID="";

/* *
* p8 file access team_id
* */
Private static final String TEAM_ID="";

/* *
* a fixed value (for authentication token interface)
* */
Private static final String GRANT_TYPE="authorization_code";

/* *
* get public address
* */
Private static final String PUBLIC_KEY_URL="https://appleid.apple.com/auth/keys";

/* *
* access authentication token address
* */
Private static final String GET_ID_TOKEN="https://appleid.apple.com/auth/token";

/* *
* apple's official website address
* */
Private static final String APPLE_URL="https://appleid.apple.com";

/* *
* apple validation after a successful return to the login user information time
* */
Private static final String AUTH_TIME="AUTH_TIME";



/* *
* get verification code
* */
Private static String getValidateCode (String code) {
RestTemplate=new restTemplate ();
//request apple to verify interface
ResponseEntity Response=restTemplate. PostForEntity (GET_ID_TOKEN, AppleThirdUtils getRequestParams (code), String, class);

Return the response. GetBody ();
}

/* *
* build verification log parameter
* @ author WangDeyu
* */
Private static HttpEntity GetRequestParams (String code) {

//build request parameter
HttpHeaders headers=new HttpHeaders();
MultiValueMap The map=new LinkedMultiValueMap<> (a);
Headers. SetContentType (MediaType. APPLICATION_FORM_URLENCODED);
The map. The add (" client_id ", APPLICATION_ID);
The map. The add (" client_secret ", getSecretKey ());
The map. The add (" code ", code);
The map. The add (" grant_type ", grant_type);

Return new HttpEntity<> (map, headers);
}

/* *
* read a file of a key to unlock the key,
* */
Private static byte [] readKey () {
Return Base64. DecodeBase64 (SECRET_KEY);
}

/* *
* access to the secret key
* */
Private static String getSecretKey () {
Try {
Map The header=new HashMap<> (16);
//reference background configuration kid
The header. The put (" kid ", FILE_KID);
Map Claims=new HashMap<> (16);
//reference background configuration team id
Claims. The put (" iss ", TEAM_ID);
Long=System. CurrentTimeMillis ()/1000;
Claims. The put (iat, now);
//the longest half a year, the unit s
Claims. The put (" exp ", now + 86400 * 30);
//apple's official website url
Claims. The put (" aud ", APPLE_URL);
//client_id (id)
Claims. The put (" sub "APPLICATION_ID);
PKCS8EncodedKeySpec PKCS8EncodedKeySpec=new PKCS8EncodedKeySpec (readKey ());
KeyFactory KeyFactory=KeyFactory. GetInstance (" EC ");
PrivateKey PrivateKey=keyFactory. GeneratePrivate (pkcs8EncodedKeySpec);

Return Jwts. Builder (). SetHeader (header). SetClaims (claims). SignWith (SignatureAlgorithm. ES256, privateKey). The compact ();
} the catch (Exception e) {
Failed to get the apple key logger. The error (" : ", e);
Failed to get the apple key throw new ServiceException (" : ", e);
}
}


/* *
* declassified information
*
* @ param identityToken APP for identityToken
* @ return decryption parameters: failure returns null
*/
Private static String verify (String identityToken) {
Try {
If (identityToken. Split (" \ \ "). The length & lt;=1) {
return null;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related