Guest User

Untitled

a guest
Apr 14th, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. package ru.kpfu.driving_school.service.impl;
  2.  
  3. import io.jsonwebtoken.JwtBuilder;
  4. import io.jsonwebtoken.Jwts;
  5. import io.jsonwebtoken.SignatureAlgorithm;
  6. import io.jsonwebtoken.impl.crypto.MacProvider;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.security.core.userdetails.User;
  9. import org.springframework.security.core.GrantedAuthority;
  10. import org.springframework.security.core.userdetails.UserDetailsService;
  11. import org.springframework.stereotype.Service;
  12. import ru.kpfu.driving_school.model.Credential;
  13. import ru.kpfu.driving_school.repository.CredentialRepository;
  14. import ru.kpfu.driving_school.service.TokenService;
  15.  
  16. import java.util.*;
  17.  
  18. @Service
  19. public class TokenServiceImpl implements TokenService {
  20.  
  21. @Autowired
  22. CredentialRepository credentialRepository;
  23.  
  24. @Override
  25. public String getToken(String username, String password) throws Exception {
  26. if (username == null || password == null)
  27. return null;
  28. Credential credential = credentialRepository.findOneByLogin(username);
  29. Map<String, Object> tokenData = new HashMap<>();
  30. if (password.equals(credential.getPassword())) {
  31. tokenData.put("clientType", "user");
  32. tokenData.put("userID", credential.getId().toString());
  33. tokenData.put("username", credential.getLogin());
  34. tokenData.put("token_create_date", new Date().getTime());
  35. Calendar calendar = Calendar.getInstance();
  36. calendar.add(Calendar.YEAR, 100);
  37. tokenData.put("token_expiration_date", calendar.getTime());
  38. JwtBuilder jwtBuilder = Jwts.builder();
  39. jwtBuilder.setExpiration(calendar.getTime());
  40. jwtBuilder.setClaims(tokenData);
  41. String key = "salth";
  42. String token = jwtBuilder.signWith(SignatureAlgorithm.HS512, key).compact();
  43.  
  44. return token;
  45. } else {
  46. throw new Exception("Authentication error");
  47. }
  48. }
  49.  
  50. }
Add Comment
Please, Sign In to add comment