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

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

Authorization layer

  • Property mode set to 100644
File size: 4.7 KB
Line 
1package com.example.rezevirajmasa.demo.config;
2
3import com.example.rezevirajmasa.demo.model.exceptions.CustomerAuthenticationEntryPoint;
4import com.example.rezevirajmasa.demo.web.filters.JwtAuthFilter;
5import org.springframework.context.annotation.Bean;
6import org.springframework.context.annotation.Configuration;
7import org.springframework.http.HttpMethod;
8import org.springframework.security.authentication.AuthenticationManager;
9import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
10import org.springframework.security.config.annotation.web.builders.HttpSecurity;
11import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
12import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
13import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
14import org.springframework.security.config.http.SessionCreationPolicy;
15import org.springframework.security.core.userdetails.UserDetailsService;
16import org.springframework.security.web.SecurityFilterChain;
17import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
18import org.springframework.web.servlet.config.annotation.CorsRegistry;
19import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
20
21@Configuration
22@EnableWebSecurity
23public class SecurityConfig implements WebMvcConfigurer {
24 private final UserDetailsService userDetailsService;
25 private final CustomerAuthenticationEntryPoint customerAuthenticationEntryPoint;
26 private final UserAuthProvider userAuthProvider;
27
28 public SecurityConfig(UserDetailsService userDetailsService, CustomerAuthenticationEntryPoint customerAuthenticationEntryPoint, UserAuthProvider userAuthProvider) {
29 this.userDetailsService = userDetailsService;
30 this.customerAuthenticationEntryPoint = customerAuthenticationEntryPoint;
31 this.userAuthProvider = userAuthProvider;
32 }
33
34 @Bean
35 public WebSecurityCustomizer webSecurityCustomizer() {
36 return (web) -> web.ignoring().anyRequest();
37 }
38
39 @Override
40 public void addCorsMappings(CorsRegistry registry) {
41 registry.addMapping("/**")
42 .allowCredentials(true)
43 .allowedOrigins("http://localhost:3000") // Allow requests from this origin
44 .allowedMethods("GET", "POST", "PUT", "DELETE") // Allow these HTTP methods
45 .allowedHeaders("*")
46 .maxAge(3600L); // Allow all headers
47 }
48
49// @Bean
50// public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
51//
52// http
53// .csrf(AbstractHttpConfigurer::disable)
54// .authorizeHttpRequests( (requests) -> requests
55// .requestMatchers(AntPathRequestMatcher.antMatcher("/"), AntPathRequestMatcher.antMatcher("/restaurants"))
56// .permitAll()
57// .anyRequest()
58// .hasAnyRole("ADMIN", "USER")
59// )
60// .formLogin((form) -> form
61// .permitAll()
62// .failureUrl("/login?error=BadCredentials")
63// .defaultSuccessUrl("/restaurants", true)
64// )
65// .logout((logout) -> logout
66// .logoutUrl("/logout")
67// .clearAuthentication(true)
68// .invalidateHttpSession(true)
69// .deleteCookies("JSESSIONID")
70// .logoutSuccessUrl("/")
71// );
72//
73// return http.build();
74// }
75
76 @Bean
77 public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
78 http
79 .exceptionHandling((exception) -> exception.authenticationEntryPoint(customerAuthenticationEntryPoint))
80 .addFilterBefore(new JwtAuthFilter(userAuthProvider), BasicAuthenticationFilter.class)
81 .csrf(AbstractHttpConfigurer::disable)
82 .sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
83 .authorizeHttpRequests((requests) -> requests
84 .requestMatchers(HttpMethod.POST, "/api/login", "/api/register").permitAll()
85 .anyRequest().authenticated()
86 );
87 return http.build();
88 }
89 @Bean
90 public AuthenticationManager authManager(HttpSecurity http) throws Exception {
91 AuthenticationManagerBuilder authenticationManagerBuilder =
92 http.getSharedObject(AuthenticationManagerBuilder.class);
93 authenticationManagerBuilder.userDetailsService(userDetailsService);
94 return authenticationManagerBuilder.build();
95 }
96}
Note: See TracBrowser for help on using the repository browser.