-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathOpenIDConnectSecurityContext.java
84 lines (70 loc) · 3.06 KB
/
OpenIDConnectSecurityContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package fr.insee.pogues.config.auth.security;
import static org.springframework.security.config.Customizer.withDefaults;
import java.util.Arrays;
import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
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.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import fr.insee.pogues.config.auth.UserProvider;
import fr.insee.pogues.config.auth.user.User;
@Configuration
@EnableWebSecurity
@ConditionalOnProperty(name = "fr.insee.pogues.authentication", havingValue = "OIDC")
public class OpenIDConnectSecurityContext extends WebSecurityConfigurerAdapter {
static final Logger logger = LogManager.getLogger(OpenIDConnectSecurityContext.class);
@Value("${fr.insee.pogues.force.ssl}")
boolean requireSSL;
@Value("${jwt.stamp-claim}")
private String stampClaim;
@Value("${jwt.username-claim}")
private String nameClaim;
@Value("${fr.insee.pogues.cors.allowedOrigin}")
private Optional<String> allowedOrigin;
@Override
protected void configure(HttpSecurity http) throws Exception {
//TODO : variabiliser path /api...
http.sessionManagement().disable();
http.cors(withDefaults())
.authorizeRequests()
.antMatchers("/api/init", "/api/healthcheck").permitAll()
.antMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll()
.antMatchers("/api/persistence/questionnaire/json-lunatic/**").permitAll()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt();
if (requireSSL)
http.antMatcher("/**").requiresChannel().anyRequest().requiresSecure();
}
@Bean
public UserProvider getUserProvider() {
return auth -> {
final Jwt jwt = (Jwt) auth.getPrincipal();
return new User(jwt.getClaimAsString(stampClaim), jwt.getClaimAsString(nameClaim));
};
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList(allowedOrigin.get()));
configuration.setAllowedMethods(Arrays.asList("GET","POST","PUT","DELETE"));
configuration.setAllowedHeaders(Arrays.asList("*"));
configuration.addExposedHeader("Content-Disposition");
UrlBasedCorsConfigurationSource source = new
UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}