source: src/main/java/com/example/task/security/SecurityConfig.java@ fdfbdde

Last change on this file since fdfbdde was fdfbdde, checked in by Stojilkova Sara <sara.stojilkova.students.finki.ukim.mk>, 9 months ago

Initial commit

  • Property mode set to 100644
File size: 1.9 KB
Line 
1package com.example.task.security;
2
3import lombok.AllArgsConstructor;
4import org.springframework.context.annotation.Bean;
5import org.springframework.context.annotation.Configuration;
6import org.springframework.security.authentication.AuthenticationProvider;
7import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
8import org.springframework.security.config.annotation.web.builders.HttpSecurity;
9import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
10import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
11import org.springframework.security.core.userdetails.UserDetailsService;
12import org.springframework.security.crypto.password.PasswordEncoder;
13import org.springframework.security.web.SecurityFilterChain;
14
15@Configuration
16@EnableWebSecurity
17@AllArgsConstructor
18public class SecurityConfig {
19
20 private final PasswordEncoder passwordEncoder;
21
22 private final UserDetailsService userDetailsService;
23
24 private final CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;
25
26
27 @Bean
28 public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
29 httpSecurity.authorizeHttpRequests((requests) -> requests.requestMatchers("/", "/login", "/register")
30 .permitAll()
31 .anyRequest().authenticated())
32 .formLogin((form) -> form.loginPage("/login").permitAll().successHandler(customAuthenticationSuccessHandler))
33 .logout((logout) -> logout.permitAll().logoutSuccessUrl("/"))
34 .csrf(AbstractHttpConfigurer::disable);
35 return httpSecurity.build();
36 }
37
38 @Bean
39 public AuthenticationProvider authenticationProvider() {
40 DaoAuthenticationProvider dao = new DaoAuthenticationProvider();
41 dao.setPasswordEncoder(passwordEncoder);
42 dao.setUserDetailsService(userDetailsService);
43 return dao;
44 }
45
46
47}
Note: See TracBrowser for help on using the repository browser.