1 | package parkup.configs.webConfigs;
|
---|
2 |
|
---|
3 | import org.springframework.context.annotation.Bean;
|
---|
4 | import org.springframework.context.annotation.Configuration;
|
---|
5 | import org.springframework.security.authentication.AuthenticationManager;
|
---|
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;
|
---|
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.GuestService;
|
---|
17 | import parkup.services.RegisteredUserService;
|
---|
18 | import parkup.services.WorkerService;
|
---|
19 |
|
---|
20 | import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
|
---|
21 |
|
---|
22 | @EnableWebSecurity
|
---|
23 | @Configuration
|
---|
24 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
25 | private final WorkerService workerService;
|
---|
26 | private final BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
27 | private final RegisteredUserService registeredUserService;
|
---|
28 | private final AdministratorService administratorService;
|
---|
29 | private final GuestService guestService;
|
---|
30 |
|
---|
31 | public WebSecurityConfig(WorkerService workerService, BCryptPasswordEncoder bCryptPasswordEncoder, RegisteredUserService registeredUserService, AdministratorService administratorService, GuestService guestService) {
|
---|
32 | this.workerService = workerService;
|
---|
33 | this.bCryptPasswordEncoder = bCryptPasswordEncoder;
|
---|
34 | this.registeredUserService = registeredUserService;
|
---|
35 | this.administratorService = administratorService;
|
---|
36 | this.guestService = guestService;
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
---|
41 | auth.userDetailsService(registeredUserService).passwordEncoder(bCryptPasswordEncoder);
|
---|
42 | auth.userDetailsService(workerService).passwordEncoder(bCryptPasswordEncoder);
|
---|
43 | auth.userDetailsService(administratorService).passwordEncoder(bCryptPasswordEncoder);
|
---|
44 | auth.userDetailsService(guestService).passwordEncoder(bCryptPasswordEncoder);
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | protected void configure(HttpSecurity http) throws Exception {
|
---|
49 | CustomAuthenticationFilter customAuthenticationFilter = new CustomAuthenticationFilter(authenticationManagerBean());
|
---|
50 | customAuthenticationFilter.setFilterProcessesUrl("/api/login");
|
---|
51 | http.csrf().disable();
|
---|
52 | http.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues());
|
---|
53 | http.sessionManagement().sessionCreationPolicy(STATELESS);
|
---|
54 | http.authorizeRequests().antMatchers("/**").permitAll();
|
---|
55 | // http.authorizeRequests().antMatchers("/user/registration/**", "/home/markers","/home/getLocation/**","/api/login/**","/home").permitAll();
|
---|
56 | // http.authorizeRequests().antMatchers( "/user/setFavourite/**","user/favourites").hasAuthority("ROLE_USER");
|
---|
57 | // http.authorizeRequests().antMatchers( "/home/**").hasAuthority("ROLE_ADMIN");
|
---|
58 | // http.authorizeRequests().anyRequest().authenticated();
|
---|
59 | http.addFilter(customAuthenticationFilter);
|
---|
60 | http.addFilterBefore(new CustomAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Bean
|
---|
64 | @Override
|
---|
65 | public AuthenticationManager authenticationManagerBean() throws Exception {
|
---|
66 | return super.authenticationManagerBean();
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | }
|
---|