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

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

Initial commit

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