[9ff45d6] | 1 | package parkup.configs.webConfigs;
|
---|
[ce6ad22] | 2 |
|
---|
| 3 | import org.springframework.context.annotation.Bean;
|
---|
| 4 | import org.springframework.context.annotation.Configuration;
|
---|
[9dd526f] | 5 | import org.springframework.security.authentication.AuthenticationManager;
|
---|
[ce6ad22] | 6 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
---|
| 7 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
---|
| 8 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
---|
| 9 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
---|
| 10 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[9dd526f] | 11 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
---|
| 12 | import org.springframework.web.cors.CorsConfiguration;
|
---|
| 13 | import parkup.configs.CustomAuthenticationFilter;
|
---|
| 14 | import parkup.configs.CustomAuthorizationFilter;
|
---|
| 15 | import parkup.services.AdministratorService;
|
---|
| 16 | import parkup.services.RegisteredUserService;
|
---|
| 17 | import parkup.services.WorkerService;
|
---|
| 18 |
|
---|
| 19 | import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
|
---|
[ce6ad22] | 20 |
|
---|
| 21 | @EnableWebSecurity
|
---|
[97fbc67] | 22 | @Configuration
|
---|
[ce6ad22] | 23 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
[9dd526f] | 24 | private final WorkerService workerService;
|
---|
[97fbc67] | 25 | private final BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
[9dd526f] | 26 | private final RegisteredUserService registeredUserService;
|
---|
| 27 | private final AdministratorService administratorService;
|
---|
[ce6ad22] | 28 |
|
---|
[9dd526f] | 29 | public WebSecurityConfig(WorkerService workerService, BCryptPasswordEncoder bCryptPasswordEncoder, RegisteredUserService registeredUserService, AdministratorService administratorService) {
|
---|
| 30 | this.workerService = workerService;
|
---|
[97fbc67] | 31 | this.bCryptPasswordEncoder = bCryptPasswordEncoder;
|
---|
[9dd526f] | 32 | this.registeredUserService = registeredUserService;
|
---|
| 33 | this.administratorService = administratorService;
|
---|
[97fbc67] | 34 | }
|
---|
[ce6ad22] | 35 |
|
---|
[97fbc67] | 36 | @Override
|
---|
[9dd526f] | 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);
|
---|
[97fbc67] | 41 | }
|
---|
[ce6ad22] | 42 |
|
---|
[97fbc67] | 43 | @Override
|
---|
[9dd526f] | 44 | protected void configure(HttpSecurity http) throws Exception {
|
---|
| 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);
|
---|
[97fbc67] | 57 | }
|
---|
[ce6ad22] | 58 |
|
---|
[97fbc67] | 59 | @Bean
|
---|
[9dd526f] | 60 | @Override
|
---|
| 61 | public AuthenticationManager authenticationManagerBean() throws Exception {
|
---|
| 62 | return super.authenticationManagerBean();
|
---|
[97fbc67] | 63 | }
|
---|
| 64 |
|
---|
[ce6ad22] | 65 |
|
---|
| 66 | }
|
---|