久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Apple Java 用戶驗證登錄

Sign in with Apple Java User Verification(使用 Apple Java 用戶驗證登錄)
本文介紹了使用 Apple Java 用戶驗證登錄的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我已經(jīng)實現(xiàn)了新的蘋果功能使用 Apple 登錄"的應(yīng)用程序端,但我無法在我的后端使用授權(quán)碼進行驗證.我的后端是用 java 編寫的,我無法生成 JWT 并與 Apple 服務(wù)器通信.

I've implemented the app side of the new apple feature "Sign in with Apple" but i'm unable to verificate with authorizationCode in my backend. My backend is written in java and i'm unable to generate JWT and communicate with Apple servers.

推薦答案

先去 developer.apple.com ->證書、標識符和配置文件 ->鑰匙.為 Apple 登錄生成密鑰并下載此密鑰.您無法再次下載此密鑰,因此請將其保存在安全的地方,不要與他人共享.此外,您在此處顯示的密鑰 ID 請注意這一點,稍后您將需要它.您還需要團隊 ID.如果你不知道,它會寫在頁面的右上角,比如 YOURNAME - XX0XX00XXX.

First go developer.apple.com -> Certificates, Identifiers & Profiles -> Keys. Generate a key for Apple Sign in and download this key. You can not download this key again so keep it in a safe place and don't share with others. Also your Key ID shown here note this, you'll need this later. You'll also need team id. If you don't know it, it's written top right of the page like YOURNAME - XX0XX00XXX.

您將基本上遵循這些步驟.

You will basicly follow these steps.

1.從您的密鑰生成 JWT

1.Generate JWT from your key

2.使用您的令牌發(fā)送驗證碼

2.Send auth code with your token

3.解碼響應(yīng)

同時使用網(wǎng)絡(luò)和移動設(shè)備的更新

如果您想在網(wǎng)頁上使用 Apple 登錄,則需要執(zhí)行更多步驟.

If you would like to use apple login for web there are few more steps you need to follow.

4.添加新的網(wǎng)絡(luò)標識符

去 developer.apple.com ->證書、標識符和配置文件 ->身份標識.單擊加號按鈕注冊新標識符.選擇服務(wù) ID 并繼續(xù).提供描述和標識符.標識符必須是唯一的,并且與您的捆綁包 ID 不同.(例如,您可以使用 com.your.bundle.id.web).點擊繼續(xù)點擊注冊.然后你需要配置這個服務(wù)ID.選擇服務(wù) ID(它位于搜索圖標附近的右上角)您在下面列出的新創(chuàng)建的服務(wù) ID 單擊它并
啟用使用 Apple 登錄復(fù)選框.然后你需要配置你的域.提供您的域名并返回網(wǎng)址.

go developer.apple.com -> Certificates, Identifiers & Profiles -> Identifiers. Register a new identifier with clicking plus button. Select Service IDs and continue. Provide a description and identifier. Identifier must be unique and different from your bundle id. (for example you can use com.your.bundle.id.web). Click continue click register. Then you need to configure this service id. Select Service IDs (It's placed at top right near search icon) your newly created services id listed below click it and
enable Sign In with Apple tick box. Then you need to configure your domain. provide your domain and return url.

如果您忘記傳遞 valid redirect_url 或嘗試多次使用相同的authorization_code.

Some important points for web you can get invalid_grant error if you forgot to pass a valid redirect_url or try to use same authorization_code more than once.

public class AppleLoginUtil {
    private static String APPLE_AUTH_URL = "https://appleid.apple.com/auth/token";

    private static String KEY_ID = "**********";
    private static String TEAM_ID = "**********";
    private static String CLIENT_ID = "com.your.bundle.id";
    private static String WEB_CLIENT_ID = "com.your.bundle.id.web";
    private static String WEB_REDIRECT_URL = "https://bundle.your.com/";

    private static PrivateKey pKey;

    private static PrivateKey getPrivateKey() throws Exception {
    //read your key
        String path = new ClassPathResource("apple/AuthKey.p8").getFile().getAbsolutePath();

        final PEMParser pemParser = new PEMParser(new FileReader(path));
        final JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
        final PrivateKeyInfo object = (PrivateKeyInfo) pemParser.readObject();
        final PrivateKey pKey = converter.getPrivateKey(object);

        return pKey;
    }

    private static String generateJWT() throws Exception {
        if (pKey == null) {
            pKey = getPrivateKey();
        }

        String token = Jwts.builder()
                .setHeaderParam(JwsHeader.KEY_ID, KEY_ID)
                .setIssuer(TEAM_ID)
                .setAudience("https://appleid.apple.com")
                .setSubject(CLIENT_ID)
                .setExpiration(new Date(System.currentTimeMillis() + (1000 * 60 * 5)))
                .setIssuedAt(new Date(System.currentTimeMillis()))
                .signWith(pKey, SignatureAlgorithm.ES256)
                .compact();

        return token;
    }

    private static String generateWebJWT() throws Exception {
        String token = Jwts.builder()
                .setHeaderParam(JwsHeader.KEY_ID, KEY_ID)
                .setIssuer(TEAM_ID)
                .setAudience("https://appleid.apple.com")
                .setSubject(WEB_CLIENT_ID)
                .setExpiration(new Date(System.currentTimeMillis() + (1000 * 60 * 5)))
                .setIssuedAt(new Date(System.currentTimeMillis()))
                .signWith(getPrivateKey(), SignatureAlgorithm.ES256)
                .compact();

        return token;
    }


    /*
    * Returns unique user id from apple
    * */
    public static String appleAuth(String authorizationCode, boolean forWeb) throws Exception {
        HttpResponse<String> response = Unirest.post(APPLE_AUTH_URL)
                .header("Content-Type", "application/x-www-form-urlencoded")
                .field("client_id", forWeb ? WEB_CLIENT_ID : CLIENT_ID)
                .field("client_secret", forWeb ? generateWebJWT() : generateJWT())
                .field("grant_type", "authorization_code")
                .field("code", authorizationCode)
                .field("redirect_uri", forWeb ? WEB_REDIRECT_URL : null)
                .asString();


        TokenResponse tokenResponse=new Gson().fromJson(response.getBody(),TokenResponse.class);
        String idToken = tokenResponse.getId_token();
        String payload = idToken.split("\.")[1];//0 is header we ignore it for now
        String decoded = new String(Decoders.BASE64.decode(payload));

        IdTokenPayload idTokenPayload = new Gson().fromJson(decoded,IdTokenPayload.class);

       return idTokenPayload.getSub();
    }

}

我使用 BouncyCastle jjwt 來生成令牌.還有用于休息呼叫的 unirest 和 gson.

I've used BouncyCastle jjwt for generating token. And also unirest and gson for rest calls.

 <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on -->
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpkix-jdk15on</artifactId>
        <version>1.63</version>
    </dependency>

<!--JJWT-->
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-api</artifactId>
        <version>0.10.7</version>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-impl</artifactId>
        <version>0.10.7</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-jackson</artifactId>
        <version>0.10.7</version>
        <scope>runtime</scope>
    </dependency>

<!--UNIREST-->
    <dependency>
        <groupId>com.mashape.unirest</groupId>
        <artifactId>unirest-java</artifactId>
        <version>1.4.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpasyncclient</artifactId>
        <version>4.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20140107</version>
    </dependency>

如果你想知道的話,我還解析了對這些類的響應(yīng).

I've also parsed the responses to these classes if you wanted to know.

public class TokenResponse {

    private String access_token;
    private String token_type;
    private Long expires_in;
    private String refresh_token;
    private String id_token;

    ..getters and setters
}


public class IdTokenPayload {

    private String iss;
    private String aud;
    private Long exp;
    private Long iat;
    private String sub;//users unique id
    private String at_hash;
    private Long auth_time;
    private Boolean nonce_supported;
    private Boolean email_verified;
    private String email;

    ..getters and setters
}

這篇關(guān)于使用 Apple Java 用戶驗證登錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 伊人久久在线观看 | 日本一二三区电影 | 中文字幕在线视频免费观看 | 97精品国产97久久久久久免费 | 亚洲精品电影在线观看 | 日本福利视频免费观看 | 欧美激情久久久 | 亚洲视频二区 | 日韩在线不卡 | 91国在线高清视频 | 成人午夜视频在线观看 | 日本不卡一区二区三区在线观看 | 亚洲视频在线一区 | 伊人成人免费视频 | hsck成人网| 国产一级一级国产 | 国产午夜精品一区二区三区 | 天天色综| 亚洲国产精品日韩av不卡在线 | 国产免费拔擦拔擦8x高清 | 久久精品| 中文字幕一区二区三区四区不卡 | 日韩欧美一区二区三区 | 国产美女自拍视频 | 九九热精品免费 | 一级黄色毛片免费 | 日本久久精品 | 精品国产乱码一区二区三区 | 亚洲免费久久久 | 性做久久久久久免费观看欧美 | 69精品久久久久久 | 欧美一级二级视频 | 免费视频成人国产精品网站 | 中文字字幕在线中文乱码范文 | www.色午夜.com| 成人精品鲁一区一区二区 | 免费看91| 91精品国产一区二区三区香蕉 | 久久精品视频91 | 91久久精品国产 | 黄色大片毛片 |