GAE/J上での認証が必要なページの作り方です。
実物はこちら(http://feed.suz-lab.com/)です。
"マイページ"をクリックすると、ログインしてなければGoogleのログインページに遷移し、
ログインが成功したら、該当ページ(/mypage/index.html)に帰ってきます。
ログイン後のページには"ログアウト"があり、それをクリックすると、
一旦、Google側でログアウトして、ログアウト後のページ(/index.html)に遷移します。
下記の情報をもとに、実装した感じです。
▼セキュリティと認証
http://code.google.com/intl/ja/appengine/docs/java/config/webxml.html#Security_and_Authentication
実際のコードは以下のようになっています。
--------【web.xml】--------
...
<security-constraint>
<web-resource-collection>
<web-resource-name>mypage</web-resource-name>
<url-pattern>/mypage/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>
...
--------
http://code.google.com/p/suz-lab-gae/source/browse/trunk/suz-lab-feed/war/WEB-INF/web.xml?r=57
--------【Java】--------
@Page("/other/mypage")
public class IndexPage extends AbstractIndexPage {
@Override
protected void setAttributes(Request request) {
UserService userService = UserServiceFactory.getUserService();
request.setAttribute("logout", userService.createLogoutURL("/index.html"));
}
}
--------
http://code.google.com/p/suz-lab-gae/source/browse/trunk/suz-lab-feed/src/suz/lab/feed/page/other/mypage/IndexPage.java?r=57
--------【Velocity】--------
#set($layout="other/layout.html")
<h2>マイページ</h2>
<a href="$logout">ログアウト</a>
--------
http://code.google.com/p/suz-lab-gae/source/browse/trunk/suz-lab-feed/war/WEB-INF/vm/page/other/mypage/index.html?r=57
UserService系の処理はVelocityToolで使えるようにしたほうがよさそうだなー…
--------
http://www.suz-lab.com

0 コメント:
コメントを投稿