|
15 | 15 | */ |
16 | 16 | package org.springframework.security.kerberos.docs; |
17 | 17 |
|
| 18 | +import org.springframework.beans.factory.annotation.Value; |
18 | 19 | import org.springframework.context.annotation.Bean; |
19 | 20 | import org.springframework.context.annotation.Configuration; |
20 | | -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 21 | +import org.springframework.core.io.FileSystemResource; |
| 22 | +import org.springframework.security.authentication.AuthenticationManager; |
| 23 | +import org.springframework.security.authentication.ProviderManager; |
21 | 24 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
22 | | -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
23 | | -import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; |
| 25 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
24 | 26 | import org.springframework.security.kerberos.authentication.KerberosAuthenticationProvider; |
| 27 | +import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider; |
25 | 28 | import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosClient; |
| 29 | +import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator; |
| 30 | +import org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter; |
| 31 | +import org.springframework.security.kerberos.web.authentication.SpnegoEntryPoint; |
| 32 | +import org.springframework.security.web.SecurityFilterChain; |
| 33 | +import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; |
26 | 34 |
|
27 | 35 | //tag::snippetA[] |
28 | 36 | @Configuration |
29 | | -@EnableWebMvcSecurity |
30 | | -public class AuthProviderConfig extends WebSecurityConfigurerAdapter { |
| 37 | +@EnableWebSecurity |
| 38 | +public class WebSecurityConfig { |
31 | 39 |
|
32 | | - @Override |
33 | | - protected void configure(HttpSecurity http) throws Exception { |
34 | | - http |
35 | | - .authorizeRequests() |
36 | | - .antMatchers("/", "/home").permitAll() |
37 | | - .anyRequest().authenticated() |
38 | | - .and() |
39 | | - .formLogin() |
40 | | - .loginPage("/login").permitAll() |
41 | | - .and() |
42 | | - .logout() |
43 | | - .permitAll(); |
44 | | - } |
| 40 | + @Value("${app.service-principal}") |
| 41 | + private String servicePrincipal; |
45 | 42 |
|
46 | | - @Override |
47 | | - protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
48 | | - auth |
49 | | - .authenticationProvider(kerberosAuthenticationProvider()); |
50 | | - } |
| 43 | + @Value("${app.keytab-location}") |
| 44 | + private String keytabLocation; |
51 | 45 |
|
52 | | - @Bean |
53 | | - public KerberosAuthenticationProvider kerberosAuthenticationProvider() { |
54 | | - KerberosAuthenticationProvider provider = |
55 | | - new KerberosAuthenticationProvider(); |
56 | | - SunJaasKerberosClient client = new SunJaasKerberosClient(); |
57 | | - client.setDebug(true); |
58 | | - provider.setKerberosClient(client); |
59 | | - provider.setUserDetailsService(dummyUserDetailsService()); |
60 | | - return provider; |
61 | | - } |
| 46 | + @Bean |
| 47 | + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 48 | + KerberosAuthenticationProvider kerberosAuthenticationProvider = kerberosAuthenticationProvider(); |
| 49 | + KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider = kerberosServiceAuthenticationProvider(); |
| 50 | + ProviderManager providerManager = new ProviderManager(kerberosAuthenticationProvider, |
| 51 | + kerberosServiceAuthenticationProvider); |
62 | 52 |
|
63 | | - @Bean |
64 | | - public DummyUserDetailsService dummyUserDetailsService() { |
65 | | - return new DummyUserDetailsService(); |
66 | | - } |
| 53 | + http |
| 54 | + .authorizeHttpRequests((authz) -> authz |
| 55 | + .requestMatchers("/", "/home").permitAll() |
| 56 | + .anyRequest().authenticated() |
| 57 | + ) |
| 58 | + .exceptionHandling() |
| 59 | + .authenticationEntryPoint(spnegoEntryPoint()) |
| 60 | + .and() |
| 61 | + .formLogin() |
| 62 | + .loginPage("/login").permitAll() |
| 63 | + .and() |
| 64 | + .logout() |
| 65 | + .permitAll() |
| 66 | + .and() |
| 67 | + .authenticationProvider(kerberosAuthenticationProvider()) |
| 68 | + .authenticationProvider(kerberosServiceAuthenticationProvider()) |
| 69 | + .addFilterBefore(spnegoAuthenticationProcessingFilter(providerManager), |
| 70 | + BasicAuthenticationFilter.class); |
| 71 | + return http.build(); |
| 72 | + } |
67 | 73 |
|
| 74 | + @Bean |
| 75 | + public KerberosAuthenticationProvider kerberosAuthenticationProvider() { |
| 76 | + KerberosAuthenticationProvider provider = new KerberosAuthenticationProvider(); |
| 77 | + SunJaasKerberosClient client = new SunJaasKerberosClient(); |
| 78 | + client.setDebug(true); |
| 79 | + provider.setKerberosClient(client); |
| 80 | + provider.setUserDetailsService(dummyUserDetailsService()); |
| 81 | + return provider; |
| 82 | + } |
| 83 | + |
| 84 | + @Bean |
| 85 | + public SpnegoEntryPoint spnegoEntryPoint() { |
| 86 | + return new SpnegoEntryPoint("/login"); |
| 87 | + } |
| 88 | + |
| 89 | + public SpnegoAuthenticationProcessingFilter spnegoAuthenticationProcessingFilter( |
| 90 | + AuthenticationManager authenticationManager) { |
| 91 | + SpnegoAuthenticationProcessingFilter filter = new SpnegoAuthenticationProcessingFilter(); |
| 92 | + filter.setAuthenticationManager(authenticationManager); |
| 93 | + return filter; |
| 94 | + } |
| 95 | + |
| 96 | + @Bean |
| 97 | + public KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider() { |
| 98 | + KerberosServiceAuthenticationProvider provider = new KerberosServiceAuthenticationProvider(); |
| 99 | + provider.setTicketValidator(sunJaasKerberosTicketValidator()); |
| 100 | + provider.setUserDetailsService(dummyUserDetailsService()); |
| 101 | + return provider; |
| 102 | + } |
| 103 | + |
| 104 | + @Bean |
| 105 | + public SunJaasKerberosTicketValidator sunJaasKerberosTicketValidator() { |
| 106 | + SunJaasKerberosTicketValidator ticketValidator = new SunJaasKerberosTicketValidator(); |
| 107 | + ticketValidator.setServicePrincipal(servicePrincipal); |
| 108 | + ticketValidator.setKeyTabLocation(new FileSystemResource(keytabLocation)); |
| 109 | + ticketValidator.setDebug(true); |
| 110 | + return ticketValidator; |
| 111 | + } |
| 112 | + |
| 113 | + @Bean |
| 114 | + public DummyUserDetailsService dummyUserDetailsService() { |
| 115 | + return new DummyUserDetailsService(); |
| 116 | + } |
68 | 117 | } |
69 | 118 | //end::snippetA[] |
0 commit comments