1
2
3
4
5
6
7
8
9
10
11
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
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 }