1 package org.apache.archiva.redback.integration.filter.authorization;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.redback.authorization.AuthorizationException;
23 import org.apache.archiva.redback.system.SecuritySession;
24 import org.apache.archiva.redback.system.SecuritySystem;
25 import org.codehaus.plexus.util.StringUtils;
26 import org.apache.archiva.redback.integration.filter.SpringServletFilter;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import javax.servlet.FilterChain;
31 import javax.servlet.FilterConfig;
32 import javax.servlet.ServletException;
33 import javax.servlet.ServletRequest;
34 import javax.servlet.ServletResponse;
35 import javax.servlet.http.HttpServletResponse;
36 import java.io.IOException;
39 * SimpleAuthorizationFilter
41 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
44 public class SimpleAuthorizationFilter
45 extends SpringServletFilter
48 private Logger logger = LoggerFactory.getLogger( getClass() );
50 private String permission;
52 private String resource;
54 private String accessDeniedLocation;
56 public void init( FilterConfig filterConfig )
57 throws ServletException
59 super.init( filterConfig );
61 permission = filterConfig.getInitParameter( "permission" );
62 resource = filterConfig.getInitParameter( "resource" );
63 accessDeniedLocation = filterConfig.getInitParameter( "accessDeniedLocation" );
65 if ( StringUtils.isEmpty( accessDeniedLocation ) )
67 throw new ServletException(
68 "Missing parameter 'accessDeniedLocation' from " + SimpleAuthorizationFilter.class.getName()
69 + " configuration." );
73 public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
74 throws IOException, ServletException
76 SecuritySession securitySession = getApplicationContext().getBean( "securitySession", SecuritySession.class );
78 if ( securitySession == null )
80 logger.warn( "Security Session is null." );
84 SecuritySystem securitySystem = getApplicationContext().getBean( "securitySystem", SecuritySystem.class );
86 boolean isAuthorized = false;
90 if ( StringUtils.isEmpty( resource ) )
92 isAuthorized = securitySystem.isAuthorized( securitySession, permission );
96 isAuthorized = securitySystem.isAuthorized( securitySession, permission, resource );
100 chain.doFilter( request, response );
104 accessDenied( response );
107 catch ( AuthorizationException e )
109 accessDenied( response );
113 protected void accessDenied( ServletResponse response )
116 String newlocation = accessDeniedLocation;
118 if ( newlocation.indexOf( '?' ) == ( -1 ) )
126 newlocation += "resource=" + resource;
128 ( (HttpServletResponse) response ).sendRedirect( newlocation );