Initialization of authorization service from the code is now working.
[photos] / src / main / java / org / wamblee / photos / security / AuthenticationFilter.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
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 package org.wamblee.photos.security;
17
18 import java.io.IOException;
19
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;
27
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";
32
33     private String loginPage;
34     private String role;
35     private String resources;
36
37     public AuthenticationFilter() {
38         // Empty.
39     }
40
41     @Override
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.");
47         }
48         role = aFilterConfig.getInitParameter(REQUIRED_ROLE);
49         if (role == null) {
50             throw new ServletException("No role name defined! Must specify '" +
51                     REQUIRED_ROLE + "' filter init parameter.");
52         }
53         resources = aFilterConfig.getInitParameter(RESOURCES);
54     }
55
56     @Override
57     public void doFilter(ServletRequest aRequest, ServletResponse aResponse, FilterChain aChain)
58             throws IOException, ServletException {
59
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());
66         }
67
68         if (request.isUserInRole(role) || (resources != null && relpath != null && relpath.startsWith(resources))) {
69             aChain.doFilter(aRequest, aResponse);
70         } else {
71             request.getSession().invalidate();
72             request.getRequestDispatcher(loginPage).forward(aRequest, aResponse);
73         }
74     }
75
76     @Override
77     public void destroy() {
78         // Empty.
79     }
80 }