AuthProviderConfig.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.KerberosAuthenticationProvider;
  26. import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider;
  27. import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosClient;
  28. import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator;
  29. import org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter;
  30. import org.springframework.security.kerberos.web.authentication.SpnegoEntryPoint;
  31. import org.springframework.security.web.SecurityFilterChain;
  32. import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
  33. //tag::snippetA[]
  34. @Configuration
  35. @EnableWebSecurity
  36. public class WebSecurityConfig {
  37. @Value("${app.service-principal}")
  38. private String servicePrincipal;
  39. @Value("${app.keytab-location}")
  40. private String keytabLocation;
  41. @Bean
  42. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  43. KerberosAuthenticationProvider kerberosAuthenticationProvider = kerberosAuthenticationProvider();
  44. KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider = kerberosServiceAuthenticationProvider();
  45. ProviderManager providerManager = new ProviderManager(kerberosAuthenticationProvider,
  46. kerberosServiceAuthenticationProvider);
  47. http
  48. .authorizeHttpRequests((authz) -> authz
  49. .requestMatchers("/", "/home").permitAll()
  50. .anyRequest().authenticated()
  51. )
  52. .exceptionHandling()
  53. .authenticationEntryPoint(spnegoEntryPoint())
  54. .and()
  55. .formLogin()
  56. .loginPage("/login").permitAll()
  57. .and()
  58. .logout()
  59. .permitAll()
  60. .and()
  61. .authenticationProvider(kerberosAuthenticationProvider())
  62. .authenticationProvider(kerberosServiceAuthenticationProvider())
  63. .addFilterBefore(spnegoAuthenticationProcessingFilter(providerManager),
  64. BasicAuthenticationFilter.class);
  65. return http.build();
  66. }
  67. @Bean
  68. public KerberosAuthenticationProvider kerberosAuthenticationProvider() {
  69. KerberosAuthenticationProvider provider = new KerberosAuthenticationProvider();
  70. SunJaasKerberosClient client = new SunJaasKerberosClient();
  71. client.setDebug(true);
  72. provider.setKerberosClient(client);
  73. provider.setUserDetailsService(dummyUserDetailsService());
  74. return provider;
  75. }
  76. @Bean
  77. public SpnegoEntryPoint spnegoEntryPoint() {
  78. return new SpnegoEntryPoint("/login");
  79. }
  80. public SpnegoAuthenticationProcessingFilter spnegoAuthenticationProcessingFilter(
  81. AuthenticationManager authenticationManager) {
  82. SpnegoAuthenticationProcessingFilter filter = new SpnegoAuthenticationProcessingFilter();
  83. filter.setAuthenticationManager(authenticationManager);
  84. return filter;
  85. }
  86. @Bean
  87. public KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider() {
  88. KerberosServiceAuthenticationProvider provider = new KerberosServiceAuthenticationProvider();
  89. provider.setTicketValidator(sunJaasKerberosTicketValidator());
  90. provider.setUserDetailsService(dummyUserDetailsService());
  91. return provider;
  92. }
  93. @Bean
  94. public SunJaasKerberosTicketValidator sunJaasKerberosTicketValidator() {
  95. SunJaasKerberosTicketValidator ticketValidator = new SunJaasKerberosTicketValidator();
  96. ticketValidator.setServicePrincipal(servicePrincipal);
  97. ticketValidator.setKeyTabLocation(new FileSystemResource(keytabLocation));
  98. ticketValidator.setDebug(true);
  99. return ticketValidator;
  100. }
  101. @Bean
  102. public DummyUserDetailsService dummyUserDetailsService() {
  103. return new DummyUserDetailsService();
  104. }
  105. }
  106. //end::snippetA[]