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