forked from camunda/camunda-bpm-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(run): add spring security oauth2 integration (camunda#4481)
related to camunda#4452
- Loading branch information
1 parent
e0b2c19
commit b53ad55
Showing
13 changed files
with
213 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...bpm/spring/boot/starter/security/oauth2/CamundaSpringSecurityOAuth2AutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* 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.security.oauth2; | ||
|
||
import jakarta.servlet.DispatcherType; | ||
import jakarta.servlet.Filter; | ||
import org.camunda.bpm.engine.rest.security.auth.ProcessEngineAuthenticationFilter; | ||
import org.camunda.bpm.engine.spring.SpringProcessEngineServicesConfiguration; | ||
import org.camunda.bpm.spring.boot.starter.CamundaBpmAutoConfiguration; | ||
import org.camunda.bpm.spring.boot.starter.property.CamundaBpmProperties; | ||
import org.camunda.bpm.spring.boot.starter.property.WebappProperty; | ||
import org.camunda.bpm.spring.boot.starter.security.oauth2.impl.OAuth2AuthenticationProvider; | ||
import org.camunda.bpm.webapp.impl.security.auth.ContainerBasedAuthenticationFilter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.AutoConfigureOrder; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.security.SecurityProperties; | ||
import org.springframework.boot.autoconfigure.security.oauth2.client.ClientsConfiguredCondition; | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.security.config.Customizer; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
import java.util.Map; | ||
|
||
@AutoConfigureOrder(CamundaSpringSecurityOAuth2AutoConfiguration.CAMUNDA_OAUTH2_ORDER) | ||
@AutoConfigureAfter({ CamundaBpmAutoConfiguration.class, SpringProcessEngineServicesConfiguration.class }) | ||
@ConditionalOnBean(CamundaBpmProperties.class) | ||
@Conditional(ClientsConfiguredCondition.class) | ||
public class CamundaSpringSecurityOAuth2AutoConfiguration { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(CamundaSpringSecurityOAuth2AutoConfiguration.class); | ||
public static final int CAMUNDA_OAUTH2_ORDER = Ordered.HIGHEST_PRECEDENCE + 100; | ||
private final String webappPath; | ||
|
||
public CamundaSpringSecurityOAuth2AutoConfiguration(CamundaBpmProperties properties) { | ||
WebappProperty webapp = properties.getWebapp(); | ||
this.webappPath = webapp.getApplicationPath(); | ||
} | ||
|
||
@Bean | ||
public FilterRegistrationBean<?> webappAuthenticationFilter() { | ||
FilterRegistrationBean<Filter> filterRegistration = new FilterRegistrationBean<>(); | ||
filterRegistration.setName("Container Based Authentication Filter"); | ||
filterRegistration.setFilter(new ContainerBasedAuthenticationFilter()); | ||
filterRegistration.setInitParameters(Map.of( | ||
ProcessEngineAuthenticationFilter.AUTHENTICATION_PROVIDER_PARAM, OAuth2AuthenticationProvider.class.getName())); | ||
// make sure the filter is registered after the Spring Security Filter Chain | ||
filterRegistration.setOrder(SecurityProperties.DEFAULT_FILTER_ORDER + 1); | ||
filterRegistration.addUrlPatterns(webappPath + "/app/*", webappPath + "/api/*"); | ||
filterRegistration.setDispatcherTypes(DispatcherType.REQUEST); | ||
return filterRegistration; | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
logger.info("Enabling Camunda Spring Security oauth2 integration"); | ||
|
||
http.authorizeHttpRequests(c -> c | ||
.requestMatchers(webappPath + "/app/**").authenticated() | ||
.requestMatchers(webappPath + "/api/**").authenticated() | ||
.anyRequest().permitAll() | ||
) | ||
.oauth2Login(Customizer.withDefaults()) | ||
.oidcLogout(Customizer.withDefaults()) | ||
.oauth2Client(Customizer.withDefaults()) | ||
.csrf(AbstractHttpConfigurer::disable); | ||
|
||
return http.build(); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...g/camunda/bpm/spring/boot/starter/security/oauth2/impl/ClientsNotConfiguredCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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.security.oauth2.impl; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome; | ||
import org.springframework.boot.autoconfigure.security.oauth2.client.ClientsConfiguredCondition; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
|
||
/** | ||
* Condition that matches if no {@code spring.security.oauth2.client.registration} properties are defined | ||
* by inverting the outcome of {@link ClientsConfiguredCondition}. | ||
*/ | ||
public class ClientsNotConfiguredCondition extends ClientsConfiguredCondition { | ||
@Override | ||
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { | ||
var matchOutcome = super.getMatchOutcome(context, metadata); | ||
return new ConditionOutcome(!matchOutcome.isMatch(), matchOutcome.getConditionMessage()); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...rg/camunda/bpm/spring/boot/starter/security/oauth2/impl/OAuth2AuthenticationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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.security.oauth2.impl; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.camunda.bpm.engine.ProcessEngine; | ||
import org.camunda.bpm.engine.rest.security.auth.AuthenticationResult; | ||
import org.camunda.bpm.engine.rest.security.auth.impl.ContainerBasedAuthenticationProvider; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
|
||
public class OAuth2AuthenticationProvider extends ContainerBasedAuthenticationProvider { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(OAuth2AuthenticationProvider.class); | ||
|
||
@Override | ||
public AuthenticationResult extractAuthenticatedUser(HttpServletRequest request, ProcessEngine engine) { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
|
||
if (authentication == null) { | ||
logger.debug("Authentication is null"); | ||
return AuthenticationResult.unsuccessful(); | ||
} | ||
|
||
if (!(authentication instanceof OAuth2AuthenticationToken)) { | ||
logger.debug("Authentication is not OAuth2, it is {}", authentication.getClass()); | ||
return AuthenticationResult.unsuccessful(); | ||
} | ||
var oauth2 = (OAuth2AuthenticationToken) authentication; | ||
String camundaUserId = oauth2.getName(); | ||
if (camundaUserId == null || camundaUserId.isEmpty()) { | ||
logger.debug("UserId is empty"); | ||
return AuthenticationResult.unsuccessful(); | ||
} | ||
|
||
logger.debug("Authenticated user '{}'", camundaUserId); | ||
return AuthenticationResult.successful(camundaUserId); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
org.camunda.bpm.spring.boot.starter.security.oauth2.CamundaBpmSpringSecurityDisableAutoConfiguration | ||
org.camunda.bpm.spring.boot.starter.security.oauth2.CamundaSpringSecurityOAuth2AutoConfiguration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 0 additions & 43 deletions
43
...-security/src/test/java/my/own/custom/spring/boot/project/SamplePermitAllApplication.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.