2 * Copyright 2005-2011 the original author or authors.
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.
16 package org.wamblee.photos.security;
18 import java.io.IOException;
20 import javax.servlet.Filter;
21 import javax.servlet.FilterChain;
22 import javax.servlet.FilterConfig;
23 import javax.servlet.ServletException;
24 import javax.servlet.ServletRequest;
25 import javax.servlet.ServletResponse;
26 import javax.servlet.http.HttpServletRequest;
28 public class AuthenticationFilter implements Filter {
29 private static final String LOGINPAGE = "loginpage";
30 private static final String REQUIRED_ROLE = "role";
31 private static final String RESOURCES = "resources";
33 private String loginPage;
35 private String resources;
37 public AuthenticationFilter() {
42 public void init(FilterConfig aFilterConfig) throws ServletException {
43 loginPage = aFilterConfig.getInitParameter(LOGINPAGE);
44 if (loginPage == null) {
45 throw new ServletException("No login page defined! Must specify '" +
46 LOGINPAGE + "' filter init parameter.");
48 role = aFilterConfig.getInitParameter(REQUIRED_ROLE);
50 throw new ServletException("No role name defined! Must specify '" +
51 REQUIRED_ROLE + "' filter init parameter.");
53 resources = aFilterConfig.getInitParameter(RESOURCES);
57 public void doFilter(ServletRequest aRequest, ServletResponse aResponse, FilterChain aChain)
58 throws IOException, ServletException {
60 HttpServletRequest request = (HttpServletRequest) aRequest;
61 String fullPath = request.getRequestURI();
62 String contextPath = request.getContextPath();
63 String relpath = null;
64 if (fullPath.startsWith(contextPath)) {
65 relpath = fullPath.substring(contextPath.length());
68 if (request.isUserInRole(role) || (resources != null && relpath != null && relpath.startsWith(resources))) {
69 aChain.doFilter(aRequest, aResponse);
71 request.getSession().invalidate();
72 request.getRequestDispatcher(loginPage).forward(aRequest, aResponse);
77 public void destroy() {