| /** |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.cxf.dosgi.samples.security; |
| |
| import java.io.IOException; |
| |
| import javax.servlet.Filter; |
| import javax.servlet.FilterChain; |
| import javax.servlet.FilterConfig; |
| import javax.servlet.ServletException; |
| import javax.servlet.ServletRequest; |
| import javax.servlet.ServletResponse; |
| import javax.servlet.http.HttpServletRequest; |
| import javax.servlet.http.HttpServletResponse; |
| |
| import org.osgi.service.component.annotations.Component; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * A filter that requires a query string of "secure" to invoke the protected resource. Pax-Web whiteboard (if |
| * deployed) will attempt to apply this filter to servlets by name or URL, and will complain if neither |
| * servletName or urlPatterns are specified. The felix http service whiteboard may do something similar. |
| */ |
| @Component // |
| (// |
| service = javax.servlet.Filter.class, |
| property = // |
| { |
| "org.apache.cxf.httpservice.filter=true", "servletNames=none" |
| }// |
| ) |
| public class SampleSecurityFilter implements Filter { |
| |
| private static final Logger LOG = LoggerFactory.getLogger(SampleSecurityFilter.class); |
| |
| public void destroy() { |
| LOG.info("destroy()"); |
| } |
| |
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| throws IOException, ServletException { |
| if ("secure".equals(((HttpServletRequest)request).getQueryString())) { |
| LOG.info("Access granted"); |
| chain.doFilter(request, response); |
| } else { |
| LOG.warn("Access denied"); |
| ((HttpServletResponse)response).sendError(HttpServletResponse.SC_FORBIDDEN); |
| } |
| } |
| |
| public void init(FilterConfig config) throws ServletException { |
| LOG.info("init()"); |
| } |
| } |