[dfd5d87] | 1 | package finki.it.phoneluxbackend.security.configs;
|
---|
| 2 |
|
---|
[f25d07e] | 3 | import finki.it.phoneluxbackend.security.CustomAuthenticationFilter;
|
---|
| 4 | import finki.it.phoneluxbackend.security.CustomAuthorizationFilter;
|
---|
[dfd5d87] | 5 | import finki.it.phoneluxbackend.services.UserService;
|
---|
| 6 | import lombok.AllArgsConstructor;
|
---|
[47f4eaf] | 7 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
[f25d07e] | 8 | import org.springframework.context.annotation.Bean;
|
---|
[dfd5d87] | 9 | import org.springframework.context.annotation.Configuration;
|
---|
[f25d07e] | 10 | import org.springframework.security.authentication.AuthenticationManager;
|
---|
[dfd5d87] | 11 | import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
---|
| 12 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
---|
| 13 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
---|
| 14 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
---|
| 15 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
---|
[f25d07e] | 16 | import org.springframework.security.config.http.SessionCreationPolicy;
|
---|
[dfd5d87] | 17 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[f25d07e] | 18 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
---|
| 19 |
|
---|
[dfd5d87] | 20 | @Configuration
|
---|
| 21 | @AllArgsConstructor
|
---|
| 22 | @EnableWebSecurity
|
---|
| 23 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
| 24 |
|
---|
| 25 | private final UserService userService;
|
---|
| 26 | private final BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
| 27 | @Override
|
---|
| 28 | protected void configure(HttpSecurity http) throws Exception {
|
---|
[775e15e] | 29 |
|
---|
[f25d07e] | 30 |
|
---|
| 31 | http.csrf().disable();
|
---|
| 32 | http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
|
---|
[775e15e] | 33 |
|
---|
| 34 | http.authorizeRequests()
|
---|
| 35 | .and()
|
---|
| 36 | .authorizeRequests()
|
---|
| 37 | .antMatchers("/user/**")
|
---|
| 38 | .hasAnyAuthority("USER","ADMIN", "SUPERADMIN")
|
---|
| 39 | .and()
|
---|
| 40 | .authorizeRequests()
|
---|
| 41 | .antMatchers("/management/**")
|
---|
| 42 | .hasAnyAuthority("SUPERADMIN")
|
---|
[5201690] | 43 | .and()
|
---|
| 44 | .authorizeRequests()
|
---|
| 45 | .antMatchers("/admin/**")
|
---|
| 46 | .hasAnyAuthority("ADMIN","SUPERADMIN")
|
---|
[47f4eaf] | 47 | .and()
|
---|
| 48 | .authorizeRequests()
|
---|
| 49 | .antMatchers("/offerreport/**")
|
---|
| 50 | .hasAnyAuthority("USER", "ADMIN", "SUPERADMIN")
|
---|
| 51 | .and()
|
---|
| 52 | .authorizeRequests()
|
---|
| 53 | .antMatchers("/scrapperinfo/**")
|
---|
| 54 | .hasAnyAuthority("SUPERADMIN")
|
---|
| 55 | .and()
|
---|
| 56 | .authorizeRequests()
|
---|
[775e15e] | 57 | .anyRequest().permitAll();
|
---|
| 58 |
|
---|
| 59 |
|
---|
[f25d07e] | 60 | http.addFilter(new CustomAuthenticationFilter(authenticationManagerBean()));
|
---|
| 61 | http.addFilterBefore(new CustomAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
|
---|
[dfd5d87] | 62 |
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[47f4eaf] | 65 |
|
---|
[dfd5d87] | 66 | @Override
|
---|
| 67 | protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
---|
| 68 | auth.authenticationProvider(daoAuthenticationProvider());
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[f25d07e] | 71 | @Bean
|
---|
| 72 | @Override
|
---|
| 73 | public AuthenticationManager authenticationManagerBean() throws Exception {
|
---|
| 74 | return super.authenticationManagerBean();
|
---|
| 75 | }
|
---|
[dfd5d87] | 76 |
|
---|
| 77 | public DaoAuthenticationProvider daoAuthenticationProvider(){
|
---|
| 78 | DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
---|
| 79 | provider.setPasswordEncoder(bCryptPasswordEncoder);
|
---|
| 80 | provider.setUserDetailsService(userService);
|
---|
| 81 | return provider;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | }
|
---|