SpringBoot Security 15 : Securing WebApp
ok
Whenever an ORG is trying to build an Auth servere there are 2 options
- custom
- product : keycloak (open src) , okta , there are many on cloud as well (aws cognito)
https://www.keycloak.org/ can be used for
- SSO
- Identity Broker & Social Login
- User Federation
- + more
What is a Keycloak : Realm ?
second : delete all authentication classes and user resource classes in config folder , because you dont want to use the existing DB authn mechnism
third : create KeyCloakRoleConverter class
responsibility of generating Access token or JWT token will be with key cloak. token has info of user , email and role details
this class will take out info from access token and convert it into form of simple granted authority, because spring framework can only understand the roles and authorities information when we present them in form of granted authority or simple granted authority. ?????.
public class KeycloakRoleConverter implements Converter<Jwt, Collection<GrantedAuthority>> {
@Override
public Collection<GrantedAuthority> convert(Jwt source) {
... // convert JWT Token to collection of authority
}
}
then configure the KeyCloakRoleConverter in ProjectConfig.
@Bean
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(new KeycloakRoleConverter());
...
http.oauth2ResourceServer(rsc -> rsc.jwt(jwtConfigurer ->
jwtConfigurer.jwtAuthenticationConverter(jwtAuthenticationConverter)));
...
}
change param of method from id to email (from DB)
client app will look forward to get account details by email id
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=${JWK_SET_URI:http://localhost:8180/realms/eazybankdev/protocol/openid-connect/certs}
implement new class https://github.com/eazybytes/spring-security/blob/4.x.x/section_15/springsecsection_15/src/main/java/com/eazybytes/config/KeycloakOpaqueRoleConverter.java
ok
Comments
Post a Comment