forked from blcham/record-manager
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #5 from akaene/main
[23ava-distribution#6] Support OAuth2
- Loading branch information
Showing
24 changed files
with
476 additions
and
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
target | ||
node_modules | ||
build | ||
logs | ||
**/generated-sources | ||
**/npm-debug.log | ||
.DS_store | ||
|
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
86 changes: 86 additions & 0 deletions
86
src/main/java/cz/cvut/kbss/study/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,86 @@ | ||
package cz.cvut.kbss.study.config; | ||
|
||
import cz.cvut.kbss.study.security.AuthenticationSuccess; | ||
import cz.cvut.kbss.study.security.SecurityConstants; | ||
import cz.cvut.kbss.study.service.ConfigReader; | ||
import cz.cvut.kbss.study.util.oidc.OidcGrantedAuthoritiesExtractor; | ||
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.http.HttpStatus; | ||
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.HttpStatusEntryPoint; | ||
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.HashSet; | ||
import java.util.Set; | ||
|
||
@ConditionalOnProperty(prefix = "security", name = "provider", havingValue = "oidc") | ||
@Configuration | ||
@EnableWebSecurity | ||
@EnableMethodSecurity | ||
public class OAuth2SecurityConfig { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(OAuth2SecurityConfig.class); | ||
|
||
private final AuthenticationSuccess authenticationSuccess; | ||
|
||
private final ConfigReader config; | ||
|
||
@Autowired | ||
public OAuth2SecurityConfig(AuthenticationSuccess authenticationSuccess, ConfigReader config) { | ||
this.authenticationSuccess = authenticationSuccess; | ||
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.anyRequest().permitAll()) | ||
.exceptionHandling(ehc -> ehc.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))) | ||
.cors((auth) -> auth.configurationSource(corsConfigurationSource())) | ||
.csrf(AbstractHttpConfigurer::disable) | ||
.logout((auth) -> auth.logoutUrl(SecurityConstants.LOGOUT_URI) | ||
.logoutSuccessHandler(authenticationSuccess)); | ||
return http.build(); | ||
} | ||
|
||
private CorsConfigurationSource corsConfigurationSource() { | ||
return SecurityConfig.createCorsConfiguration(config); | ||
} | ||
|
||
private Converter<Jwt, AbstractAuthenticationToken> grantedAuthoritiesExtractor() { | ||
return source -> { | ||
final Collection<SimpleGrantedAuthority> extractedRoles = | ||
new OidcGrantedAuthoritiesExtractor(config).convert(source); | ||
assert extractedRoles != null; | ||
final Set<SimpleGrantedAuthority> authorities = new HashSet<>(extractedRoles); | ||
// Add default role if it is not present | ||
authorities.add(new SimpleGrantedAuthority(SecurityConstants.ROLE_USER)); | ||
return new JwtAuthenticationToken(source, authorities); | ||
}; | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
src/main/java/cz/cvut/kbss/study/rest/OidcUserController.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,34 @@ | ||
package cz.cvut.kbss.study.rest; | ||
|
||
import cz.cvut.kbss.study.model.User; | ||
import cz.cvut.kbss.study.security.SecurityConstants; | ||
import cz.cvut.kbss.study.service.UserService; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* API for getting basic user info. | ||
* <p> | ||
* Enabled when OIDC security is used. | ||
*/ | ||
@ConditionalOnProperty(prefix = "security", name = "provider", havingValue = "oidc") | ||
@RestController | ||
@RequestMapping("/users") | ||
public class OidcUserController extends BaseController { | ||
|
||
private final UserService userService; | ||
|
||
public OidcUserController(UserService userService) { | ||
this.userService = userService; | ||
} | ||
|
||
@PreAuthorize("hasRole('" + SecurityConstants.ROLE_USER + "')") | ||
@GetMapping(value = "/current", produces = MediaType.APPLICATION_JSON_VALUE) | ||
public User getCurrent() { | ||
return userService.getCurrentUser(); | ||
} | ||
} |
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
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.