Ignore:
Timestamp:
03/05/24 14:15:44 (15 months ago)
Author:
Aleksandar Panovski <apano77@…>
Branches:
main
Children:
db39d9e
Parents:
a2c6c2b
Message:

Authorization layer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/rezevirajmasa/demo/config/SecurityConfig.java

    ra2c6c2b r5a9c93b  
    11package com.example.rezevirajmasa.demo.config;
    22
     3import com.example.rezevirajmasa.demo.model.exceptions.CustomerAuthenticationEntryPoint;
     4import com.example.rezevirajmasa.demo.web.filters.JwtAuthFilter;
    35import org.springframework.context.annotation.Bean;
    46import org.springframework.context.annotation.Configuration;
     7import org.springframework.http.HttpMethod;
    58import org.springframework.security.authentication.AuthenticationManager;
    69import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
     
    912import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
    1013import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
     14import org.springframework.security.config.http.SessionCreationPolicy;
    1115import org.springframework.security.core.userdetails.UserDetailsService;
    1216import 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;
     17import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
    1718import org.springframework.web.servlet.config.annotation.CorsRegistry;
    1819import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    19 
    2020
    2121@Configuration
     
    2323public class SecurityConfig implements WebMvcConfigurer {
    2424    private final UserDetailsService userDetailsService;
     25    private final CustomerAuthenticationEntryPoint customerAuthenticationEntryPoint;
     26    private final UserAuthProvider userAuthProvider;
    2527
    26     public SecurityConfig(UserDetailsService userDetailsService) {
     28    public SecurityConfig(UserDetailsService userDetailsService, CustomerAuthenticationEntryPoint customerAuthenticationEntryPoint, UserAuthProvider userAuthProvider) {
    2729        this.userDetailsService = userDetailsService;
     30        this.customerAuthenticationEntryPoint = customerAuthenticationEntryPoint;
     31        this.userAuthProvider = userAuthProvider;
    2832    }
    2933
     
    4347    }
    4448
     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
    4576    @Bean
    46     public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception  {
    47 
     77    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    4878        http
     79                .exceptionHandling((exception) -> exception.authenticationEntryPoint(customerAuthenticationEntryPoint))
     80                .addFilterBefore(new JwtAuthFilter(userAuthProvider), BasicAuthenticationFilter.class)
    4981                .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()
    6786                );
    68 
    6987        return http.build();
    7088    }
    71 
    7289    @Bean
    7390    public AuthenticationManager authManager(HttpSecurity http) throws Exception {
Note: See TracChangeset for help on using the changeset viewer.