0
iam使用openid登錄與谷歌API,我能夠通過訪問谷歌(身份驗證)通過openid的網站重定向,但我無法訪問用戶信息,即我nedd用戶電子郵件ID保存到我的數據庫,這裏是我的openid servlet代碼:用谷歌API登錄
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.");
}
}
}
謝謝,現在iam使用oauth2和代碼是這樣的這個 – user3119158
感謝並開始使用OAuth 2 – user3119158