且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

测试了 下 openID google 和 yahoo

更新时间:2022-08-13 11:21:46

看了 下 要实现 openID 还是有点麻烦。

 

可是有好心的 哥们 写好了 程序。

 

http://code.google.com/p/jopenid/

 

之间下载 程序 测试 下就明白了。就需要一个 servlet 和 jar 就行了。

 

		// 拼写域名
		String serverURL = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ request.getContextPath();
		OpenIdManager manager = new OpenIdManager();
		manager.setRealm(serverURL);
		manager.setReturnTo(serverURL + "/openid");

		String op = request.getParameter("op");
		if (op == null) {// 如果是null则是返回函数。
			// check sign on result from Google or Yahoo:
			System.out.println("openid.response_nonce:["
					+ request.getParameter("openid.response_nonce"));
			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(response.getWriter(), authentication);
			return;
		}
		if (op.equals("Google") || op.equals("Yahoo")) {
			// redirect to Google or Yahoo sign on page:
			Endpoint endpoint = manager.lookupEndpoint(op);
			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);
			response.sendRedirect(url);
		} else {
			throw new ServletException("Unsupported OP: " + op);
		}

 

if (op.equals("Google") || op.equals("Yahoo")) {

 

就进行页面跳转验证。

 

if (op == null) {// 如果是null则是返回函数。

就是结果返回函数。从google和yahoo返回数据。

 

可以在这里进行数据库插入。