Skip to content

Commit f5fb127

Browse files
committed
Add Spring Security Kerberos
Move the Spring Security Kerberos Extension into Spring Security Closes gh-17879
1 parent e8bf470 commit f5fb127

File tree

69 files changed

+6173
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+6173
-0
lines changed
19 KB
Loading
24.1 KB
Loading
24.1 KB
Loading
22 KB
Loading
34.6 KB
Loading
35.4 KB
Loading
32.1 KB
Loading
15.2 KB
Loading
18.8 KB
Loading
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.kerberos.docs;
17+
18+
import org.springframework.beans.factory.annotation.Value;
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.core.io.FileSystemResource;
22+
import org.springframework.security.authentication.AuthenticationManager;
23+
import org.springframework.security.authentication.ProviderManager;
24+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
25+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
26+
import org.springframework.security.kerberos.authentication.KerberosAuthenticationProvider;
27+
import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider;
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;
34+
35+
//tag::snippetA[]
36+
@Configuration
37+
@EnableWebSecurity
38+
public class WebSecurityConfig {
39+
40+
@Value("${app.service-principal}")
41+
private String servicePrincipal;
42+
43+
@Value("${app.keytab-location}")
44+
private String keytabLocation;
45+
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);
52+
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+
}
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+
}
117+
}
118+
//end::snippetA[]

0 commit comments

Comments
 (0)