WebSecurityConfig.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.lqkj.cmlcp.config;
  2. import com.lqkj.cmlcp.config.auth.LoginAuthenticationProvider;
  3. import com.lqkj.cmlcp.filter.JwtAuthFilter;
  4. import com.lqkj.cmlcp.module.authority.service.impl.DatabaseUserDetailService;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.security.authentication.AuthenticationManager;
  8. import org.springframework.security.authentication.AuthenticationProvider;
  9. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  10. import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
  11. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  12. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  13. import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
  14. import org.springframework.security.config.http.SessionCreationPolicy;
  15. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  16. import org.springframework.security.crypto.password.PasswordEncoder;
  17. import org.springframework.security.web.SecurityFilterChain;
  18. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  19. @Configuration
  20. @EnableWebSecurity
  21. public class WebSecurityConfig {
  22. private final JwtAuthFilter authFilter;
  23. private final DatabaseUserDetailService userDetailService;
  24. public WebSecurityConfig(JwtAuthFilter authFilter, DatabaseUserDetailService userDetailService) {
  25. this.authFilter = authFilter;
  26. this.userDetailService = userDetailService;
  27. }
  28. @Bean
  29. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  30. return http
  31. .csrf(AbstractHttpConfigurer::disable)
  32. .authorizeHttpRequests((requests) -> requests
  33. .requestMatchers(
  34. "/jwt/token",
  35. "/jwt/getAdminToken",
  36. "/encrypt/**",
  37. "/swagger-ui.html",
  38. "/swagger-ui/**",
  39. "/v3/api-docs/**",
  40. "/geom/all",
  41. "/test/**",
  42. "/upload/**")
  43. .permitAll()
  44. .requestMatchers("/**")
  45. .authenticated())
  46. .sessionManagement((session) -> session
  47. .sessionCreationPolicy(SessionCreationPolicy.STATELESS))
  48. .authenticationProvider(authenticationProvider())
  49. .addFilterBefore(authFilter, UsernamePasswordAuthenticationFilter.class)
  50. .build();
  51. }
  52. @Bean
  53. public PasswordEncoder passwordEncoder() {
  54. return new BCryptPasswordEncoder();
  55. }
  56. @Bean
  57. public AuthenticationProvider authenticationProvider(){
  58. DaoAuthenticationProvider authenticationProvider=new LoginAuthenticationProvider(userDetailService, passwordEncoder());
  59. authenticationProvider.setUserDetailsService(userDetailService);
  60. authenticationProvider.setPasswordEncoder(passwordEncoder());
  61. return authenticationProvider;
  62. }
  63. @Bean
  64. public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
  65. return config.getAuthenticationManager();
  66. }
  67. }