-
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
base: master
Are you sure you want to change the base?
Changes from 13 commits
5d04727
6d5577f
7ec7479
9373176
5046a53
e3b5f8c
0b88bb6
6dccf41
a2f4fb8
cdff49d
0d60ce9
362038d
e15dbd0
bd15971
fa8c094
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<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>5.4.0-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> | ||
|
||
<!-- Optional dependencies --> | ||
<dependency> | ||
<groupId>ca.uhn.hapi.fhir</groupId> | ||
<artifactId>hapi-fhir-base</artifactId> | ||
<version>${project.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>ca.uhn.hapi.fhir</groupId> | ||
<artifactId>hapi-fhir-server</artifactId> | ||
<version>${project.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>ca.uhn.hapi.fhir</groupId> | ||
<artifactId>hapi-fhir-jpaserver-base</artifactId> | ||
<version>${project.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>ca.uhn.hapi.fhir</groupId> | ||
<artifactId>hapi-fhir-jaxrsserver-base</artifactId> | ||
<version>${project.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>ca.uhn.hapi.fhir</groupId> | ||
<artifactId>hapi-fhir-client</artifactId> | ||
<version>${project.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>ca.uhn.hapi.fhir</groupId> | ||
<artifactId>hapi-fhir-client-okhttp</artifactId> | ||
<version>${project.version}</version> | ||
<optional>true</optional> | ||
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. Since our approach to Keycloak configuration is not dependent on Hapi FHIR modules, I think that we don't need these We can also create a new repository for this module. I believe that this is what we follow currently. We ended up splitting the previous OpenSRP server repository into separate and smaller modules each in it's own repository 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. Yes, I will be removing these unused dependencies of hapi fhir 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. Agreed. 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. @ekigamba does opensrp-fhir-authentication sound right for the new module? 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. @dubdabasoduba Oh! Yea, I am not sure why the fhir work/modules don't have the client/server for each module. But this could also be 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. @ekigamba thanks. Let me follow up on the repo creation |
||
</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>ca.uhn.hapi.fhir</groupId> | ||
<artifactId>hapi-fhir-validation-resources-dstu2</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>ca.uhn.hapi.fhir</groupId> | ||
<artifactId>hapi-fhir-validation-resources-dstu3</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
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. These two are not required 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. Removed @ekigamba |
||
|
||
<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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
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() | ||
// .antMatchers("/fhir").permitAll() | ||
.mvcMatchers("/logout.do").permitAll() | ||
.antMatchers("/fhir/rest/**") | ||
.authenticated() | ||
.and() | ||
.csrf() | ||
.ignoringAntMatchers("/fhir/rest/**", "/fhir/**") | ||
.and() | ||
.logout() | ||
.logoutRequestMatcher(new AntPathRequestMatcher("logout.do", "GET")); | ||
// .logoutSuccessUrl("/loggedout"); | ||
|
||
} | ||
|
||
@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/rest/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); | ||
} | ||
|
||
} |
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 { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=autoconfigure.SecurityAutoConfiguration |
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> |
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 |
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?