Feat: config spring-security pour getRestaurants sans authentification
This commit is contained in:
parent
1cec457011
commit
9a55149802
3 changed files with 83 additions and 0 deletions
|
@ -0,0 +1,42 @@
|
||||||
|
package fr.vincent.ramiere.mangerautourdesimplonback.config;
|
||||||
|
|
||||||
|
import fr.vincent.ramiere.mangerautourdesimplonback.service.JpaUserDetailsService;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.config.Customizer;
|
||||||
|
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableWebSecurity
|
||||||
|
@EnableMethodSecurity
|
||||||
|
public class SecurityConfig {
|
||||||
|
|
||||||
|
private final JpaUserDetailsService jpaUserDetailsService;
|
||||||
|
|
||||||
|
public SecurityConfig(JpaUserDetailsService jpaUserDetailsService) {
|
||||||
|
this.jpaUserDetailsService = jpaUserDetailsService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||||
|
return http
|
||||||
|
.csrf(csrf -> csrf.disable())
|
||||||
|
.authorizeHttpRequests(auth -> auth
|
||||||
|
.requestMatchers("/api/v1/restaurants/**").permitAll()
|
||||||
|
.requestMatchers("/api/v1/personnes/**").permitAll()
|
||||||
|
.anyRequest().authenticated())
|
||||||
|
.userDetailsService(jpaUserDetailsService)
|
||||||
|
.httpBasic(Customizer.withDefaults())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
PasswordEncoder passwordEncoder() {
|
||||||
|
return new BCryptPasswordEncoder();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package fr.vincent.ramiere.mangerautourdesimplonback.repository;
|
||||||
|
|
||||||
|
import fr.vincent.ramiere.mangerautourdesimplonback.models.Personne;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface PersonneRepository extends JpaRepository<Personne, Integer> {
|
||||||
|
Optional<Personne> findByEmail(String email);
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package fr.vincent.ramiere.mangerautourdesimplonback.service;
|
||||||
|
|
||||||
|
import fr.vincent.ramiere.mangerautourdesimplonback.models.Personne;
|
||||||
|
import fr.vincent.ramiere.mangerautourdesimplonback.repository.PersonneRepository;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class JpaUserDetailsService implements UserDetailsService {
|
||||||
|
|
||||||
|
private final PersonneRepository personneRepository;
|
||||||
|
|
||||||
|
public JpaUserDetailsService(PersonneRepository personneRepository) {
|
||||||
|
this.personneRepository = personneRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
|
Personne personne = personneRepository.findByEmail(username)
|
||||||
|
.orElseThrow(() -> new UsernameNotFoundException("Aucun utilisateur trouvé avec l'email: " + username));
|
||||||
|
|
||||||
|
return new org.springframework.security.core.userdetails.User(
|
||||||
|
personne.getEmail(),
|
||||||
|
personne.getPassword(),
|
||||||
|
personne.getRoleList());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue