|
| 1 | +package com.csaba79coder.littersnap.config; |
| 2 | + |
| 3 | +import org.springframework.context.annotation.Bean; |
| 4 | +import org.springframework.context.annotation.Configuration; |
| 5 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 6 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 7 | +import org.springframework.security.core.userdetails.User; |
| 8 | +import org.springframework.security.core.userdetails.UserDetails; |
| 9 | +import org.springframework.security.core.userdetails.UserDetailsService; |
| 10 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 11 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 12 | +import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
| 13 | +import org.springframework.security.web.SecurityFilterChain; |
| 14 | + |
| 15 | +@Configuration |
| 16 | +@EnableWebSecurity |
| 17 | +public class SecurityConfiguration { |
| 18 | + |
| 19 | + @Bean |
| 20 | + public PasswordEncoder encoder() { |
| 21 | + return new BCryptPasswordEncoder(); |
| 22 | + } |
| 23 | + |
| 24 | + @Bean |
| 25 | + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 26 | + http |
| 27 | + .authorizeRequests() |
| 28 | + .anyRequest().permitAll() // Allow access to all endpoints |
| 29 | + .and() |
| 30 | + .formLogin() |
| 31 | + .loginPage("/login") |
| 32 | + .permitAll() |
| 33 | + .and() |
| 34 | + .logout() |
| 35 | + .permitAll() |
| 36 | + .and() |
| 37 | + .csrf().disable(); // Disable CSRF protection |
| 38 | + return http.build(); |
| 39 | + |
| 40 | + /* |
| 41 | + this disables all authentication and authorization for the moment! |
| 42 | + http |
| 43 | + .authorizeRequests() |
| 44 | + .anyRequest().permitAll() // Allow access to all endpoints |
| 45 | + .and() |
| 46 | + .formLogin() |
| 47 | + .disable() // Disable form-based login |
| 48 | + .and() |
| 49 | + .logout() |
| 50 | + .disable() // Disable logout functionality |
| 51 | + .and() |
| 52 | + .csrf() |
| 53 | + .disable(); // Disable CSRF protection |
| 54 | + return http.build(); |
| 55 | + */ |
| 56 | + } |
| 57 | + |
| 58 | + @Bean |
| 59 | + public UserDetailsService users() { |
| 60 | + UserDetails user = User.builder() |
| 61 | + .username("user") |
| 62 | + .password("{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW") // password |
| 63 | + .roles("USER_ROLE") |
| 64 | + .build(); |
| 65 | + UserDetails admin = User.builder() |
| 66 | + .username("admin") |
| 67 | + .password("{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW") // password |
| 68 | + .roles("ADMIN_ROLE") |
| 69 | + .build(); |
| 70 | + return new InMemoryUserDetailsManager(user, admin); |
| 71 | + } |
| 72 | +} |
0 commit comments