OSDN Git Service

1b2fc35178e1e8a3de01652f18ff242ae925281e
[rabbit-bts/RabbitBTS.git] / src / jp / sourceforge / rabbitBTS / interceptors / AuthenticationInterceptor.java
1 /*
2    Copyright 2009 senju@users.sourceforge.jp
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15  */
16
17 package jp.sourceforge.rabbitBTS.interceptors;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import jp.sourceforge.rabbitBTS.Sht;
23 import jp.sourceforge.rabbitBTS.controllers.IPublicController;
24 import jp.sourceforge.rabbitBTS.exceptions.HasNotValidIdException;
25 import jp.sourceforge.rabbitBTS.models.Account;
26 import jp.sourceforge.rabbitBTS.services.AccountService;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.web.servlet.ModelAndView;
31 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
32
33 /**
34  * 認証チェック用インターセプタ
35  */
36 public class AuthenticationInterceptor extends HandlerInterceptorAdapter {
37
38         @Autowired
39         private AccountService accountService;
40
41         /**
42          * 認証チェックを行う。
43          * 
44          * @return ページが表示可能ならtrue、そうでないならfalse
45          */
46         @Override
47         public boolean preHandle(HttpServletRequest request,
48                         HttpServletResponse response, Object handler) throws Exception {
49                 try {
50                         final Account acc = this.accountService.fetchAccount();
51
52                         if (handler instanceof IPublicController || acc != null) {
53                                 // 公開ページの場合、登録してなくても戻り値はtrue
54                                 return true;
55                         }
56                         // アカウント未登録の場合、登録ページにリダイレクトする。
57                         response.sendRedirect("/register/");
58                         Sht.log(this).warning(
59                                         "403(not registerd.) " + request.getRequestURL());
60                         return false;
61                 } catch (final HasNotValidIdException e) {
62                         if (handler instanceof IPublicController) {
63                                 // 公開ページの場合、ログインしてなくても戻り値はtrue
64                                 return true;
65                         }
66
67                         // ログインしていない場合、ログインページにリダイレクトする。
68                         response.sendRedirect(Sht.loginUrl(request));
69                         Sht.log(this).warning("403 " + request.getRequestURL());
70                         return false;
71                 }
72         }
73
74         /**
75          * viewでの描画用にアカウント、ログインURL、ログアウトURLをセットする。
76          */
77         @Override
78         public void postHandle(HttpServletRequest request,
79                         HttpServletResponse response, Object handler, ModelAndView mav)
80                         throws Exception {
81                 if (mav != null) {
82                         // リダイレクトする場合は不要
83                         if (!StringUtils.startsWith(mav.getViewName(), "redirect:")) {
84                                 mav.addObject("acc", this.accountService.getCurrentAccount());
85                                 mav.addObject("loginurl", Sht.loginUrl(request));
86                                 mav.addObject("logouturl", Sht.logoutUrl(request));
87                         }
88                 }
89         }
90
91 }