使用Java服务器域限制的Google OAuth使用hdd参数无法正常工作

时间:2014-10-02 11:29:54

标签: google-oauth google-oauth-java-client

我使用JAVA Web Server实现了GOOGLE OAuth。 我无法弄清楚如何通过example@example.com对登录进行域名限制 T https://developers.google.com/accounts/docs/OAuth2Login#hd-param

这是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 个答案:

答案 0 :(得分:0)

您所要做的就是修改网址

return url.setRedirectUri(CALLBACK_URI).setState(stateToken).build()+"&hd=example.com";