-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from kbss-cvut/upd/spring-6
Upd/spring 6
- Loading branch information
Showing
84 changed files
with
446 additions
and
320 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
16 changes: 0 additions & 16 deletions
16
src/main/java/cz/cvut/kbss/termit/config/KeycloakConfiguration.java
This file was deleted.
Oops, something went wrong.
66 changes: 0 additions & 66 deletions
66
src/main/java/cz/cvut/kbss/termit/config/KeycloakSecurityConfig.java
This file was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
src/main/java/cz/cvut/kbss/termit/config/OAuth2SecurityConfig.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,93 @@ | ||
package cz.cvut.kbss.termit.config; | ||
|
||
import cz.cvut.kbss.termit.security.AuthenticationSuccess; | ||
import cz.cvut.kbss.termit.security.HierarchicalRoleBasedAuthorityMapper; | ||
import cz.cvut.kbss.termit.security.SecurityConstants; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.security.authentication.AbstractAuthenticationToken; | ||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.session.SessionRegistryImpl; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy; | ||
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy; | ||
import org.springframework.web.cors.CorsConfigurationSource; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@ConditionalOnProperty(prefix = "termit.security", name = "provider", havingValue = "oidc") | ||
@Configuration | ||
@EnableWebSecurity | ||
@EnableMethodSecurity | ||
public class OAuth2SecurityConfig { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(OAuth2SecurityConfig.class); | ||
|
||
private final AuthenticationSuccess authenticationSuccessHandler; | ||
|
||
private final cz.cvut.kbss.termit.util.Configuration config; | ||
|
||
@Autowired | ||
public OAuth2SecurityConfig(AuthenticationSuccess authenticationSuccessHandler, | ||
cz.cvut.kbss.termit.util.Configuration config) { | ||
this.authenticationSuccessHandler = authenticationSuccessHandler; | ||
this.config = config; | ||
} | ||
|
||
@Bean | ||
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() { | ||
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl()); | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
LOG.debug("Using OAuth2/OIDC security."); | ||
http.oauth2ResourceServer( | ||
(auth) -> auth.jwt((jwt) -> jwt.jwtAuthenticationConverter(grantedAuthoritiesExtractor()))) | ||
.authorizeHttpRequests((auth) -> auth.requestMatchers("/rest/query").permitAll() | ||
.requestMatchers("/**").permitAll()) | ||
.cors((auth) -> auth.configurationSource(corsConfigurationSource())) | ||
.csrf(AbstractHttpConfigurer::disable) | ||
.logout((auth) -> auth.logoutUrl(SecurityConstants.LOGOUT_PATH) | ||
.logoutSuccessHandler(authenticationSuccessHandler)); | ||
return http.build(); | ||
} | ||
|
||
private CorsConfigurationSource corsConfigurationSource() { | ||
return SecurityConfig.createCorsConfiguration(config.getCors()); | ||
} | ||
|
||
private Converter<Jwt, AbstractAuthenticationToken> grantedAuthoritiesExtractor() { | ||
return source -> { | ||
final Collection<SimpleGrantedAuthority> authorities = new GrantedAuthoritiesExtractor().convert(source); | ||
return new JwtAuthenticationToken(source, authorities); | ||
}; | ||
} | ||
|
||
private class GrantedAuthoritiesExtractor implements Converter<Jwt, Collection<SimpleGrantedAuthority>> { | ||
public Collection<SimpleGrantedAuthority> convert(Jwt jwt) { | ||
final List<SimpleGrantedAuthority> allAuths = ( | ||
(Map<String, Collection<?>>) jwt.getClaims().getOrDefault( | ||
OAuth2SecurityConfig.this.config.getSecurity().getRoleClaim(), Collections.emptyMap()) | ||
).getOrDefault("roles", Collections.emptyList()) | ||
.stream() | ||
.map(Object::toString) | ||
.map(SimpleGrantedAuthority::new).toList(); | ||
return new HierarchicalRoleBasedAuthorityMapper().mapAuthorities(allAuths); | ||
} | ||
} | ||
} |
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.