source: src/main/java/com/example/rezevirajmasa/demo/config/SecurityConfig.java@ a2c6c2b

main
Last change on this file since a2c6c2b was a2c6c2b, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Authorization layer

  • Property mode set to 100644
File size: 3.4 KB
Line 
1package com.example.rezevirajmasa.demo.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.web.builders.HttpSecurity;
8import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
10import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
11import org.springframework.security.core.userdetails.UserDetailsService;
12import org.springframework.security.web.SecurityFilterChain;
13import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
14import org.springframework.web.cors.CorsConfiguration;
15import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
16import org.springframework.web.filter.CorsFilter;
17import org.springframework.web.servlet.config.annotation.CorsRegistry;
18import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
19
20
21@Configuration
22@EnableWebSecurity
23public class SecurityConfig implements WebMvcConfigurer {
24 private final UserDetailsService userDetailsService;
25
26 public SecurityConfig(UserDetailsService userDetailsService) {
27 this.userDetailsService = userDetailsService;
28 }
29
30 @Bean
31 public WebSecurityCustomizer webSecurityCustomizer() {
32 return (web) -> web.ignoring().anyRequest();
33 }
34
35 @Override
36 public void addCorsMappings(CorsRegistry registry) {
37 registry.addMapping("/**")
38 .allowCredentials(true)
39 .allowedOrigins("http://localhost:3000") // Allow requests from this origin
40 .allowedMethods("GET", "POST", "PUT", "DELETE") // Allow these HTTP methods
41 .allowedHeaders("*")
42 .maxAge(3600L); // Allow all headers
43 }
44
45 @Bean
46 public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
47
48 http
49 .csrf(AbstractHttpConfigurer::disable)
50 .authorizeHttpRequests( (requests) -> requests
51 .requestMatchers(AntPathRequestMatcher.antMatcher("/"), AntPathRequestMatcher.antMatcher("/restaurants"))
52 .permitAll()
53 .anyRequest()
54 .hasAnyRole("ADMIN", "USER")
55 )
56 .formLogin((form) -> form
57 .permitAll()
58 .failureUrl("/login?error=BadCredentials")
59 .defaultSuccessUrl("/restaurants", true)
60 )
61 .logout((logout) -> logout
62 .logoutUrl("/logout")
63 .clearAuthentication(true)
64 .invalidateHttpSession(true)
65 .deleteCookies("JSESSIONID")
66 .logoutSuccessUrl("/")
67 );
68
69 return http.build();
70 }
71
72 @Bean
73 public AuthenticationManager authManager(HttpSecurity http) throws Exception {
74 AuthenticationManagerBuilder authenticationManagerBuilder =
75 http.getSharedObject(AuthenticationManagerBuilder.class);
76 authenticationManagerBuilder.userDetailsService(userDetailsService);
77 return authenticationManagerBuilder.build();
78 }
79}
Note: See TracBrowser for help on using the repository browser.