cookies done miam

This commit is contained in:
Parpaillax
2024-05-02 16:45:06 +02:00
parent e576d52dfb
commit 50c854d9b5
5 changed files with 9 additions and 42 deletions

View File

@@ -5,7 +5,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import fr.eni.enchere.security.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.http.HttpStatus;
@@ -37,17 +36,15 @@ public class InscriptionController {
@Autowired
private final UserService userService;
private LoginService loginService;
private PasswordEncoder passwordEncoder;
private EmailValidator emailValidator;
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.passwordEncoder = passwordEncoder;
this.emailValidator = emailValidator;
this.phoneValidator = phoneValidator;
this.loginService = loginService;
}
@GetMapping

View File

@@ -1,10 +1,6 @@
package fr.eni.enchere.controllers;
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.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
@@ -22,25 +18,11 @@ public class LoginController {
@GetMapping
public String login(Model modele) {
//Début cookie
String savedUsername = "";
String savedPassword = "";
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!authentication.getName().equals("anonymousUser")){
return "redirect:/enchere";
}
modele.addAttribute("savedUsername", savedUsername);
modele.addAttribute("savedPassword", savedPassword);
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";
}
}
}

View File

@@ -6,9 +6,10 @@ import org.springframework.security.core.userdetails.User;
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.Component;
import org.springframework.stereotype.Service;
@Component
@Service
public class LoginService implements UserDetailsService {
private UserRepository userRep;

View File

@@ -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();
// }
}

View File

@@ -4,13 +4,11 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
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.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import static javax.management.Query.and;
@Configuration
@EnableWebSecurity
@@ -27,6 +25,11 @@ public class WebSecurityConfig{
.formLogin((form) -> form
.loginPage("/login")
.defaultSuccessUrl("/enchere", true))
.rememberMe(rememberMe -> rememberMe
.key("secretKey")
.rememberMeParameter("remember-me")
.tokenValiditySeconds(604800)
.rememberMeCookieName("remember-me"))
.logout((logout) -> logout
.clearAuthentication(true).invalidateHttpSession(true)
.deleteCookies("JSESSIONID").logoutSuccessUrl("/enchere")