source: phonelux-backend/src/main/java/finki/it/phoneluxbackend/security/configs/WebSecurityConfig.java@ 7e88e46

Last change on this file since 7e88e46 was 775e15e, checked in by Marko <Marko@…>, 22 months ago

Added more controllers

  • Property mode set to 100644
File size: 2.8 KB
Line 
1package finki.it.phoneluxbackend.security.configs;
2
3import finki.it.phoneluxbackend.security.CustomAuthenticationFilter;
4import finki.it.phoneluxbackend.security.CustomAuthorizationFilter;
5import finki.it.phoneluxbackend.services.UserService;
6import lombok.AllArgsConstructor;
7import org.springframework.context.annotation.Bean;
8import org.springframework.context.annotation.Configuration;
9import org.springframework.security.authentication.AuthenticationManager;
10import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
11import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
12import org.springframework.security.config.annotation.web.builders.HttpSecurity;
13import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
14import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
15import org.springframework.security.config.http.SessionCreationPolicy;
16import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
17import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
18
19import static org.springframework.http.HttpMethod.GET;
20
21@Configuration
22@AllArgsConstructor
23@EnableWebSecurity
24public 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 {
31
32
33 http.csrf().disable();
34 http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
35
36 http.authorizeRequests()
37 .and()
38 .authorizeRequests()
39 .antMatchers("/user/**")
40 .hasAnyAuthority("USER","ADMIN", "SUPERADMIN")
41 .and()
42 .authorizeRequests()
43 .antMatchers("/management/**")
44 .hasAnyAuthority("SUPERADMIN")
45 .anyRequest().permitAll();
46
47
48
49 http.addFilter(new CustomAuthenticationFilter(authenticationManagerBean()));
50 http.addFilterBefore(new CustomAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
51
52 }
53
54 @Override
55 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
56 auth.authenticationProvider(daoAuthenticationProvider());
57 }
58
59 @Bean
60 @Override
61 public AuthenticationManager authenticationManagerBean() throws Exception {
62 return super.authenticationManagerBean();
63 }
64
65 public DaoAuthenticationProvider daoAuthenticationProvider(){
66 DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
67 provider.setPasswordEncoder(bCryptPasswordEncoder);
68 provider.setUserDetailsService(userService);
69 return provider;
70 }
71
72}
Note: See TracBrowser for help on using the repository browser.