[b3f2adb] | 1 | package com.example.eatys_app.config;
|
---|
| 2 |
|
---|
| 3 | import org.springframework.context.annotation.Bean;
|
---|
| 4 | import org.springframework.context.annotation.Configuration;
|
---|
| 5 | import org.springframework.security.authentication.AuthenticationManager;
|
---|
| 6 | import org.springframework.security.authentication.AuthenticationProvider;
|
---|
| 7 | import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
---|
| 8 | import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
|
---|
| 9 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
---|
| 10 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
---|
| 11 | import org.springframework.security.core.userdetails.UserDetailsService;
|
---|
| 12 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
| 13 | import org.springframework.security.web.SecurityFilterChain;
|
---|
| 14 |
|
---|
| 15 | @Configuration
|
---|
| 16 | @EnableWebSecurity
|
---|
| 17 | public class SecurityConfig {
|
---|
| 18 |
|
---|
| 19 | private final PasswordEncoder passwordEncoder;
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 | private final UserDetailsService userDetailsService;
|
---|
| 25 |
|
---|
| 26 | public SecurityConfig(PasswordEncoder passwordEncoder, UserDetailsService userDetailsService) {
|
---|
| 27 | this.passwordEncoder = passwordEncoder;
|
---|
| 28 | this.userDetailsService = userDetailsService;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | @Bean
|
---|
| 35 | public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
---|
| 36 |
|
---|
| 37 | http.csrf().disable()
|
---|
| 38 | .authorizeHttpRequests ()
|
---|
| 39 | .requestMatchers ("/", "/restorani", "/menija", "/obroci", "/register", "/obroci/{id}/show").permitAll()
|
---|
| 40 | .anyRequest()
|
---|
| 41 | .authenticated()
|
---|
| 42 | .and()
|
---|
| 43 | .formLogin()
|
---|
| 44 | .loginPage("/login").permitAll()
|
---|
| 45 | .failureUrl("/login?error=BadCredentials")
|
---|
| 46 | .defaultSuccessUrl("/restorani", true)
|
---|
| 47 | .and()
|
---|
| 48 | .logout()
|
---|
| 49 | .logoutUrl("/logout")
|
---|
| 50 | .clearAuthentication(true)
|
---|
| 51 | .invalidateHttpSession(true)
|
---|
| 52 | .deleteCookies("JSESSIONID")
|
---|
| 53 | .logoutSuccessUrl("/");
|
---|
| 54 |
|
---|
| 55 | return http.build();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Bean
|
---|
| 59 | public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
|
---|
| 60 | throws Exception {
|
---|
| 61 | return authenticationConfiguration.getAuthenticationManager();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | @Bean
|
---|
| 65 | public AuthenticationProvider authenticationProvider(){
|
---|
| 66 |
|
---|
| 67 | final DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
|
---|
| 68 | authenticationProvider.setUserDetailsService(userDetailsService);
|
---|
| 69 | authenticationProvider.setPasswordEncoder(passwordEncoder);
|
---|
| 70 |
|
---|
| 71 | return authenticationProvider;
|
---|
| 72 |
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | }
|
---|