33
44import com .howtodoinjava .app .security .filter .JwtTokenFilter ;
55import com .howtodoinjava .app .security .repo .UserRepository ;
6- import com .howtodoinjava .app .security .utils .JwtAuthenticationEntryPoint ;
76import jakarta .servlet .http .HttpServletResponse ;
87import lombok .RequiredArgsConstructor ;
98import org .springframework .beans .factory .annotation .Autowired ;
109import org .springframework .context .annotation .Bean ;
1110import org .springframework .context .annotation .Configuration ;
1211
13- import org .springframework .http . HttpMethod ;
12+ import org .springframework .context . annotation . Lazy ;
1413import org .springframework .security .authentication .AuthenticationManager ;
1514
1615import org .springframework .security .authentication .AuthenticationProvider ;
1918import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
2019import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
2120import org .springframework .security .config .http .SessionCreationPolicy ;
22- import org .springframework .security .core .userdetails .UserDetails ;
2321import org .springframework .security .core .userdetails .UserDetailsService ;
24- import org .springframework .security .core .userdetails .UsernameNotFoundException ;
2522import org .springframework .security .crypto .password .NoOpPasswordEncoder ;
2623import org .springframework .security .crypto .password .PasswordEncoder ;
2724import org .springframework .security .web .SecurityFilterChain ;
2825import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
29- import org .springframework .web .cors .CorsConfiguration ;
30- import org .springframework .web .cors .CorsConfigurationSource ;
31- import org .springframework .web .cors .UrlBasedCorsConfigurationSource ;
26+
3227
3328@ EnableWebSecurity
3429@ Configuration
3530@ RequiredArgsConstructor
3631public class SecurityConfig {
3732
3833 private final JwtTokenFilter jwtAuthenticationFilter ;
39- private final AuthenticationProvider authenticationProvider ;
40- private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint ;
34+ private final UserDetailsService userDetailsService ;
4135
42- @ Autowired
43- private UserRepository userRepository ;
4436
45- @ Bean
46- UserDetailsService userDetailsService () {
47- return new UserDetailsService () {
48- @ Override
49- public UserDetails loadUserByUsername (String username ) throws UsernameNotFoundException {
50- try {
51- return userRepository .findByUsername (username )
52- .orElseThrow (() -> new Exception ("user Not found hahahah " ));
53- } catch (Exception e ) {
54- throw new RuntimeException (e );
55- }
56- }
57- };
58- }
5937
6038 @ Bean
6139 public AuthenticationProvider authenticationProvider () {
6240 DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider ();
63- daoAuthenticationProvider .setUserDetailsService (userDetailsService () );
41+ daoAuthenticationProvider .setUserDetailsService (userDetailsService );
6442 daoAuthenticationProvider .setPasswordEncoder (passwordEncoder ());
6543 return daoAuthenticationProvider ;
6644 }
@@ -78,25 +56,25 @@ AuthenticationManager authenticationManager(
7856
7957 @ Bean
8058 public SecurityFilterChain securityFilterChain (HttpSecurity httpSecurity ) throws Exception {
81- httpSecurity . csrf (). disable ();
59+
8260 httpSecurity .headers ().frameOptions ().disable ();
61+ httpSecurity .csrf ().disable ();
8362 httpSecurity
84- .authorizeRequests ()
85- .requestMatchers (HttpMethod .POST , "/api/auth/login" ).permitAll ()
86- .requestMatchers (HttpMethod .GET , "/api/auth/logout" ).permitAll ()
87- .requestMatchers (HttpMethod .POST , "/h2-console" ).permitAll ()
88- .anyRequest ().authenticated ()
89- .and ()
90- .exceptionHandling ()
91- .authenticationEntryPoint (jwtAuthenticationEntryPoint )
92- .and ()
93- .sessionManagement ()
94- .sessionCreationPolicy (SessionCreationPolicy .STATELESS )
95- .and ()
96- .authenticationProvider (authenticationProvider )
97- .addFilterBefore (jwtAuthenticationFilter , UsernamePasswordAuthenticationFilter .class );
63+ .authorizeHttpRequests ()
64+ .requestMatchers ("/api/auth/**" ).permitAll ()
65+ .anyRequest ().authenticated ()
66+ .and ()
67+ .sessionManagement ()
68+ .sessionCreationPolicy (SessionCreationPolicy .STATELESS )
69+ .and ()
70+ .exceptionHandling ()
71+ .authenticationEntryPoint (((request , response , authException ) -> response .sendError (HttpServletResponse .SC_UNAUTHORIZED )))
72+ .and ()
73+ .authenticationProvider (authenticationProvider ())
74+ .addFilterBefore (jwtAuthenticationFilter , UsernamePasswordAuthenticationFilter .class );
9875
9976 return httpSecurity .build ();
77+
10078 }
10179
10280}
0 commit comments