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.config;
15  
16  import javax.servlet.ServletContext;
17  import javax.servlet.ServletException;
18  
19  import org.springframework.web.context.WebApplicationContext;
20  import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
21  import org.springframework.web.filter.DelegatingFilterProxy;
22  import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
23  
24  /**
25   * @author Dave Syer
26   * 
27   */
28  public class ServletInitializer extends AbstractDispatcherServletInitializer {
29  
30  	@Override
31  	protected WebApplicationContext createServletApplicationContext() {
32  		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
33  		context.register(SecurityConfig.class, WebMvcConfig.class);
34  		return context;
35  	}
36  
37  	@Override
38  	protected String[] getServletMappings() {
39  		return new String[] { "/" };
40  	}
41  
42  	@Override
43  	protected WebApplicationContext createRootApplicationContext() {
44  		return null;
45  	}
46  
47  	@Override
48  	public void onStartup(ServletContext servletContext) throws ServletException {
49  		super.onStartup(servletContext);
50  		registerProxyFilter(servletContext, "springSecurityFilterChain");
51  		registerProxyFilter(servletContext, "oauth2ClientContextFilter");
52  	}
53  
54  	private void registerProxyFilter(ServletContext servletContext, String name) {
55  		DelegatingFilterProxy filter = new DelegatingFilterProxy(name);
56  		filter.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");
57  		servletContext.addFilter(name, filter).addMappingForUrlPatterns(null, false, "/*");
58  	}
59  
60  }