gpt4 book ai didi

java - 使用 google api java 登录

转载 作者:行者123 更新时间:2023-12-01 13:06:57 26 4
gpt4 key购买 nike

iam 使用 openid 使用 google api 登录,我能够通过访问 google (身份验证)通过 openid 重定向网站,但我无法访问用户信息,即我需要将用户电子邮件 ID 保存到我的数据库中,这里是我的 openid servlet 代码:

public class OpenIdServlet extends HttpServlet {

static final long ONE_HOUR = 3600000L;
static final long TWO_HOUR = ONE_HOUR * 2L;
static final String ATTR_MAC = "openid_mac";
static final String ATTR_ALIAS = "openid_alias";
String email_id=null;
String first_name=null;
String last_name=null;

private OpenIdManager manager;

@Override
public void init() throws ServletException {
super.init();
manager = new OpenIdManager();
manager.setRealm("http://aws.lifeboard.in:8080");
manager.setReturnTo("http://aws.lifeboard.in:8080/#!home");
}

public void login(HttpServletRequest request, HttpServletResponse response,String Google)
throws ServletException, IOException {
String op="Google";
manager = new OpenIdManager();
manager.setRealm("http://aws.lifeboard.in:8080/");
manager.setReturnTo("http://aws.lifeboard.in:8080/#!home");
if (op==null) {
// check sign on result from Google or Yahoo:
checkNonce(request.getParameter("openid.response_nonce"));
// get authentication:
byte[] mac_key = (byte[]) request.getSession().getAttribute(ATTR_MAC);
String alias = (String) request.getSession().getAttribute(ATTR_ALIAS);
Authentication authentication = manager.getAuthentication(request, mac_key, alias);
response.setContentType("text/html; charset=UTF-8");
showAuthentication(authentication);
return;
}
if (op.equals("Google") || op.equals("Yahoo")) {
// redirect to Google or Yahoo sign on page:
System.out.println("enterutrerrereererererer");
Endpoint endpoint = manager.lookupEndpoint(op);

System.out.println("endpoint"+endpoint);
Association association = manager.lookupAssociation(endpoint);
request.getSession().setAttribute(ATTR_MAC, association.getRawMacKey());
request.getSession().setAttribute(ATTR_ALIAS, endpoint.getAlias());
String url = manager.getAuthenticationUrl(endpoint, association);
AuthenticationControllerDb authenticationControllerDb=new AuthenticationControllerDb();
String alias = (String) request.getSession().getAttribute(ATTR_ALIAS);
System.out.println("=========================Url"+url);

response.sendRedirect(url);
// Create a state token to prevent request forgery.
// Store it in the session for later validation.
String state = new BigInteger(130, new SecureRandom()).toString(32);
request.getSession().setAttribute("state", state);
// Read index.html into memory, and set the client ID,
// token state, and application name in the HTML before serving it.
return new Scanner(new File("index.html"), "UTF-8")
.useDelimiter("\\A").next()
.replaceAll("[{]{2}\\s*CLIENT_ID\\s*[}]{2}", CLIENT_ID)
.replaceAll("[{]{2}\\s*STATE\\s*[}]{2}", state)
.replaceAll("[{]{2}\\s*APPLICATION_NAME\\s*[}]{2}",
APPLICATION_NAME);



// get authentication:
}
else {
throw new ServletException("Unsupported OP: " + op);
}
}



void showAuthentication( Authentication auth) {
//pw.print("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>Test JOpenID</title></head><body><h1>You have successfully signed on!</h1>");
// pw.print("<p>Identity: " + auth.getIdentity() + "</p>");
// pw.print("<p>Email: " + auth.getEmail() + "</p>");
// pw.print("<p>Full name: " + auth.getFullname() + "</p>");
AuthenticationControllerDb authenticationControllerDb=new AuthenticationControllerDb();
email_id=auth.getEmail();
first_name=auth.getFirstname();
last_name=auth.getLastname();
Map<String,Object> registration=authenticationControllerDb.googleRegistration(email_id,first_name,last_name);
// pw.print("<p>First name: " + auth.getFirstname() + "</p>");
// pw.print("<p>Last name: " + auth.getLastname() + "</p>");
// pw.print("<p>Gender: " + auth.getGender() + "</p>");
// pw.print("<p>Language: " + auth.getLanguage() + "</p>");
// pw.print("</body></html>");
// pw.flush();
}

void checkNonce(String nonce) {
// check response_nonce to prevent replay-attack:
if (nonce==null || nonce.length()<20)
throw new OpenIdException("Verify failed.");
// make sure the time of server is correct:
long nonceTime = getNonceTime(nonce);
long diff = Math.abs(System.currentTimeMillis() - nonceTime);
if (diff > ONE_HOUR)
throw new OpenIdException("Bad nonce time.");
if (isNonceExist(nonce))
throw new OpenIdException("Verify nonce failed.");
storeNonce(nonce, nonceTime + TWO_HOUR);
}

// simulate a database that store all nonce:
private Set<String> nonceDb = new HashSet<String>();

// check if nonce is exist in database:
boolean isNonceExist(String nonce) {
return nonceDb.contains(nonce);
}

// store nonce in database:
void storeNonce(String nonce, long expires) {
nonceDb.add(nonce);
}

long getNonceTime(String nonce) {
try {
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.parse(nonce.substring(0, 19) + "+0000")
.getTime();
}
catch(ParseException e) {
throw new OpenIdException("Bad nonce time.");
}
}
}

最佳答案

Google OpenID2 已弃用。您应该使用 OpenID Connect用于通过 Google 登录。

关于java - 使用 google api java 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23190797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com