Commit dabc782190fc3f8a5020b8a6a5bb24f699c4b9ac
1 parent
3064f583
Oups
Showing
2 changed files
with
70 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,60 @@ | @@ -0,0 +1,60 @@ | ||
1 | +package etunicorn; | ||
2 | + | ||
3 | +import javax.persistence.Entity; | ||
4 | +import javax.persistence.Id; | ||
5 | +import javax.persistence.ManyToOne; | ||
6 | +import java.math.BigInteger; | ||
7 | +import java.security.SecureRandom; | ||
8 | +import java.util.Date; | ||
9 | + | ||
10 | +/** | ||
11 | + * Created by geoffrey on 04/02/17. | ||
12 | + */ | ||
13 | +@Entity | ||
14 | +public class Session { | ||
15 | + | ||
16 | + // Durée par défaut d'une session en secondes | ||
17 | + private static final int SESSION_DURATION = 10 * 60; | ||
18 | + private static SecureRandom random = new SecureRandom(); | ||
19 | + @ManyToOne | ||
20 | + private Personne personne; | ||
21 | + @Id | ||
22 | + private String token; | ||
23 | + // TODO Vérifier si c'est bien initialisé qu'une seule fois par éxecution car c'est lourd à initialiser | ||
24 | + private Date validity; | ||
25 | + | ||
26 | + | ||
27 | + public Session() { | ||
28 | + } | ||
29 | + | ||
30 | + public Session(Personne personne) { | ||
31 | + this.personne = personne; | ||
32 | + // From http://stackoverflow.com/a/41156 | ||
33 | + this.token = new BigInteger(130, random).toString(32); | ||
34 | + this.validity = new Date(new Date().getTime() + SESSION_DURATION * 1000); | ||
35 | + } | ||
36 | + | ||
37 | + public Personne getPersonne() { | ||
38 | + return personne; | ||
39 | + } | ||
40 | + | ||
41 | + public void setPersonne(Personne personne) { | ||
42 | + this.personne = personne; | ||
43 | + } | ||
44 | + | ||
45 | + public String getToken() { | ||
46 | + return token; | ||
47 | + } | ||
48 | + | ||
49 | + public void setToken(String token) { | ||
50 | + this.token = token; | ||
51 | + } | ||
52 | + | ||
53 | + public Date getValidity() { | ||
54 | + return validity; | ||
55 | + } | ||
56 | + | ||
57 | + public void setValidity(Date validity) { | ||
58 | + this.validity = validity; | ||
59 | + } | ||
60 | +} |
@@ -0,0 +1,10 @@ | @@ -0,0 +1,10 @@ | ||
1 | +package etunicorn; | ||
2 | + | ||
3 | +import org.springframework.data.repository.CrudRepository; | ||
4 | + | ||
5 | +/** | ||
6 | + * Created by geoffrey on 04/02/17. | ||
7 | + */ | ||
8 | +public interface SessionRepository extends CrudRepository<Session, Long> { | ||
9 | + Session findByToken(String token); | ||
10 | +} |