View Javadoc

1   /*
2    * Copyright 2013-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5    * the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11   * specific language governing permissions and limitations under the License.
12   */
13  
14  package org.springframework.security.oauth.examples.sparklr.config;
15  
16  import javax.servlet.ServletContext;
17  import javax.servlet.ServletException;
18  
19  import org.springframework.util.ClassUtils;
20  import org.springframework.web.context.WebApplicationContext;
21  import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
22  import org.springframework.web.filter.DelegatingFilterProxy;
23  import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
24  
25  /**
26   * @author Dave Syer
27   * 
28   */
29  public class ServletInitializer extends AbstractDispatcherServletInitializer {
30  
31  	@Override
32  	protected WebApplicationContext createServletApplicationContext() {
33  		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
34  		context.scan(ClassUtils.getPackageName(getClass()));
35  		return context;
36  	}
37  
38  	@Override
39  	protected String[] getServletMappings() {
40  		return new String[] { "/" };
41  	}
42  
43  	@Override
44  	protected WebApplicationContext createRootApplicationContext() {
45  		return null;
46  	}
47  	
48  	@Override
49  	public void onStartup(ServletContext servletContext) throws ServletException {
50  		super.onStartup(servletContext);
51  		DelegatingFilterProxy filter = new DelegatingFilterProxy("springSecurityFilterChain");
52  		filter.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");
53  		servletContext.addFilter("springSecurityFilterChain", filter).addMappingForUrlPatterns(null, false, "/*");
54  	}
55  	
56  }