2011年4月3日日曜日

Googleアカウントを使う

  1. protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  2.   throws ServletException, IOException {  
  3.  UserService userService = UserServiceFactory.getUserService();  
  4.   
  5.  String thisURL = req.getRequestURI();  
  6.  if(req.getUserPrincipal() != null){  
  7.   resp.getWriter().print("<p>Hello, " +  
  8.    req.getUserPrincipal().getName() +  
  9.    "! You can <a href="" +  
  10.    userService.createLogoutURL(thisURL) +  
  11.    "">sign out</a>.</p>");  
  12.  }else{  
  13.   resp.getWriter().print("<p>Please <a href="" +  
  14.     userService.createLoginURL(thisURL) +  
  15.     "">sign in</a>.</p>");  
  16.  }  
  17. }  
からのweb.xml
  1. <security-constraint>  
  2.  <web-resource-collection>  
  3.   <url-pattern>/admin/*</url-pattern>  
  4.  </web-resource-collection>  
  5.  <auth-constraint>  
  6.   <role-name>admin</role-name>  
  7.  </auth-constraint>  
  8. </security-constraint>  
とすれば/admin/以下は管理者のみしか見ることができない。

ちなみにローカルで実行するとダミーアカウントでの認証になる。

0 件のコメント:

コメントを投稿