Ignore:
Timestamp:
02/02/22 14:15:35 (2 years ago)
Author:
andrejTavchioski <andrej.tavchioski@…>
Branches:
master
Children:
df4089c
Parents:
9ff45d6
Message:

backend refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sources/app/src/main/java/parkup/configs/webConfigs/WebSecurityConfig.java

    r9ff45d6 r9dd526f  
    33import org.springframework.context.annotation.Bean;
    44import org.springframework.context.annotation.Configuration;
    5 import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
     5import org.springframework.security.authentication.AuthenticationManager;
    66import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    77import org.springframework.security.config.annotation.web.builders.HttpSecurity;
     
    99import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    1010import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    11 import parkup.services.RegistriranParkiracService;
    12 import parkup.services.VrabotenService;
     11import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
     12import org.springframework.web.cors.CorsConfiguration;
     13import parkup.configs.CustomAuthenticationFilter;
     14import parkup.configs.CustomAuthorizationFilter;
     15import parkup.services.AdministratorService;
     16import parkup.services.RegisteredUserService;
     17import parkup.services.WorkerService;
     18
     19import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
    1320
    1421@EnableWebSecurity
    1522@Configuration
    1623public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    17         private final VrabotenService vrabotenService;
     24        private final WorkerService workerService;
    1825        private final BCryptPasswordEncoder bCryptPasswordEncoder;
    19         private final RegistriranParkiracService registriranParkiracService;
     26        private final RegisteredUserService registeredUserService;
     27        private final AdministratorService administratorService;
    2028
    21         public WebSecurityConfig(VrabotenService vrabotenService, BCryptPasswordEncoder bCryptPasswordEncoder, RegistriranParkiracService registriranParkiracService) {
    22             this.vrabotenService = vrabotenService;
     29        public WebSecurityConfig(WorkerService workerService, BCryptPasswordEncoder bCryptPasswordEncoder, RegisteredUserService registeredUserService, AdministratorService administratorService) {
     30            this.workerService = workerService;
    2331            this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    24             this.registriranParkiracService = registriranParkiracService;
     32            this.registeredUserService = registeredUserService;
     33            this.administratorService = administratorService;
     34        }
     35
     36        @Override
     37        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
     38            auth.userDetailsService(registeredUserService).passwordEncoder(bCryptPasswordEncoder);
     39            auth.userDetailsService(workerService).passwordEncoder(bCryptPasswordEncoder);
     40            auth.userDetailsService(administratorService).passwordEncoder(bCryptPasswordEncoder);
    2541        }
    2642
    2743        @Override
    2844        protected void configure(HttpSecurity http) throws Exception {
    29             http
    30                     .csrf().disable()
    31                     .authorizeRequests()
    32                         .antMatchers("/registriranParkirac/registration/**")
    33                         .permitAll()
    34                     .anyRequest()
    35                     .authenticated().and().formLogin();//ruta na viktor
    36         }
    37 
    38         @Override
    39         protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    40             auth.authenticationProvider(daoAuthenticationProviderW());
    41             auth.authenticationProvider(daoAuthenticationProviderRP());
     45            CustomAuthenticationFilter customAuthenticationFilter = new CustomAuthenticationFilter(authenticationManagerBean());
     46            customAuthenticationFilter.setFilterProcessesUrl("/api/login");
     47            http.csrf().disable();
     48            http.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues());
     49            http.sessionManagement().sessionCreationPolicy(STATELESS);
     50            http.authorizeRequests().antMatchers("/**").permitAll();
     51//            http.authorizeRequests().antMatchers("/user/registration/**", "/home/markers","/home/getLocation/**","/api/login/**","/home").permitAll();
     52//            http.authorizeRequests().antMatchers( "/user/setFavourite/**","user/favourites").hasAuthority("ROLE_USER");
     53//            http.authorizeRequests().antMatchers( "/home/**").hasAuthority("ROLE_ADMIN");
     54//            http.authorizeRequests().anyRequest().authenticated();
     55            http.addFilter(customAuthenticationFilter);
     56            http.addFilterBefore(new CustomAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
    4257        }
    4358
    4459        @Bean
    45         public DaoAuthenticationProvider daoAuthenticationProviderW() {
    46             DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    47             provider.setPasswordEncoder(bCryptPasswordEncoder);
    48             provider.setUserDetailsService(vrabotenService);
    49             return provider;
     60        @Override
     61        public AuthenticationManager authenticationManagerBean() throws Exception {
     62            return super.authenticationManagerBean();
    5063        }
    5164
    52         @Bean
    53         public DaoAuthenticationProvider daoAuthenticationProviderRP(){
    54             DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    55             provider.setPasswordEncoder(bCryptPasswordEncoder);
    56             provider.setUserDetailsService(registriranParkiracService);
    57             return provider;
    58         }
    5965
    6066    }
Note: See TracChangeset for help on using the changeset viewer.