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

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

Authorization layer

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