-
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.
Added oauth2 using keycloak for authentication and authorization
- Loading branch information
Showing
7 changed files
with
123 additions
and
0 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
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
31 changes: 31 additions & 0 deletions
31
gateway-server/src/main/java/com/gatewayserver/config/KeycloakRoleConverter.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,31 @@ | ||
package com.gatewayserver.config; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class KeycloakRoleConverter implements Converter<Jwt, Collection<GrantedAuthority>> { | ||
@Override | ||
public Collection<GrantedAuthority> convert(Jwt source) { | ||
Map<String, Set<String>> realmAccess = (Map<String, Set<String>>) source.getClaims().get("realm_access"); | ||
if(realmAccess==null||realmAccess.isEmpty()){ | ||
return new ArrayList<>(); | ||
} | ||
return realmAccess.get("roles") | ||
.stream().map(roleName->new SimpleGrantedAuthority("ROLE_"+roleName)) | ||
.collect(Collectors.toSet()); | ||
|
||
} | ||
|
||
/*@Override | ||
public <U> Converter<Jwt, U> andThen(Converter<? super Collection<GrantedAuthority>, ? extends U> after) { | ||
return Converter.super.andThen(after); | ||
}*/ | ||
} |
6 changes: 6 additions & 0 deletions
6
gateway-server/src/main/java/com/gatewayserver/config/Roles.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,6 @@ | ||
package com.gatewayserver.config; | ||
|
||
public enum Roles { | ||
CLIENT, | ||
ADMIN | ||
} |
41 changes: 41 additions & 0 deletions
41
gateway-server/src/main/java/com/gatewayserver/config/SecurityConfig.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 com.gatewayserver.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.security.authentication.AbstractAuthenticationToken; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.web.server.ServerHttpSecurity; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter; | ||
import org.springframework.security.oauth2.server.resource.authentication.ReactiveJwtAuthenticationConverterAdapter; | ||
import org.springframework.security.web.server.SecurityWebFilterChain; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfig { | ||
private final String[] PATH_TO_AUTHENTICATE={"pdf-editor/users/**","pdf-editor/documents/**","pdf-editor/emails/**"}; | ||
|
||
@Bean | ||
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity httpSecurity){ | ||
httpSecurity. | ||
authorizeExchange(exchange-> | ||
exchange.pathMatchers(HttpMethod.GET).permitAll() | ||
.pathMatchers(PATH_TO_AUTHENTICATE).hasAnyRole(Roles.ADMIN.name(),Roles.CLIENT.name()) | ||
|
||
|
||
) | ||
.oauth2ResourceServer(oauth2Server->oauth2Server.jwt(jwtSpec -> | ||
jwtSpec.jwtAuthenticationConverter(grantedAuth()))); | ||
httpSecurity.csrf(ServerHttpSecurity.CsrfSpec::disable); | ||
return httpSecurity.build(); | ||
} | ||
|
||
private Converter<Jwt,? extends Mono<? extends AbstractAuthenticationToken>> grantedAuth() { | ||
var jwtConv= new JwtAuthenticationConverter(); | ||
jwtConv.setJwtGrantedAuthoritiesConverter(new KeycloakRoleConverter()); | ||
return new ReactiveJwtAuthenticationConverterAdapter(jwtConv); | ||
} | ||
} |
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