-
Notifications
You must be signed in to change notification settings - Fork 4
/
AppSecurityConfiguration.groovy
50 lines (44 loc) · 2.35 KB
/
AppSecurityConfiguration.groovy
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
package example.gateway
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Configuration
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.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService
import org.springframework.security.core.userdetails.User
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken
import org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter
import org.springframework.stereotype.Component
@Configuration
@EnableWebSecurity
class AppSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Slf4j
@Component
static class MyUserDetailsService implements AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> {
@Override
UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token) {
new User(token.principal.toString(), '', [])
}
}
@Autowired
private MyUserDetailsService myUserDetailsService
@Override
protected void configure(AuthenticationManagerBuilder auth) {
def provider = new PreAuthenticatedAuthenticationProvider()
provider.preAuthenticatedUserDetailsService = myUserDetailsService
auth.authenticationProvider(provider)
}
@Override
protected void configure(HttpSecurity http) {
def requestHeaderAuthenticationFilter = new RequestHeaderAuthenticationFilter()
requestHeaderAuthenticationFilter.principalRequestHeader = 'x-user'
requestHeaderAuthenticationFilter.exceptionIfHeaderMissing = false
requestHeaderAuthenticationFilter.authenticationManager = authenticationManager()
http.addFilter(requestHeaderAuthenticationFilter)
http.authorizeRequests().anyRequest().permitAll()
}
}