source: bus-n-go-do-5ta-faza/src/main/java/mk/ukim/finki/busngo/config/WebSecurityConfig.java

Last change on this file was baf4cc4, checked in by ppaunovski <paunovskipavel@…>, 3 months ago

split group project and individual project into two separate folders

  • Property mode set to 100644
File size: 3.1 KB
Line 
1package mk.ukim.finki.busngo.config;
2
3import org.springframework.context.annotation.Bean;
4import org.springframework.context.annotation.Configuration;
5import org.springframework.security.authentication.AuthenticationManager;
6import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
7import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
8import org.springframework.security.config.annotation.web.builders.HttpSecurity;
9import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
10import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
11import org.springframework.security.core.userdetails.User;
12import org.springframework.security.core.userdetails.UserDetails;
13import org.springframework.security.core.userdetails.UserDetailsService;
14import org.springframework.security.crypto.password.PasswordEncoder;
15import org.springframework.security.provisioning.InMemoryUserDetailsManager;
16import org.springframework.security.web.SecurityFilterChain;
17
18
19@Configuration
20@EnableWebSecurity
21@EnableMethodSecurity
22public class WebSecurityConfig {
23
24 private final PasswordEncoder passwordEncoder;
25 private final CustomUsernamePasswordAuthenticationProvider authProvider;
26
27 public WebSecurityConfig(PasswordEncoder passwordEncoder, CustomUsernamePasswordAuthenticationProvider authProvider) {
28 this.passwordEncoder = passwordEncoder;
29 this.authProvider = authProvider;
30 }
31
32 @Bean
33 public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
34
35 http
36 .csrf(AbstractHttpConfigurer::disable)
37 .authorizeHttpRequests( (requests) -> requests
38 .requestMatchers("/", "/home", "/assets/**", "/register")
39 .permitAll()
40 .requestMatchers("/admin/**").hasRole("ADMIN")
41 .anyRequest()
42 .authenticated()
43 )
44 .formLogin((form) -> form
45 .loginPage("/login")
46 .permitAll()
47 .failureUrl("/login?error=BadCredentials")
48 .defaultSuccessUrl("/bilet", true)
49 )
50 .logout((logout) -> logout
51 .logoutUrl("/logout")
52 .clearAuthentication(true)
53 .invalidateHttpSession(true)
54 .deleteCookies("JSESSIONID")
55 .logoutSuccessUrl("/login")
56 )
57 .exceptionHandling((ex) -> ex
58 .accessDeniedPage("/access_denied")
59 );
60
61 return http.build();
62 }
63
64
65
66 @Bean
67 public AuthenticationManager authManager(HttpSecurity http) throws Exception {
68 AuthenticationManagerBuilder authenticationManagerBuilder =
69 http.getSharedObject(AuthenticationManagerBuilder.class);
70 authenticationManagerBuilder.authenticationProvider(authProvider);
71 return authenticationManagerBuilder.build();
72 }
73}
Note: See TracBrowser for help on using the repository browser.