SpnegoConfig.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.context.annotation.Bean;
  19. import org.springframework.context.annotation.Configuration;
  20. import org.springframework.core.io.FileSystemResource;
  21. import org.springframework.security.authentication.AuthenticationManager;
  22. import org.springframework.security.authentication.ProviderManager;
  23. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  24. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  25. import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider;
  26. import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator;
  27. import org.springframework.security.kerberos.client.config.SunJaasKrb5LoginConfig;
  28. import org.springframework.security.kerberos.client.ldap.KerberosLdapContextSource;
  29. import org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter;
  30. import org.springframework.security.kerberos.web.authentication.SpnegoEntryPoint;
  31. import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;
  32. import org.springframework.security.ldap.search.FilterBasedLdapUserSearch;
  33. import org.springframework.security.ldap.userdetails.LdapUserDetailsMapper;
  34. import org.springframework.security.ldap.userdetails.LdapUserDetailsService;
  35. import org.springframework.security.web.SecurityFilterChain;
  36. import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
  37. //tag::snippetA[]
  38. @Configuration
  39. @EnableWebSecurity
  40. public class WebSecurityConfig {
  41. @Value("${app.ad-domain}")
  42. private String adDomain;
  43. @Value("${app.ad-server}")
  44. private String adServer;
  45. @Value("${app.service-principal}")
  46. private String servicePrincipal;
  47. @Value("${app.keytab-location}")
  48. private String keytabLocation;
  49. @Value("${app.ldap-search-base}")
  50. private String ldapSearchBase;
  51. @Value("${app.ldap-search-filter}")
  52. private String ldapSearchFilter;
  53. @Bean
  54. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  55. KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider = kerberosServiceAuthenticationProvider();
  56. ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider = activeDirectoryLdapAuthenticationProvider();
  57. ProviderManager providerManager = new ProviderManager(kerberosServiceAuthenticationProvider,
  58. activeDirectoryLdapAuthenticationProvider);
  59. http
  60. .authorizeHttpRequests((authz) -> authz
  61. .requestMatchers("/", "/home").permitAll()
  62. .anyRequest().authenticated()
  63. )
  64. .exceptionHandling()
  65. .authenticationEntryPoint(spnegoEntryPoint())
  66. .and()
  67. .formLogin()
  68. .loginPage("/login").permitAll()
  69. .and()
  70. .logout()
  71. .permitAll()
  72. .and()
  73. .authenticationProvider(activeDirectoryLdapAuthenticationProvider())
  74. .authenticationProvider(kerberosServiceAuthenticationProvider())
  75. .addFilterBefore(spnegoAuthenticationProcessingFilter(providerManager),
  76. BasicAuthenticationFilter.class);
  77. return http.build();
  78. }
  79. @Bean
  80. public ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
  81. return new ActiveDirectoryLdapAuthenticationProvider(adDomain, adServer);
  82. }
  83. @Bean
  84. public SpnegoEntryPoint spnegoEntryPoint() {
  85. return new SpnegoEntryPoint("/login");
  86. }
  87. public SpnegoAuthenticationProcessingFilter spnegoAuthenticationProcessingFilter(
  88. AuthenticationManager authenticationManager) {
  89. SpnegoAuthenticationProcessingFilter filter = new SpnegoAuthenticationProcessingFilter();
  90. filter.setAuthenticationManager(authenticationManager);
  91. return filter;
  92. }
  93. public KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider() throws Exception {
  94. KerberosServiceAuthenticationProvider provider = new KerberosServiceAuthenticationProvider();
  95. provider.setTicketValidator(sunJaasKerberosTicketValidator());
  96. provider.setUserDetailsService(ldapUserDetailsService());
  97. return provider;
  98. }
  99. @Bean
  100. public SunJaasKerberosTicketValidator sunJaasKerberosTicketValidator() {
  101. SunJaasKerberosTicketValidator ticketValidator = new SunJaasKerberosTicketValidator();
  102. ticketValidator.setServicePrincipal(servicePrincipal);
  103. ticketValidator.setKeyTabLocation(new FileSystemResource(keytabLocation));
  104. ticketValidator.setDebug(true);
  105. return ticketValidator;
  106. }
  107. @Bean
  108. public KerberosLdapContextSource kerberosLdapContextSource() throws Exception {
  109. KerberosLdapContextSource contextSource = new KerberosLdapContextSource(adServer);
  110. contextSource.setLoginConfig(loginConfig());
  111. return contextSource;
  112. }
  113. public SunJaasKrb5LoginConfig loginConfig() throws Exception {
  114. SunJaasKrb5LoginConfig loginConfig = new SunJaasKrb5LoginConfig();
  115. loginConfig.setKeyTabLocation(new FileSystemResource(keytabLocation));
  116. loginConfig.setServicePrincipal(servicePrincipal);
  117. loginConfig.setDebug(true);
  118. loginConfig.setIsInitiator(true);
  119. loginConfig.afterPropertiesSet();
  120. return loginConfig;
  121. }
  122. @Bean
  123. public LdapUserDetailsService ldapUserDetailsService() throws Exception {
  124. FilterBasedLdapUserSearch userSearch =
  125. new FilterBasedLdapUserSearch(ldapSearchBase, ldapSearchFilter, kerberosLdapContextSource());
  126. LdapUserDetailsService service =
  127. new LdapUserDetailsService(userSearch, new ActiveDirectoryLdapAuthoritiesPopulator());
  128. service.setUserDetailsMapper(new LdapUserDetailsMapper());
  129. return service;
  130. }
  131. }
  132. //end::snippetA[]