forked from hapifhir/hapi-fhir
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
40 : Added keycloak configuration #1
Open
rehammuzzamil
wants to merge
15
commits into
master
Choose a base branch
from
40-FHIR-Authentication
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5d04727
40 : Added keycloak configuration
rehammuzzamil 6d5577f
40 : Added keycloak configuration
rehammuzzamil 7ec7479
40 : Added keycloak configuration
rehammuzzamil 9373176
40 : Added keycloak configuration
rehammuzzamil 5046a53
40 : Added keycloak configuration
rehammuzzamil e3b5f8c
40 : Add keycloak configuration
rehammuzzamil 0b88bb6
Set skip to false to resolve duplicate finder plugin exceptions while…
rehammuzzamil 6dccf41
Code cleanup
rehammuzzamil a2f4fb8
Removed redundant application.yml from the autoconfigure package
rehammuzzamil cdff49d
40 : Create a separate security configuration package
rehammuzzamil 0d60ce9
40 : Revert changes on spring boot module
rehammuzzamil 362038d
40 : Add authentication work
rehammuzzamil e15dbd0
40 : Add authentication work
rehammuzzamil bd15971
40 : Code cleanup
rehammuzzamil fa8c094
40 : Code cleanup
rehammuzzamil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>ca.uhn.hapi.fhir</groupId> | ||
<artifactId>hapi-deployable-pom</artifactId> | ||
<version>5.4.0-PRE5-SNAPSHOT</version> | ||
<relativePath>../../hapi-deployable-pom/pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>hapi-fhir-opensrp-security-config</artifactId> | ||
<version>0.0.1-PRE5-SNAPSHOT</version> | ||
|
||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<!-- Compile dependencies --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-autoconfigure</artifactId> | ||
<version>${spring_boot_version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<!-- @ConfigurationProperties annotation processing (metadata for IDEs) --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>log4j-over-slf4j</artifactId> | ||
<scope>test</scope> | ||
<version>1.7.30</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.keycloak</groupId> | ||
<artifactId>keycloak-spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-security</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>${spring_boot_version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.keycloak.bom</groupId> | ||
<artifactId>keycloak-adapter-bom</artifactId> | ||
<version>13.0.0</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.basepom.maven</groupId> | ||
<artifactId>duplicate-finder-maven-plugin</artifactId> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
127 changes: 127 additions & 0 deletions
127
hapi-fhir-opensrp-security-config/src/main/java/autoconfigure/KeycloakSecurityConfig.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,127 @@ | ||
package autoconfigure; | ||
|
||
import org.keycloak.adapters.KeycloakConfigResolver; | ||
import org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver; | ||
import org.keycloak.adapters.springsecurity.KeycloakConfiguration; | ||
import org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider; | ||
import org.keycloak.adapters.springsecurity.client.KeycloakClientRequestFactory; | ||
import org.keycloak.adapters.springsecurity.client.KeycloakRestTemplate; | ||
import org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.config.ConfigurableBeanFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Scope; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.builders.WebSecurity; | ||
import org.springframework.security.core.authority.mapping.SimpleAuthorityMapper; | ||
import org.springframework.security.core.session.SessionRegistryImpl; | ||
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy; | ||
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy; | ||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.CorsConfigurationSource; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.springframework.http.HttpMethod.DELETE; | ||
import static org.springframework.http.HttpMethod.GET; | ||
import static org.springframework.http.HttpMethod.POST; | ||
import static org.springframework.http.HttpMethod.PUT; | ||
|
||
@KeycloakConfiguration | ||
public class KeycloakSecurityConfig extends KeycloakWebSecurityConfigurerAdapter { | ||
|
||
private static final String CORS_ALLOWED_HEADERS = "origin,content-type,accept,x-requested-with,Authorization"; | ||
|
||
private String opensrpAllowedSources=""; | ||
|
||
private long corsMaxAge=60; | ||
|
||
|
||
private static final Logger logger = LoggerFactory.getLogger(KeycloakSecurityConfig.class); | ||
|
||
@Autowired | ||
private KeycloakClientRequestFactory keycloakClientRequestFactory; | ||
|
||
@Autowired | ||
public void configureGlobal(AuthenticationManagerBuilder auth) { | ||
|
||
SimpleAuthorityMapper grantedAuthorityMapper = new SimpleAuthorityMapper(); | ||
grantedAuthorityMapper.setPrefix("ROLE_"); | ||
|
||
KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider(); | ||
keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper()); | ||
auth.authenticationProvider(keycloakAuthenticationProvider); | ||
} | ||
|
||
@Bean | ||
public KeycloakConfigResolver keycloakConfigResolver() { | ||
return new KeycloakSpringBootConfigResolver(); | ||
} | ||
|
||
@Bean | ||
@Override | ||
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() { | ||
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl()); | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
super.configure(http); | ||
logger.error("Inside configure method"); | ||
http.cors() | ||
.and() | ||
.authorizeRequests() | ||
.antMatchers("/").permitAll() | ||
.antMatchers("/home").permitAll() | ||
.mvcMatchers("/logout.do").permitAll() | ||
.antMatchers("/fhir/**") | ||
.authenticated() | ||
.and() | ||
.csrf() | ||
.ignoringAntMatchers("/fhir/**") | ||
.and() | ||
.logout() | ||
.logoutRequestMatcher(new AntPathRequestMatcher("logout.do", "GET")); | ||
|
||
} | ||
|
||
@Override | ||
public void configure(WebSecurity web) throws Exception { | ||
/* @formatter:off */ | ||
web.ignoring().mvcMatchers("/js/**") | ||
.and().ignoring().mvcMatchers("/css/**") | ||
.and().ignoring().mvcMatchers("/images/**") | ||
.and().ignoring().mvcMatchers("/html/**") | ||
.and().ignoring().antMatchers(HttpMethod.OPTIONS, "/**") | ||
.and().ignoring().antMatchers("/home") | ||
.and().ignoring().antMatchers("/*") | ||
// .and().ignoring().antMatchers("/fhir") | ||
.and().ignoring().antMatchers("/fhir/metadata"); | ||
// /* @formatter:on */ | ||
} | ||
|
||
@Bean | ||
public CorsConfigurationSource corsConfigurationSource() { | ||
CorsConfiguration configuration = new CorsConfiguration(); | ||
configuration.setAllowedOrigins(Arrays.asList(opensrpAllowedSources.split(","))); | ||
configuration.setAllowedMethods(Arrays.asList(GET.name(), POST.name(), PUT.name(), DELETE.name())); | ||
configuration.setAllowedHeaders(Arrays.asList(CORS_ALLOWED_HEADERS.split(","))); | ||
configuration.setMaxAge(corsMaxAge); | ||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
source.registerCorsConfiguration("/**", configuration); | ||
return source; | ||
} | ||
|
||
@Bean | ||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) | ||
public KeycloakRestTemplate keycloakRestTemplate() { | ||
return new KeycloakRestTemplate(keycloakClientRequestFactory); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
hapi-fhir-opensrp-security-config/src/main/java/autoconfigure/SecurityAutoConfiguration.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,41 @@ | ||
package autoconfigure; | ||
|
||
/*- | ||
* #%L | ||
* hapi-fhir-spring-boot-autoconfigure | ||
* %% | ||
* Copyright (C) 2014 - 2021 Smile CDR, Inc. | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* 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. | ||
* #L% | ||
*/ | ||
|
||
|
||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for HAPI FHIR. | ||
* | ||
* @author Reham Muzzamil | ||
*/ | ||
@Configuration | ||
@AutoConfigureAfter({KeycloakSecurityConfig.class}) | ||
@EnableWebSecurity | ||
@Import({ KeycloakSecurityConfig.class }) | ||
public class SecurityAutoConfiguration { | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
hapi-fhir-opensrp-security-config/src/main/resources/META-INF/spring.factories
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 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=autoconfigure.SecurityAutoConfiguration |
4 changes: 4 additions & 0 deletions
4
hapi-fhir-opensrp-security-config/src/test/resources/logback-test.xml
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,4 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
<include resource="org/springframework/boot/logging/logback/base.xml"/> | ||
</configuration> |
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 |
---|---|---|
|
@@ -18,4 +18,4 @@ management: | |
enabled: false | ||
logging: | ||
level: | ||
ca.uhn.fhir.jaxrs: debug | ||
ca.uhn.fhir.jaxrs: debug | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be affected if we rename the group id |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this parent causes the artefact to inherit the groupId
ca.uhn.hapi.fhir
. I also think that this module will be published to Sonatype. We would need to own this groupIdca.uhn.hapi.fhir
to publish to it. Therefore, we need to change this toorg.smartregister
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ekigamba I agree on this. Do we maintain the versioning too or should we reset it too?