-
Notifications
You must be signed in to change notification settings - Fork 161
/
CamundaBpmWebappInitializer.java
145 lines (116 loc) · 6.78 KB
/
CamundaBpmWebappInitializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; 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.camunda.bpm.spring.boot.starter.webapp;
import org.camunda.bpm.admin.impl.web.AdminApplication;
import org.camunda.bpm.admin.impl.web.bootstrap.AdminContainerBootstrap;
import org.camunda.bpm.cockpit.impl.web.CockpitApplication;
import org.camunda.bpm.cockpit.impl.web.bootstrap.CockpitContainerBootstrap;
import org.camunda.bpm.engine.rest.filter.CacheControlFilter;
import org.camunda.bpm.engine.rest.filter.EmptyBodyFilter;
import org.camunda.bpm.spring.boot.starter.property.CamundaBpmProperties;
import org.camunda.bpm.spring.boot.starter.webapp.filter.LazyProcessEnginesFilter;
import org.camunda.bpm.spring.boot.starter.webapp.filter.LazySecurityFilter;
import org.camunda.bpm.spring.boot.starter.webapp.filter.SpringBootCsrfPreventionFilter;
import org.camunda.bpm.tasklist.impl.web.TasklistApplication;
import org.camunda.bpm.tasklist.impl.web.bootstrap.TasklistContainerBootstrap;
import org.camunda.bpm.webapp.impl.engine.EngineRestApplication;
import org.camunda.bpm.webapp.impl.security.auth.AuthenticationFilter;
import org.camunda.bpm.webapp.impl.security.filter.headersec.HttpHeaderSecurityFilter;
import org.camunda.bpm.webapp.impl.security.filter.util.HttpSessionMutexListener;
import org.camunda.bpm.welcome.impl.web.WelcomeApplication;
import org.camunda.bpm.welcome.impl.web.bootstrap.WelcomeContainerBootstrap;
import org.glassfish.jersey.servlet.ServletContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.SessionTrackingMode;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Map;
import static java.util.Collections.singletonMap;
import static org.glassfish.jersey.servlet.ServletProperties.JAXRS_APPLICATION_CLASS;
/**
* Inspired by:
* https://groups.google.com/forum/#!msg/camunda-bpm-users/BQHdcLIivzs
* /iNVix8GkhYAJ (Christoph Berg)
*/
public class CamundaBpmWebappInitializer implements ServletContextInitializer {
private static final Logger log = LoggerFactory.getLogger(CamundaBpmWebappInitializer.class);
private static final EnumSet<DispatcherType> DISPATCHER_TYPES = EnumSet.of(DispatcherType.REQUEST);
private ServletContext servletContext;
private final CamundaBpmProperties properties;
public CamundaBpmWebappInitializer(CamundaBpmProperties properties) {
this.properties = properties;
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
this.servletContext = servletContext;
servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
servletContext.addListener(new CockpitContainerBootstrap());
servletContext.addListener(new AdminContainerBootstrap());
servletContext.addListener(new TasklistContainerBootstrap());
servletContext.addListener(new WelcomeContainerBootstrap());
servletContext.addListener(new HttpSessionMutexListener());
registerFilter("Authentication Filter", AuthenticationFilter.class, "/api/*", "/app/*");
registerFilter("Security Filter", LazySecurityFilter.class, singletonMap("configFile", properties.getWebapp().getSecurityConfigFile()), "/api/*", "/app/*");
registerFilter("CsrfPreventionFilter", SpringBootCsrfPreventionFilter.class, properties.getWebapp().getCsrf().getInitParams(),"/api/*", "/app/*");
Map<String, String> headerSecurityProperties = properties.getWebapp()
.getHeaderSecurity()
.getInitParams();
registerFilter("HttpHeaderSecurity", HttpHeaderSecurityFilter.class, headerSecurityProperties,"/api/*", "/app/*");
registerFilter("Engines Filter", LazyProcessEnginesFilter.class, "/api/*", "/app/*");
registerFilter("EmptyBodyFilter", EmptyBodyFilter.class, "/api/*", "/app/*");
registerFilter("CacheControlFilter", CacheControlFilter.class, "/api/*", "/app/*");
registerServlet("Cockpit Api", CockpitApplication.class, "/api/cockpit/*");
registerServlet("Admin Api", AdminApplication.class, "/api/admin/*");
registerServlet("Tasklist Api", TasklistApplication.class, "/api/tasklist/*");
registerServlet("Engine Api", EngineRestApplication.class, "/api/engine/*");
registerServlet("Welcome Api", WelcomeApplication.class, "/api/welcome/*");
}
private FilterRegistration registerFilter(final String filterName, final Class<? extends Filter> filterClass, final String... urlPatterns) {
return registerFilter(filterName, filterClass, null, urlPatterns);
}
private FilterRegistration registerFilter(final String filterName, final Class<? extends Filter> filterClass, final Map<String, String> initParameters,
final String... urlPatterns) {
FilterRegistration filterRegistration = servletContext.getFilterRegistration(filterName);
if (filterRegistration == null) {
filterRegistration = servletContext.addFilter(filterName, filterClass);
filterRegistration.addMappingForUrlPatterns(DISPATCHER_TYPES, true, urlPatterns);
if (initParameters != null) {
filterRegistration.setInitParameters(initParameters);
}
log.debug("Filter {} for URL {} registered.", filterName, urlPatterns);
}
return filterRegistration;
}
private ServletRegistration registerServlet(final String servletName, final Class<?> applicationClass, final String... urlPatterns) {
ServletRegistration servletRegistration = servletContext.getServletRegistration(servletName);
if (servletRegistration == null) {
servletRegistration = servletContext.addServlet(servletName, ServletContainer.class);
servletRegistration.addMapping(urlPatterns);
servletRegistration.setInitParameters(singletonMap(JAXRS_APPLICATION_CLASS, applicationClass.getName()));
log.debug("Servlet {} for URL {} registered.", servletName, urlPatterns);
}
return servletRegistration;
}
}