1 | package mk.ukim.finki.busngo.config;
|
---|
2 |
|
---|
3 | import org.springframework.context.annotation.Bean;
|
---|
4 | import org.springframework.context.annotation.Configuration;
|
---|
5 | import org.springframework.security.authentication.AuthenticationManager;
|
---|
6 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
---|
7 | import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
---|
8 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
---|
9 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
---|
10 | import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
---|
11 | import org.springframework.security.core.userdetails.User;
|
---|
12 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
13 | import org.springframework.security.core.userdetails.UserDetailsService;
|
---|
14 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
15 | import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
---|
16 | import org.springframework.security.web.SecurityFilterChain;
|
---|
17 |
|
---|
18 |
|
---|
19 | @Configuration
|
---|
20 | @EnableWebSecurity
|
---|
21 | @EnableMethodSecurity
|
---|
22 | public 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 | }
|
---|