GAEでGoogleアカウントを使用したログイン処理を実装

GAEを使用して、Googleアカウントでのログイン処理を実装した。

サーブレット

public class LoginCheckServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		UserService userService = UserServiceFactory.getUserService();

		String thisURL = request.getRequestURI();
		if (request.getUserPrincipal() != null) {
			response.getWriter().println(
					"<p>Hello, " + request.getUserPrincipal().getName()
							+ "!  You can <a href=\""
							+ userService.createLogoutURL(thisURL)
							+ "\">sign out</a>.</p>");
		} else {
			response.getWriter().println(
					"<p>Please <a href=\""
							+ userService.createLoginURL(thisURL)
							+ "\">sign in</a>.</p>");
		}
	}
}

見てお分かりの通り、Googleのサンプルのまんまです。

②上記サーブレットへリクエストを送信するJSP

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="/javascript/common.js"></script>
<title>Hello, World!!</title>
</head>
<body>
<% out.println("<p>Hello,GAE Apps!!</p>"); %>

<form action="/loginCheckServlet">
<input type="submit"  value="Login">
</form>
</body>
</html>

サブミットボタンを押すと、web.xmlのurl-mappingで紐付けられたサーブレットが呼ばれます。

③サンプルは下記URLから試すことができます

http://wbs-newworld.appspot.com/guest1

私がテスト用に使用しているページなので、余計なJavaScriptがあったり、予告なくサービスが停止される場合もありますが、ご了承ください。