WebSecurityConfig.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package org.example.sweater.config;
  2. import org.example.sweater.service.UserService;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.security.authentication.AuthenticationManager;
  6. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  7. import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
  8. import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
  9. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  10. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  11. import org.springframework.security.crypto.password.NoOpPasswordEncoder;
  12. import org.springframework.security.crypto.password.PasswordEncoder;
  13. import org.springframework.security.web.SecurityFilterChain;
  14. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  15. import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
  16. import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
  17. @Configuration
  18. @EnableWebSecurity
  19. @EnableMethodSecurity
  20. public class WebSecurityConfig {
  21. public WebSecurityConfig(UserService userService) {
  22. this.userService = userService;
  23. }
  24. private final UserService userService;
  25. @Bean
  26. public FreeMarkerConfigurer freeMarkerConfigurer() {
  27. FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
  28. configurer.setTemplateLoaderPath("classpath:/templates/");
  29. return configurer;
  30. }
  31. @Bean
  32. FreeMarkerViewResolver freeMarkerViewResolver() {
  33. FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
  34. resolver.setCache(true);
  35. resolver.setPrefix("");
  36. resolver.setSuffix(".ftl");
  37. resolver.setOrder(1);
  38. return resolver;
  39. }
  40. @Bean
  41. public SecurityFilterChain securityFilterChain(
  42. HttpSecurity http
  43. ) throws Exception {
  44. http
  45. .authorizeHttpRequests((requests) -> requests
  46. .requestMatchers("/", "/registration")
  47. .permitAll()
  48. .anyRequest()
  49. .authenticated()
  50. )
  51. .formLogin((form) -> form
  52. .loginPage("/login")
  53. .permitAll()
  54. )
  55. .logout((logout) -> logout.permitAll())
  56. .csrf((csrf) -> csrf
  57. .csrfTokenRepository(
  58. CookieCsrfTokenRepository.withHttpOnlyFalse()
  59. )
  60. );
  61. return http.build();
  62. }
  63. @Bean
  64. public AuthenticationManager authenticationManager(
  65. AuthenticationConfiguration authenticationConfiguration
  66. ) throws Exception {
  67. return authenticationConfiguration.getAuthenticationManager();
  68. }
  69. @Bean
  70. public PasswordEncoder passwordEncoder() {
  71. return NoOpPasswordEncoder.getInstance();
  72. }
  73. @Bean
  74. public DaoAuthenticationProvider authenticationProvider() {
  75. DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
  76. authenticationProvider.setUserDetailsService(userService);
  77. authenticationProvider.setPasswordEncoder(passwordEncoder());
  78. return authenticationProvider;
  79. }
  80. }