Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. package br.gov.serpro.security;
  2.  
  3. import java.io.Serializable;
  4. import java.security.Principal;
  5. import java.util.Collections;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.UUID;
  9.  
  10. import br.gov.frameworkdemoiselle.security.TokenManager;
  11.  
  12. public class AppTokenManager implements TokenManager {
  13.  
  14. private TokenStore store = new TokenStore();
  15.  
  16. @Override
  17. public String persist(Principal user) throws Exception {
  18. String token = store.get(user);
  19.  
  20. if (token == null) {
  21. token = store.put(user);
  22. }
  23.  
  24. return token;
  25. }
  26.  
  27. @Override
  28. public Principal load(String token) throws Exception {
  29. return store.get(token);
  30. }
  31.  
  32. public static class TokenStore implements Serializable {
  33.  
  34. private static final long serialVersionUID = 1L;
  35.  
  36. private Map<String, Principal> map = Collections.synchronizedMap(new HashMap<String, Principal>());
  37.  
  38. public String put(Principal user) {
  39. String token = UUID.randomUUID().toString();
  40.  
  41. if (map.containsValue(user)) {
  42. map.remove(token);
  43. }
  44.  
  45. map.put(token, user);
  46.  
  47. return token;
  48. }
  49.  
  50. public String get(Principal user) {
  51. String result = null;
  52.  
  53. if (map.containsValue(user)) {
  54. for (String key : map.keySet()) {
  55. if (map.get(key).equals(user)) {
  56. result = key;
  57. break;
  58. }
  59. }
  60. }
  61.  
  62. return result;
  63. }
  64.  
  65. public Principal get(String token) {
  66. return map.get(token);
  67. }
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement