From 34526d9216c3f643f81cfc0ebd17333576f6b9d9 Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Sun, 29 Jan 2017 15:21:50 +0100 Subject: [PATCH] WIP Authentification --- .gitignore | 1 + etunicorn-server.iml | 20 +++++++++++++++----- pom.xml | 8 ++++++++ src/main/java/etunicorn/CustomUserDetailsService.java | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/etunicorn/Personne.java | 8 ++++++++ src/main/java/etunicorn/UserDetailsService.java | 11 +++++++++++ src/main/java/etunicorn/WebSecurityConfiguration.java | 33 +++++++++++++++++++++++++++++++++ src/main/main.iml | 2 ++ 8 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 src/main/java/etunicorn/CustomUserDetailsService.java create mode 100644 src/main/java/etunicorn/UserDetailsService.java create mode 100644 src/main/java/etunicorn/WebSecurityConfiguration.java diff --git a/.gitignore b/.gitignore index c4a4981..d54b284 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ .idea/libraries/ target/ demo*/ +*.ldif diff --git a/etunicorn-server.iml b/etunicorn-server.iml index 54ef4dd..6c2fc62 100644 --- a/etunicorn-server.iml +++ b/etunicorn-server.iml @@ -5,6 +5,7 @@ + @@ -28,8 +29,6 @@ - - @@ -41,7 +40,6 @@ - @@ -61,9 +59,7 @@ - - @@ -78,11 +74,25 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 27dc2ba..85cb0cb 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,10 @@ spring-boot-starter-test + org.springframework.boot + spring-boot-starter-security + + com.jayway.jsonpath json-path @@ -34,6 +38,10 @@ com.h2database h2 + + org.springframework.security.oauth + spring-security-oauth2 + diff --git a/src/main/java/etunicorn/CustomUserDetailsService.java b/src/main/java/etunicorn/CustomUserDetailsService.java new file mode 100644 index 0000000..e9e6c4f --- /dev/null +++ b/src/main/java/etunicorn/CustomUserDetailsService.java @@ -0,0 +1,73 @@ +package etunicorn; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + +import java.util.Collection; + +/** + * Created by geoffrey on 29/01/17. + */ +public class CustomUserDetailsService implements UserDetailsService { + + private final PersonneRepository personneRepository; + + @Autowired + public CustomUserDetailsService(PersonneRepository personneRepository) { + this.personneRepository = personneRepository; + } + + @Override + public UserDetails loadByUsername(String login) throws UsernameNotFoundException { + Personne personne = personneRepository.findByLogin(login); + if (personne == null) { + throw new UsernameNotFoundException(String.format("L'utilisateur %s n'existe pas !", login)); + } + return new PersonneRepositoryUserDetails(personne); + } + + private final static class PersonneRepositoryUserDetails extends Personne implements UserDetails { + + public PersonneRepositoryUserDetails(Personne personne) { + super(personne); + } + + @Override + public Collection getAuthorities() { + // return getRoles(); + return null; + } + + @Override + public String getPassword() { + return "test"; + } + + @Override + public String getUsername() { + return getLogin(); + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return true; + } + } +} diff --git a/src/main/java/etunicorn/Personne.java b/src/main/java/etunicorn/Personne.java index ada8cd2..c431bda 100644 --- a/src/main/java/etunicorn/Personne.java +++ b/src/main/java/etunicorn/Personne.java @@ -24,6 +24,14 @@ public class Personne { public Personne() { } + public Personne(Personne personne) { + super(); + this.id = personne.getId(); + this.carte = personne.getCarte(); + this.naissance = personne.getNaissance(); + this.login = personne.getLogin(); + } + public int getId() { return id; } diff --git a/src/main/java/etunicorn/UserDetailsService.java b/src/main/java/etunicorn/UserDetailsService.java new file mode 100644 index 0000000..36242f7 --- /dev/null +++ b/src/main/java/etunicorn/UserDetailsService.java @@ -0,0 +1,11 @@ +package etunicorn; + +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + +/** + * Created by geoffrey on 29/01/17. + */ +public interface UserDetailsService { + UserDetails loadByUsername(String s) throws UsernameNotFoundException; +} diff --git a/src/main/java/etunicorn/WebSecurityConfiguration.java b/src/main/java/etunicorn/WebSecurityConfiguration.java new file mode 100644 index 0000000..a8f0da9 --- /dev/null +++ b/src/main/java/etunicorn/WebSecurityConfiguration.java @@ -0,0 +1,33 @@ +package etunicorn; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +/** + * Created by geoffrey on 29/01/17. + */ +@Configuration +@EnableWebSecurity +public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { + + @Autowired + private CustomUserDetailsService userDetailsService; + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(userDetailsService); + } + + @Override + @Bean + public AuthenticationManager authenticationManager() throws Exception { + return super.authenticationManager(); + } + + +} diff --git a/src/main/main.iml b/src/main/main.iml index 232395b..a7741cb 100644 --- a/src/main/main.iml +++ b/src/main/main.iml @@ -25,5 +25,7 @@ + + \ No newline at end of file -- libgit2 0.21.2