[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;
|
---|
[f25d07e] | 7 | import org.springframework.context.annotation.Bean;
|
---|
[dfd5d87] | 8 | import org.springframework.context.annotation.Configuration;
|
---|
[f25d07e] | 9 | import org.springframework.security.authentication.AuthenticationManager;
|
---|
[dfd5d87] | 10 | import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
---|
| 11 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
---|
| 12 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
---|
| 13 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
---|
| 14 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
---|
[f25d07e] | 15 | import org.springframework.security.config.http.SessionCreationPolicy;
|
---|
[dfd5d87] | 16 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[f25d07e] | 17 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
---|
| 18 |
|
---|
| 19 | import static org.springframework.http.HttpMethod.GET;
|
---|
[dfd5d87] | 20 |
|
---|
| 21 | @Configuration
|
---|
| 22 | @AllArgsConstructor
|
---|
| 23 | @EnableWebSecurity
|
---|
| 24 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
| 25 |
|
---|
| 26 | private final UserService userService;
|
---|
| 27 | private final BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
| 28 |
|
---|
| 29 | @Override
|
---|
| 30 | protected void configure(HttpSecurity http) throws Exception {
|
---|
[f25d07e] | 31 | // http
|
---|
| 32 | // .csrf().disable()
|
---|
| 33 | // .authorizeRequests()
|
---|
| 34 | // .antMatchers("/registration/**")
|
---|
| 35 | // .permitAll()
|
---|
| 36 | // .anyRequest()
|
---|
| 37 | // .authenticated().and()
|
---|
| 38 | // .formLogin();
|
---|
| 39 |
|
---|
| 40 | http.csrf().disable();
|
---|
| 41 | http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
|
---|
| 42 | // http.authorizeRequests().antMatchers(GET,"/phones").hasAnyAuthority("USER");
|
---|
| 43 | http.authorizeRequests().anyRequest().permitAll();
|
---|
| 44 | http.addFilter(new CustomAuthenticationFilter(authenticationManagerBean()));
|
---|
| 45 | http.addFilterBefore(new CustomAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
|
---|
[dfd5d87] | 46 |
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | @Override
|
---|
| 50 | protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
---|
| 51 | auth.authenticationProvider(daoAuthenticationProvider());
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[f25d07e] | 54 | @Bean
|
---|
| 55 | @Override
|
---|
| 56 | public AuthenticationManager authenticationManagerBean() throws Exception {
|
---|
| 57 | return super.authenticationManagerBean();
|
---|
| 58 | }
|
---|
[dfd5d87] | 59 |
|
---|
| 60 | public DaoAuthenticationProvider daoAuthenticationProvider(){
|
---|
| 61 | DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
---|
| 62 | provider.setPasswordEncoder(bCryptPasswordEncoder);
|
---|
| 63 | provider.setUserDetailsService(userService);
|
---|
| 64 | return provider;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | }
|
---|