[8d11f8c] | 1 | package com.example.fooddeliverysystem.configuration;
|
---|
| 2 |
|
---|
| 3 | import org.springframework.context.annotation.Bean;
|
---|
| 4 | import org.springframework.context.annotation.Configuration;
|
---|
| 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
---|
| 6 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
---|
| 7 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
| 8 | import org.springframework.security.web.SecurityFilterChain;
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | @Configuration
|
---|
| 12 | @EnableWebSecurity
|
---|
| 13 | public class WebSecurityConfiguration {
|
---|
| 14 |
|
---|
| 15 | private final PasswordEncoder passwordEncoder;
|
---|
| 16 | private final UsernameAndPasswordAuthProvider usernameAndPasswordAuthProvider;
|
---|
| 17 |
|
---|
| 18 | public WebSecurityConfiguration(PasswordEncoder passwordEncoder, UsernameAndPasswordAuthProvider usernameAndPasswordAuthProvider) {
|
---|
| 19 | this.passwordEncoder = passwordEncoder;
|
---|
| 20 | this.usernameAndPasswordAuthProvider = usernameAndPasswordAuthProvider;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | @Bean
|
---|
| 24 | public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
---|
| 25 | http.csrf()
|
---|
| 26 | .disable()
|
---|
| 27 | .authorizeHttpRequests()
|
---|
| 28 | .requestMatchers("/salePlaces").hasAnyRole("ADMIN", "CONSUMER")
|
---|
| 29 | .requestMatchers("/checkOrderStatus").hasAnyRole("ADMIN", "CONSUMER")
|
---|
| 30 | .requestMatchers("/salePlace/Orders").hasAnyRole("ADMIN", "SALEPLACEEMPLOYEE")
|
---|
| 31 | .requestMatchers("/salePlace/**").hasAnyRole("ADMIN", "CONSUMER")
|
---|
| 32 | .requestMatchers("/checkOrderStatus").hasAnyRole("ADMIN", "CONSUMER")
|
---|
| 33 | .requestMatchers("/deliveryOrders").hasAnyRole("ADMIN", "DELIVER")
|
---|
| 34 | .requestMatchers("/takeOrder/*").hasAnyRole("ADMIN", "DELIVER")
|
---|
| 35 | .requestMatchers("/showOrderDeliverer").hasAnyRole("ADMIN", "DELIVER")
|
---|
| 36 | .requestMatchers("/orderPayment/*").hasAnyRole("ADMIN", "DELIVER")
|
---|
| 37 | .requestMatchers("/reports/**").hasRole("ADMIN")
|
---|
| 38 | .requestMatchers("/images/**","/home","/").permitAll()
|
---|
| 39 | .anyRequest()
|
---|
| 40 | .authenticated()
|
---|
| 41 | .and()
|
---|
| 42 | .formLogin()
|
---|
| 43 | .permitAll()
|
---|
| 44 | .failureUrl("/login?error=BadCredentials")
|
---|
| 45 | .defaultSuccessUrl("/home", true)
|
---|
| 46 | .and()
|
---|
| 47 | .logout()
|
---|
| 48 | .logoutUrl("/logout")
|
---|
| 49 | .clearAuthentication(true)
|
---|
| 50 | .invalidateHttpSession(true)
|
---|
| 51 | .deleteCookies("JSESSIONID")
|
---|
| 52 | .logoutSuccessUrl("/home");
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | return http.build();
|
---|
| 56 |
|
---|
| 57 | }
|
---|
| 58 | }
|
---|