-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
55 changed files
with
2,892 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
HELP.md | ||
target/ | ||
logs/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/com/openclassrooms/moneytransfersystem/configuration/CustomUserDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.openclassrooms.moneytransfersystem.configuration; | ||
|
||
import com.openclassrooms.moneytransfersystem.model.User; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import java.util.Collection; | ||
|
||
public class CustomUserDetails implements UserDetails { | ||
|
||
private User user; | ||
|
||
public CustomUserDetails(User user) { | ||
|
||
this.user = user; | ||
} | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
|
||
return user.getPassword(); | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
|
||
return user.getEmail(); | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
|
||
return true; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...n/java/com/openclassrooms/moneytransfersystem/configuration/CustomUserDetailsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.openclassrooms.moneytransfersystem.configuration; | ||
|
||
import com.openclassrooms.moneytransfersystem.dao.UserRepository; | ||
import com.openclassrooms.moneytransfersystem.model.User; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
|
||
public class CustomUserDetailsService implements UserDetailsService { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
|
||
User user = userRepository.findByEmail(username); | ||
if (user == null) { | ||
throw new UsernameNotFoundException("[loadUserByUsername] user not found: " + username); | ||
} | ||
|
||
return new CustomUserDetails(user); | ||
} | ||
} |
62 changes: 46 additions & 16 deletions
62
src/main/java/com/openclassrooms/moneytransfersystem/configuration/SpringSecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,70 @@ | ||
package com.openclassrooms.moneytransfersystem.configuration; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
import javax.sql.DataSource; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
||
@Override | ||
protected void configure(AuthenticationManagerBuilder auth) throws Exception { | ||
@Autowired | ||
private DataSource dataSource; | ||
|
||
auth.inMemoryAuthentication() | ||
.withUser("[email protected]") | ||
.password(passwordEncoder().encode("jamesbond")) | ||
.roles("CLIENT"); | ||
@Bean | ||
public UserDetailsService userDetailsService() { | ||
|
||
return new CustomUserDetailsService(); | ||
} | ||
|
||
@Override | ||
public void configure(HttpSecurity http) throws Exception { | ||
@Bean | ||
public BCryptPasswordEncoder passwordEncoder() { | ||
|
||
http.authorizeRequests() | ||
.antMatchers("/compte").hasRole("CLIENT") | ||
.anyRequest().authenticated() | ||
.and() | ||
.formLogin(); | ||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
public DaoAuthenticationProvider authenticationProvider() { | ||
|
||
return new BCryptPasswordEncoder(); | ||
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); | ||
authProvider.setUserDetailsService(userDetailsService()); | ||
authProvider.setPasswordEncoder(passwordEncoder()); | ||
|
||
return authProvider; | ||
} | ||
|
||
@Override | ||
protected void configure(AuthenticationManagerBuilder auth) throws Exception { | ||
|
||
auth.authenticationProvider(authenticationProvider()); | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
|
||
http.httpBasic() | ||
.and() | ||
.authorizeRequests() | ||
.antMatchers("/").permitAll() | ||
.antMatchers("/app") | ||
.authenticated() | ||
.and() | ||
.formLogin() | ||
.defaultSuccessUrl("/app") | ||
.usernameParameter("email") | ||
.permitAll() | ||
.and() | ||
.logout().logoutSuccessUrl("/").permitAll() | ||
.and() | ||
.csrf().disable(); // Enabling Postman POST requests | ||
} | ||
} |
Oops, something went wrong.