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

Last change on this file since 47f4eaf was 47f4eaf, checked in by Marko <Marko@…>, 20 months ago

Final features implemented

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[dfd5d87]1package finki.it.phoneluxbackend.security.configs;
2
[f25d07e]3import finki.it.phoneluxbackend.security.CustomAuthenticationFilter;
4import finki.it.phoneluxbackend.security.CustomAuthorizationFilter;
[dfd5d87]5import finki.it.phoneluxbackend.services.UserService;
6import lombok.AllArgsConstructor;
[47f4eaf]7import org.springframework.beans.factory.annotation.Autowired;
[f25d07e]8import org.springframework.context.annotation.Bean;
[dfd5d87]9import org.springframework.context.annotation.Configuration;
[f25d07e]10import org.springframework.security.authentication.AuthenticationManager;
[dfd5d87]11import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
12import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
13import org.springframework.security.config.annotation.web.builders.HttpSecurity;
14import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
15import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
[f25d07e]16import org.springframework.security.config.http.SessionCreationPolicy;
[dfd5d87]17import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
[f25d07e]18import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
19
[dfd5d87]20@Configuration
21@AllArgsConstructor
22@EnableWebSecurity
23public 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}
Note: See TracBrowser for help on using the repository browser.