cookies done miam
This commit is contained in:
@@ -5,7 +5,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.google.i18n.phonenumbers.NumberParseException;
|
import com.google.i18n.phonenumbers.NumberParseException;
|
||||||
import com.google.i18n.phonenumbers.PhoneNumberUtil;
|
import com.google.i18n.phonenumbers.PhoneNumberUtil;
|
||||||
import com.google.i18n.phonenumbers.Phonenumber;
|
import com.google.i18n.phonenumbers.Phonenumber;
|
||||||
import fr.eni.enchere.security.LoginService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.apache.commons.validator.routines.EmailValidator;
|
import org.apache.commons.validator.routines.EmailValidator;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
@@ -37,17 +36,15 @@ public class InscriptionController {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private LoginService loginService;
|
|
||||||
private PasswordEncoder passwordEncoder;
|
private PasswordEncoder passwordEncoder;
|
||||||
private EmailValidator emailValidator;
|
private EmailValidator emailValidator;
|
||||||
private PhoneNumberUtil phoneValidator;
|
private PhoneNumberUtil phoneValidator;
|
||||||
|
|
||||||
public InscriptionController(UserService userService, PasswordEncoder passwordEncoder, EmailValidator emailValidator, PhoneNumberUtil phoneValidator, LoginService loginService) {
|
public InscriptionController(UserService userService, PasswordEncoder passwordEncoder, EmailValidator emailValidator, PhoneNumberUtil phoneValidator) {
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
this.passwordEncoder = passwordEncoder;
|
this.passwordEncoder = passwordEncoder;
|
||||||
this.emailValidator = emailValidator;
|
this.emailValidator = emailValidator;
|
||||||
this.phoneValidator = phoneValidator;
|
this.phoneValidator = phoneValidator;
|
||||||
this.loginService = loginService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
package fr.eni.enchere.controllers;
|
package fr.eni.enchere.controllers;
|
||||||
|
|
||||||
import fr.eni.enchere.bll.UserService;
|
import fr.eni.enchere.bll.UserService;
|
||||||
import fr.eni.enchere.bo.UserProfil;
|
|
||||||
import jakarta.servlet.http.Cookie;
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
@@ -22,25 +18,11 @@ public class LoginController {
|
|||||||
@GetMapping
|
@GetMapping
|
||||||
public String login(Model modele) {
|
public String login(Model modele) {
|
||||||
//Début cookie
|
//Début cookie
|
||||||
String savedUsername = "";
|
|
||||||
String savedPassword = "";
|
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
if (!authentication.getName().equals("anonymousUser")){
|
if (!authentication.getName().equals("anonymousUser")){
|
||||||
return "redirect:/enchere";
|
return "redirect:/enchere";
|
||||||
}
|
}
|
||||||
modele.addAttribute("savedUsername", savedUsername);
|
|
||||||
modele.addAttribute("savedPassword", savedPassword);
|
|
||||||
return "security/login";
|
return "security/login";
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
|
||||||
public String login(@RequestParam("username") String username, @RequestParam("password") String password) {
|
|
||||||
UserProfil user = userService.utilisateurByName(username);
|
|
||||||
if (user != null && user.getPassword().equals(password)) {
|
|
||||||
return "redirect:/enchere";
|
|
||||||
} else {
|
|
||||||
return "redirect:/login?error";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,10 @@ import org.springframework.security.core.userdetails.User;
|
|||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Component
|
|
||||||
|
@Service
|
||||||
public class LoginService implements UserDetailsService {
|
public class LoginService implements UserDetailsService {
|
||||||
|
|
||||||
private UserRepository userRep;
|
private UserRepository userRep;
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
package fr.eni.enchere.security;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class SecurityConfig {
|
|
||||||
|
|
||||||
// @Bean
|
|
||||||
// public PasswordEncoder passwordEncoder() {
|
|
||||||
// return new BCryptPasswordEncoder();
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -4,13 +4,11 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
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.EnableWebSecurity;
|
||||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||||
|
|
||||||
import static javax.management.Query.and;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@@ -27,6 +25,11 @@ public class WebSecurityConfig{
|
|||||||
.formLogin((form) -> form
|
.formLogin((form) -> form
|
||||||
.loginPage("/login")
|
.loginPage("/login")
|
||||||
.defaultSuccessUrl("/enchere", true))
|
.defaultSuccessUrl("/enchere", true))
|
||||||
|
.rememberMe(rememberMe -> rememberMe
|
||||||
|
.key("secretKey")
|
||||||
|
.rememberMeParameter("remember-me")
|
||||||
|
.tokenValiditySeconds(604800)
|
||||||
|
.rememberMeCookieName("remember-me"))
|
||||||
.logout((logout) -> logout
|
.logout((logout) -> logout
|
||||||
.clearAuthentication(true).invalidateHttpSession(true)
|
.clearAuthentication(true).invalidateHttpSession(true)
|
||||||
.deleteCookies("JSESSIONID").logoutSuccessUrl("/enchere")
|
.deleteCookies("JSESSIONID").logoutSuccessUrl("/enchere")
|
||||||
|
|||||||
Reference in New Issue
Block a user