2 * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
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 edu.internet2.middleware.shibboleth.utils;
19 import java.io.IOException;
20 import java.security.Principal;
22 import javax.servlet.Filter;
23 import javax.servlet.FilterChain;
24 import javax.servlet.FilterConfig;
25 import javax.servlet.ServletException;
26 import javax.servlet.ServletRequest;
27 import javax.servlet.ServletResponse;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletRequestWrapper;
30 import javax.servlet.http.HttpServletResponse;
32 import org.apache.log4j.Logger;
33 import org.apache.log4j.MDC;
36 * Simple Servlet Filter that strips realm information from Kerberos authenticated container-managed security
38 * @author Scott Cantor
40 public class KerberosPrincipalFilter implements Filter {
42 private static Logger log = Logger.getLogger(KerberosPrincipalFilter.class.getName());
45 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
47 public void init(FilterConfig config) throws ServletException {
52 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
53 * javax.servlet.FilterChain)
55 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
58 if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
59 MDC.put("serviceId", "[Kerberos Principal Filter]");
60 log.error("Only HTTP(s) requests are supported by the KerberosPrincipalFilter.");
63 HttpServletRequest httpRequest = (HttpServletRequest) request;
65 String name = httpRequest.getRemoteUser();
66 int split = name.indexOf('@');
67 if (split > -1) name = name.substring(0, split);
69 chain.doFilter(new KerberosPrincipalWrapper(httpRequest, new PrincipalImpl(name)), response);
73 * @see javax.servlet.Filter#destroy()
75 public void destroy() {
79 class KerberosPrincipalWrapper extends HttpServletRequestWrapper {
83 KerberosPrincipalWrapper(HttpServletRequest request, Principal principal) {
86 this.principal = principal;
90 * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
92 public String getRemoteUser() {
94 return principal.getName();
98 * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
100 public Principal getUserPrincipal() {
106 class PrincipalImpl implements Principal {
108 private String name = null;
110 PrincipalImpl(String name) {
116 * @see java.security.Principal#getName()
118 public String getName() {
124 * @see java.lang.Object#equals(java.lang.Object)
126 public boolean equals(Object obj) {
128 return name.equals(obj);
132 * @see java.lang.Object#toString()
134 public String toString() {