Google OAuth, использующий ограничение домена Java Server Использование параметра hd не работает

Я реализовал GOOGLE OAuth с помощью веб-сервера JAVA. Я не могу понять, как настроить ограничение домена при входе с сайта example@example.com T https://developers.google.com/accounts/docs/OAuth2Login

Вот код Java-сервера.

public final class GoogleAuthHelper {

private static final String CLIENT_ID = "KEY";
private static final String CLIENT_SECRET = "Secret key";
/**
 * Callback URI that google will redirect to after successful authentication
 */
private static final String CALLBACK_URI = "http://localhost:8080/OAuth2v1/index.jsp";
// private static final String HD = "mobiquityinc.com";

// start google authentication constants
private static final Iterable<String> SCOPE = Arrays
        .asList("https://www.googleapis.com/auth/userinfo.profile;https://www.googleapis.com/auth/userinfo.email"
                .split(";"));
private static final String USER_INFO_URL = "https://www.googleapis.com/oauth2/v1/userinfo";
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
// end google authentication constants

private String stateToken;

private final GoogleAuthorizationCodeFlow flow;

/**
 * Constructor initializes the Google Authorization Code Flow with CLIENT
 * ID, SECRET, and SCOPE
 */
public GoogleAuthHelper() {

    System.out.println("google auth helper called");
    flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT,
            JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, SCOPE).build();

    generateStateToken();
}

/**
 * Builds a login URL based on client ID, secret, callback URI, and scope
 */
public String buildLoginUrl() {
    System.out.println("building uri called");
    final GoogleAuthorizationCodeRequestUrl url = flow
            .newAuthorizationUrl();

    return url.setRedirectUri(CALLBACK_URI).setState(stateToken).build();
}

/**
 * Generates a secure state token
 */
private void generateStateToken() {
    System.out.println("generated token called");
    SecureRandom sr1 = new SecureRandom();
    // System.out.println(sr1);
    stateToken = "google;" + sr1.nextInt();

}

/**
 * Accessor for state token
 */
public String getStateToken() {
    System.out.println("gettoken called");
    return stateToken;
}

/**
 * Expects an Authentication Code, and makes an authenticated request for
 * the user's profile information
 * 
 * @return JSON formatted user profile information
 * @param authCode
 *            authentication code provided by google
 */
public String getUserInfoJson(final String authCode) throws IOException {
    System.out.println("getuserinfojson called");
    final GoogleTokenResponse response = flow.newTokenRequest(authCode)
            .setRedirectUri(CALLBACK_URI).execute();
    final Credential credential = flow.createAndStoreCredential(response,
            null);
    final HttpRequestFactory requestFactory = HTTP_TRANSPORT
            .createRequestFactory(credential);
    // Make an authenticated request
    final GenericUrl url = new GenericUrl(USER_INFO_URL);
    final HttpRequest request = requestFactory.buildGetRequest(url);
    request.getHeaders().setContentType("application/json");
    final String jsonIdentity = request.execute().parseAsString();

    return jsonIdentity;

}

}

1 ответ

Все, что вам нужно сделать, это изменить URL

return url.setRedirectUri(CALLBACK_URI).setState(stateToken).build()+"&hd=example.com";
Другие вопросы по тегам