- Timestamp:
- 03/05/24 14:15:44 (15 months ago)
- Branches:
- main
- Children:
- db39d9e
- Parents:
- a2c6c2b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/rezevirajmasa/demo/config/SecurityConfig.java
ra2c6c2b r5a9c93b 1 1 package com.example.rezevirajmasa.demo.config; 2 2 3 import com.example.rezevirajmasa.demo.model.exceptions.CustomerAuthenticationEntryPoint; 4 import com.example.rezevirajmasa.demo.web.filters.JwtAuthFilter; 3 5 import org.springframework.context.annotation.Bean; 4 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.http.HttpMethod; 5 8 import org.springframework.security.authentication.AuthenticationManager; 6 9 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; … … 9 12 import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; 10 13 import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; 14 import org.springframework.security.config.http.SessionCreationPolicy; 11 15 import org.springframework.security.core.userdetails.UserDetailsService; 12 16 import org.springframework.security.web.SecurityFilterChain; 13 import org.springframework.security.web.util.matcher.AntPathRequestMatcher; 14 import org.springframework.web.cors.CorsConfiguration; 15 import org.springframework.web.cors.UrlBasedCorsConfigurationSource; 16 import org.springframework.web.filter.CorsFilter; 17 import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; 17 18 import org.springframework.web.servlet.config.annotation.CorsRegistry; 18 19 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 19 20 20 21 21 @Configuration … … 23 23 public class SecurityConfig implements WebMvcConfigurer { 24 24 private final UserDetailsService userDetailsService; 25 private final CustomerAuthenticationEntryPoint customerAuthenticationEntryPoint; 26 private final UserAuthProvider userAuthProvider; 25 27 26 public SecurityConfig(UserDetailsService userDetailsService ) {28 public SecurityConfig(UserDetailsService userDetailsService, CustomerAuthenticationEntryPoint customerAuthenticationEntryPoint, UserAuthProvider userAuthProvider) { 27 29 this.userDetailsService = userDetailsService; 30 this.customerAuthenticationEntryPoint = customerAuthenticationEntryPoint; 31 this.userAuthProvider = userAuthProvider; 28 32 } 29 33 … … 43 47 } 44 48 49 // @Bean 50 // public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { 51 // 52 // http 53 // .csrf(AbstractHttpConfigurer::disable) 54 // .authorizeHttpRequests( (requests) -> requests 55 // .requestMatchers(AntPathRequestMatcher.antMatcher("/"), AntPathRequestMatcher.antMatcher("/restaurants")) 56 // .permitAll() 57 // .anyRequest() 58 // .hasAnyRole("ADMIN", "USER") 59 // ) 60 // .formLogin((form) -> form 61 // .permitAll() 62 // .failureUrl("/login?error=BadCredentials") 63 // .defaultSuccessUrl("/restaurants", true) 64 // ) 65 // .logout((logout) -> logout 66 // .logoutUrl("/logout") 67 // .clearAuthentication(true) 68 // .invalidateHttpSession(true) 69 // .deleteCookies("JSESSIONID") 70 // .logoutSuccessUrl("/") 71 // ); 72 // 73 // return http.build(); 74 // } 75 45 76 @Bean 46 public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { 47 77 public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { 48 78 http 79 .exceptionHandling((exception) -> exception.authenticationEntryPoint(customerAuthenticationEntryPoint)) 80 .addFilterBefore(new JwtAuthFilter(userAuthProvider), BasicAuthenticationFilter.class) 49 81 .csrf(AbstractHttpConfigurer::disable) 50 .authorizeHttpRequests( (requests) -> requests 51 .requestMatchers(AntPathRequestMatcher.antMatcher("/"), AntPathRequestMatcher.antMatcher("/restaurants")) 52 .permitAll() 53 .anyRequest() 54 .hasAnyRole("ADMIN", "USER") 55 ) 56 .formLogin((form) -> form 57 .permitAll() 58 .failureUrl("/login?error=BadCredentials") 59 .defaultSuccessUrl("/restaurants", true) 60 ) 61 .logout((logout) -> logout 62 .logoutUrl("/logout") 63 .clearAuthentication(true) 64 .invalidateHttpSession(true) 65 .deleteCookies("JSESSIONID") 66 .logoutSuccessUrl("/") 82 .sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) 83 .authorizeHttpRequests((requests) -> requests 84 .requestMatchers(HttpMethod.POST, "/api/login", "/api/register").permitAll() 85 .anyRequest().authenticated() 67 86 ); 68 69 87 return http.build(); 70 88 } 71 72 89 @Bean 73 90 public AuthenticationManager authManager(HttpSecurity http) throws Exception {
Note:
See TracChangeset
for help on using the changeset viewer.