2 Copyright 2009 senju@users.sourceforge.jp
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
17 package jp.sourceforge.rabbitBTS.interceptors;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
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;
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;
36 public class AuthenticationInterceptor extends HandlerInterceptorAdapter {
39 private AccountService accountService;
44 * @return ページが表示可能ならtrue、そうでないならfalse
47 public boolean preHandle(HttpServletRequest request,
48 HttpServletResponse response, Object handler) throws Exception {
50 final Account acc = this.accountService.fetchAccount();
52 if (handler instanceof IPublicController || acc != null) {
53 // 公開ページの場合、登録してなくても戻り値はtrue
56 // アカウント未登録の場合、登録ページにリダイレクトする。
57 response.sendRedirect("/register/");
58 Sht.log(this).warning(
59 "403(not registerd.) " + request.getRequestURL());
61 } catch (final HasNotValidIdException e) {
62 if (handler instanceof IPublicController) {
63 // 公開ページの場合、ログインしてなくても戻り値はtrue
67 // ログインしていない場合、ログインページにリダイレクトする。
68 response.sendRedirect(Sht.loginUrl(request));
69 Sht.log(this).warning("403 " + request.getRequestURL());
75 * viewでの描画用にアカウント、ログインURL、ログアウトURLをセットする。
78 public void postHandle(HttpServletRequest request,
79 HttpServletResponse response, Object handler, ModelAndView mav)
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));