- Ajout de la date d'expiration dans la table token

- Suppression de TokenCleaner et déplacement de la fonction de suppression des token dans Token Service
- Implémentation fonctionnelle de la nouvelle stratégie de suppression des tokens :)
This commit is contained in:
LeoMoulin 2024-03-08 17:45:16 +01:00
parent 206c5e958b
commit a297641fa9
5 changed files with 41 additions and 40 deletions

View File

@ -14,6 +14,7 @@ import ovh.herisson.Clyde.Tables.User;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
@RestController @RestController
@ -53,7 +54,16 @@ public class MockController {
userRepo.saveAll(mockUsers); userRepo.saveAll(mockUsers);
for (User user: mockUsers){ for (User user: mockUsers){
tokenService.saveToken(new Token(user,user.getPassword()), null); //Petit test pour être sur de delete les bons tokens
if (user.getLastName().equals("brine")){
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DAY_OF_WEEK, 1);
tokenService.saveToken(new Token(user,user.getPassword(), c.getTime()));
}else{
tokenService.saveToken(new Token(user,user.getPassword(), new Date()));
}
} }
} }

View File

@ -27,7 +27,7 @@ public class AuthenticatorService {
if (user == null){return null;} if (user == null){return null;}
if (!userService.checkPassword(user,password)){return null;} if (!userService.checkPassword(user,password)){return null;}
String token = tokenService.generateNewToken(); String token = tokenService.generateNewToken();
tokenService.saveToken(new Token(user, token),expirationDate); tokenService.saveToken(new Token(user, token,expirationDate));
return token; return token;
} }
} }

View File

@ -1,19 +1,15 @@
package ovh.herisson.Clyde.Services; package ovh.herisson.Clyde.Services;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.TokenRepository; import ovh.herisson.Clyde.Repositories.TokenRepository;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.Token;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
import ovh.herisson.Clyde.TokenCleaner;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
@Service @Service
public class TokenService { public class TokenService {
@ -37,9 +33,22 @@ public class TokenService {
return tokenRepo.getByToken(token).getUser(); return tokenRepo.getByToken(token).getUser();
} }
public void saveToken(Token token, Date expirationDate){ public void saveToken(Token token){
tokenRepo.save(token); tokenRepo.save(token);
TokenCleaner cleaner = new TokenCleaner(token, tokenRepo);
} }
//Tous les jours a minuit
@Scheduled(cron = "0 0 0 * * ?")
public void autoDeleteToken() {
for (Token t: tokenRepo.findAll()){
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
Calendar cal2 = Calendar.getInstance();
cal2.setTime(t.getExpirationDate());
if (cal.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)){
tokenRepo.delete(t);
}
}
};
} }

View File

@ -4,6 +4,8 @@ import jakarta.persistence.*;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import ovh.herisson.Clyde.Repositories.TokenRepository; import ovh.herisson.Clyde.Repositories.TokenRepository;
import java.util.Date;
@Entity @Entity
public class Token { public class Token {
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
@ -14,10 +16,12 @@ public class Token {
@JoinColumn(name ="Users") @JoinColumn(name ="Users")
private User user; private User user;
private String token; private String token;
private Date expirationDate;
public Token(User user, String token){ public Token(User user, String token, Date expirationDate){
this.user = user; this.user = user;
this.token = token; this.token = token;
this.expirationDate = expirationDate;
} }
public Token(){} public Token(){}
@ -40,4 +44,12 @@ public class Token {
public void setToken(String data) { public void setToken(String data) {
this.token = data; this.token = data;
} }
public void setExpirationDate(Date date){
this.expirationDate = date;
}
public Date getExpirationDate(){
return expirationDate;
}
} }

View File

@ -1,30 +0,0 @@
package ovh.herisson.Clyde;
import org.springframework.scheduling.annotation.Scheduled;
import ovh.herisson.Clyde.Repositories.TokenRepository;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.Token;
import ovh.herisson.Clyde.Tables.User;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TokenCleaner {
private Token token;
private TokenRepository tokenRepo;
public TokenCleaner(Token token, TokenRepository tokenRepo){
this.token = token;
this.tokenRepo = tokenRepo;
Timer timer = new Timer();
timer.schedule(autoDeleteTokenTask, 30000);
}
TimerTask autoDeleteTokenTask = new TimerTask() {
public void run() {
tokenRepo.delete(token);
}
};
}