Skip to content

Commit 7409740

Browse files
author
Camunda-ChrisAllen
committed
first commit
1 parent adc4b9e commit 7409740

8 files changed

Lines changed: 435 additions & 0 deletions

File tree

pom.xml

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.camunda.example.oauth2</groupId>
9+
<artifactId>azure-oauth2-cambpm-web</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
12+
<properties>
13+
<!-- ================ CAMUNDA VERSIONS ================ -->
14+
15+
<!-- Community Edition -->
16+
<camunda.version>7.13.0</camunda.version>
17+
<!-- Enterprise Edition -->
18+
<!-- <camunda.version>7.13.0-ee</camunda.version>-->
19+
20+
<camunda.spring-boot.version>7.13.0</camunda.spring-boot.version>
21+
22+
<!-- ============== SPRING BOOT VERSIONS ============== -->
23+
24+
<!-- 7.12 and above -->
25+
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
26+
27+
<maven.compiler.source>1.8</maven.compiler.source>
28+
<maven.compiler.target>1.8</maven.compiler.target>
29+
<version.java>1.8</version.java>
30+
31+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
32+
<failOnMissingWebXml>false</failOnMissingWebXml>
33+
<azure.version>2.2.4</azure.version>
34+
35+
</properties>
36+
37+
<dependencyManagement>
38+
<dependencies>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-dependencies</artifactId>
42+
<version>${spring-boot.version}</version>
43+
<type>pom</type>
44+
<scope>import</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.camunda.bpm</groupId>
48+
<artifactId>camunda-bom</artifactId>
49+
<version>${camunda.version}</version>
50+
<scope>import</scope>
51+
<type>pom</type>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.microsoft.azure</groupId>
55+
<artifactId>azure-spring-boot-bom</artifactId>
56+
<version>${azure.version}</version>
57+
<type>pom</type>
58+
<scope>import</scope>
59+
</dependency>
60+
</dependencies>
61+
</dependencyManagement>
62+
63+
<dependencies>
64+
65+
<!-- ================ CAMUNDA ================ -->
66+
67+
<!-- Community Edition -->
68+
<dependency>
69+
<groupId>org.camunda.bpm.springboot</groupId>
70+
<artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
71+
<version>${camunda.spring-boot.version}</version>
72+
</dependency>
73+
74+
<!-- Enterprise Edition -->
75+
<!-- <dependency>-->
76+
<!-- <groupId>org.camunda.bpm.springboot</groupId>-->
77+
<!-- <artifactId>camunda-bpm-spring-boot-starter-webapp-ee</artifactId>-->
78+
<!-- <version>${camunda.spring-boot.version}-ee</version>-->
79+
<!-- </dependency>-->
80+
81+
<!-- ================ general - data ================ -->
82+
83+
<!-- <dependency>-->
84+
<!-- <groupId>com.h2database</groupId>-->
85+
<!-- <artifactId>h2</artifactId>-->
86+
<!-- </dependency>-->
87+
<dependency>
88+
<groupId>com.sun.xml.bind</groupId>
89+
<artifactId>jaxb-impl</artifactId>
90+
<version>2.2.3</version>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.postgresql</groupId>
94+
<artifactId>postgresql</artifactId>
95+
<version>42.2.12</version>
96+
</dependency>
97+
98+
<!-- ================ spring boot ================ -->
99+
100+
<dependency>
101+
<groupId>org.springframework.boot</groupId>
102+
<artifactId>spring-boot-starter-security</artifactId>
103+
<version>${spring-boot.version}</version>
104+
</dependency>
105+
<dependency>
106+
<groupId>org.springframework.boot</groupId>
107+
<artifactId>spring-boot-starter-data-jpa</artifactId>
108+
</dependency>
109+
110+
<!-- ================ spring security ================ -->
111+
112+
<dependency>
113+
<groupId>org.springframework.security</groupId>
114+
<artifactId>spring-security-oauth2-client</artifactId>
115+
</dependency>
116+
<dependency>
117+
<groupId>org.springframework.security</groupId>
118+
<artifactId>spring-security-oauth2-jose</artifactId>
119+
</dependency>
120+
121+
<!-- ================ microsoft azure ================ -->
122+
123+
<dependency>
124+
<groupId>com.microsoft.azure</groupId>
125+
<artifactId>azure-active-directory-spring-boot-starter</artifactId>
126+
</dependency>
127+
<dependency>
128+
<groupId>com.microsoft.azure</groupId>
129+
<artifactId>azure-spring-boot</artifactId>
130+
<version>${azure.version}</version>
131+
</dependency>
132+
133+
</dependencies>
134+
135+
<build>
136+
<plugins>
137+
<plugin>
138+
<groupId>org.springframework.boot</groupId>
139+
<artifactId>spring-boot-maven-plugin</artifactId>
140+
<version>${spring-boot.version}</version>
141+
<configuration>
142+
<layout>ZIP</layout>
143+
</configuration>
144+
<executions>
145+
<execution>
146+
<goals>
147+
<goal>repackage</goal>
148+
</goals>
149+
</execution>
150+
</executions>
151+
</plugin>
152+
</plugins>
153+
</build>
154+
155+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.camunda.example.oauth2;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class);
11+
}
12+
13+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.camunda.example.oauth2.config;
2+
3+
import com.camunda.example.oauth2.filter.CamundaAuthenticationFilter;
4+
import com.camunda.example.oauth2.filter.WebAppAuthenticationProvider;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.autoconfigure.security.SecurityProperties;
9+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.core.annotation.Order;
13+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
14+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
15+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
16+
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest;
17+
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
18+
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
19+
20+
import java.util.Collections;
21+
22+
@Configuration
23+
@EnableWebSecurity(debug = true)
24+
@Order(SecurityProperties.BASIC_AUTH_ORDER - 15)
25+
public class WebAppSecurityConfig extends WebSecurityConfigurerAdapter {
26+
27+
private final Logger logger = LoggerFactory.getLogger(WebAppSecurityConfig.class.getName());
28+
29+
@Autowired
30+
private OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService;
31+
32+
@Override
33+
protected void configure(HttpSecurity http) throws Exception {
34+
35+
http.authorizeRequests().antMatchers("/app/admin/**", "/app/cockpit/**", "/app/tasklist/**").authenticated()
36+
.and()
37+
.authorizeRequests().antMatchers("/**").permitAll()
38+
.and()
39+
.oauth2Login()
40+
.userInfoEndpoint()
41+
.oidcUserService(oidcUserService);
42+
43+
http.csrf().disable();
44+
}
45+
46+
@Bean
47+
@Order(SecurityProperties.BASIC_AUTH_ORDER - 15)
48+
public FilterRegistrationBean<CamundaAuthenticationFilter> containerBasedAuthenticationFilter() {
49+
50+
logger.info("++++++++ WebAppSecurityConfig.containerBasedAuthenticationFilter()....");
51+
FilterRegistrationBean<CamundaAuthenticationFilter> filterRegistration
52+
= new FilterRegistrationBean<>();
53+
filterRegistration.setFilter(new CamundaAuthenticationFilter());
54+
filterRegistration.setInitParameters(Collections.singletonMap("authentication-provider", WebAppAuthenticationProvider.class.getName()));
55+
filterRegistration.setOrder(101); // make sure the filter is registered after the Spring Security Filter Chain
56+
filterRegistration.addUrlPatterns("/*");
57+
return filterRegistration;
58+
59+
}
60+
61+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.camunda.example.oauth2.filter;
2+
3+
import org.camunda.bpm.webapp.impl.security.auth.ContainerBasedAuthenticationFilter;
4+
import org.camunda.bpm.webapp.impl.util.ServletContextUtil;
5+
6+
import javax.servlet.ServletContext;
7+
import javax.servlet.http.HttpServletRequest;
8+
9+
public class CamundaAuthenticationFilter extends ContainerBasedAuthenticationFilter {
10+
11+
@Override
12+
protected String getRequestUri(HttpServletRequest request) {
13+
String requestURI = request.getRequestURI();
14+
String contextPath = request.getContextPath();
15+
16+
int contextPathLength = contextPath.length();
17+
if (contextPathLength > 0) {
18+
requestURI = requestURI.substring(contextPathLength);
19+
}
20+
21+
ServletContext servletContext = request.getServletContext();
22+
String applicationPath = ServletContextUtil.getAppPath(servletContext);
23+
int applicationPathLength = applicationPath.length();
24+
25+
if (applicationPathLength > 0 && applicationPathLength < requestURI.length()) {
26+
requestURI = requestURI.substring(applicationPathLength);
27+
}
28+
29+
return requestURI;
30+
}
31+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.camunda.example.oauth2.filter;
2+
3+
import org.camunda.bpm.engine.ProcessEngine;
4+
import org.camunda.bpm.engine.rest.security.auth.AuthenticationResult;
5+
import org.camunda.bpm.engine.rest.security.auth.impl.ContainerBasedAuthenticationProvider;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.security.core.Authentication;
9+
import org.springframework.security.core.context.SecurityContextHolder;
10+
11+
import javax.servlet.http.HttpServletRequest;
12+
import java.util.List;
13+
import java.util.stream.Collectors;
14+
15+
public class WebAppAuthenticationProvider extends ContainerBasedAuthenticationProvider {
16+
17+
private final Logger logger = LoggerFactory.getLogger(WebAppAuthenticationProvider.class.getName());
18+
19+
@Override
20+
public AuthenticationResult extractAuthenticatedUser(HttpServletRequest request, ProcessEngine engine) {
21+
22+
logger.info("++ WebAppAuthenticationProvider.extractAuthenticatedUser()....");
23+
24+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
25+
26+
if (authentication == null) {
27+
logger.debug("++ authentication == null...return unsuccessful.");
28+
return AuthenticationResult.unsuccessful();
29+
}
30+
31+
logger.debug("++ authentication IS NOT NULL");
32+
String name = authentication.getName();
33+
if (name == null || name.isEmpty()) {
34+
return AuthenticationResult.unsuccessful();
35+
}
36+
37+
logger.debug("++ name = " + name);
38+
AuthenticationResult authenticationResult = new AuthenticationResult(name, true);
39+
authenticationResult.setGroups(getUserGroups(authentication));
40+
41+
return authenticationResult;
42+
}
43+
44+
private List<String> getUserGroups(Authentication authentication) {
45+
46+
logger.info("++ WebAppAuthenticationProvider.getUserGroups()....");
47+
List<String> groupIds;
48+
49+
groupIds = authentication.getAuthorities().stream()
50+
.map(res -> res.getAuthority())
51+
.map(res -> res.substring(5)) // Strip "ROLE_"
52+
.collect(Collectors.toList());
53+
54+
logger.debug("++ groupIds = " + groupIds.toString());
55+
56+
return groupIds;
57+
58+
}
59+
60+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
window.camWelcomeConf = {
2+
links: [
3+
{
4+
label: 'Login with your Microsoft Credentials',
5+
href: '/oauth2/authorization/azure',
6+
description: 'Link to Azure AD'
7+
}
8+
]
9+
};

0 commit comments

Comments
 (0)