forked from OpenConext/OpenConext-pdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSecurityConfig.java
121 lines (102 loc) · 5.08 KB
/
WebSecurityConfig.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package pdp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.security.web.csrf.CsrfFilter;
import pdp.access.BasicAuthenticationProvider;
import pdp.access.PolicyIdpAccessEnforcerFilter;
import pdp.manage.Manage;
import pdp.shibboleth.ShibbolethPreAuthenticatedProcessingFilter;
import pdp.shibboleth.ShibbolethUserDetailService;
import pdp.shibboleth.mock.MockShibbolethFilter;
import pdp.web.CsrfProtectionMatcher;
import pdp.web.CsrfTokenResponseHeaderBindingFilter;
@Configuration
@EnableWebSecurity
@Order(1)
public class WebSecurityConfig {
@Value("${policy.enforcement.point.user.name}")
private String policyEnforcementPointUserName;
@Value("${policy.enforcement.point.user.password}")
private String policyEnforcementPointPassword;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
PreAuthenticatedAuthenticationProvider authenticationProvider = new PreAuthenticatedAuthenticationProvider();
authenticationProvider.setPreAuthenticatedUserDetailsService(new ShibbolethUserDetailService());
auth.authenticationProvider(authenticationProvider);
BasicAuthenticationProvider basicAuthenticationProvider = new BasicAuthenticationProvider(policyEnforcementPointUserName, policyEnforcementPointPassword);
auth.authenticationProvider(basicAuthenticationProvider);
}
@Order(2)
@Configuration
public static class InternalSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Autowired
private Manage manage;
@Autowired
private Environment environment;
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/internal/health", "/internal/info", "/public/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/internal/**")
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and()
.csrf()
.requireCsrfProtectionMatcher(new CsrfProtectionMatcher())
.and()
.addFilterAfter(new CsrfTokenResponseHeaderBindingFilter(), CsrfFilter.class)
.addFilterAfter(
new ShibbolethPreAuthenticatedProcessingFilter(authenticationManagerBean(), manage),
AbstractPreAuthenticatedProcessingFilter.class
)
.authorizeRequests()
.antMatchers("/internal/**").hasAnyRole("PEP", "ADMIN");
if (environment.acceptsProfiles(Profiles.of("no-csrf"))) {
http.csrf().disable();
}
if (environment.acceptsProfiles(Profiles.of("dev", "perf"))) {
//we can't use @Profile, because we need to add it before the real filter
http.addFilterBefore(new MockShibbolethFilter(), ShibbolethPreAuthenticatedProcessingFilter.class);
}
}
}
@Configuration
@Order
public static class ApiSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Autowired
private Manage manage;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf()
.disable()
.addFilterBefore(
new PolicyIdpAccessEnforcerFilter(authenticationManager(), manage),
BasicAuthenticationFilter.class
)
.authorizeRequests()
.antMatchers("/protected/**", "/decide/policy")
.hasAnyRole("PEP", "ADMIN");
}
}
}