- 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:
parent
206c5e958b
commit
a297641fa9
@ -14,6 +14,7 @@ import ovh.herisson.Clyde.Tables.User;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
@RestController
|
||||
@ -53,7 +54,16 @@ public class MockController {
|
||||
userRepo.saveAll(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()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ public class AuthenticatorService {
|
||||
if (user == null){return null;}
|
||||
if (!userService.checkPassword(user,password)){return null;}
|
||||
String token = tokenService.generateNewToken();
|
||||
tokenService.saveToken(new Token(user, token),expirationDate);
|
||||
tokenService.saveToken(new Token(user, token,expirationDate));
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,15 @@
|
||||
package ovh.herisson.Clyde.Services;
|
||||
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
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 ovh.herisson.Clyde.TokenCleaner;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
@Service
|
||||
public class TokenService {
|
||||
@ -37,9 +33,22 @@ public class TokenService {
|
||||
return tokenRepo.getByToken(token).getUser();
|
||||
}
|
||||
|
||||
public void saveToken(Token token, Date expirationDate){
|
||||
public void saveToken(Token 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);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@ -4,6 +4,8 @@ import jakarta.persistence.*;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import ovh.herisson.Clyde.Repositories.TokenRepository;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Entity
|
||||
public class Token {
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@ -14,10 +16,12 @@ public class Token {
|
||||
@JoinColumn(name ="Users")
|
||||
private User user;
|
||||
private String token;
|
||||
private Date expirationDate;
|
||||
|
||||
public Token(User user, String token){
|
||||
public Token(User user, String token, Date expirationDate){
|
||||
this.user = user;
|
||||
this.token = token;
|
||||
this.expirationDate = expirationDate;
|
||||
}
|
||||
|
||||
public Token(){}
|
||||
@ -40,4 +44,12 @@ public class Token {
|
||||
public void setToken(String data) {
|
||||
this.token = data;
|
||||
}
|
||||
|
||||
public void setExpirationDate(Date date){
|
||||
this.expirationDate = date;
|
||||
}
|
||||
|
||||
public Date getExpirationDate(){
|
||||
return expirationDate;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user