Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.kpfu.driving_school.service.impl;
- import io.jsonwebtoken.JwtBuilder;
- import io.jsonwebtoken.Jwts;
- import io.jsonwebtoken.SignatureAlgorithm;
- import io.jsonwebtoken.impl.crypto.MacProvider;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.core.userdetails.User;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.userdetails.UserDetailsService;
- import org.springframework.stereotype.Service;
- import ru.kpfu.driving_school.model.Credential;
- import ru.kpfu.driving_school.repository.CredentialRepository;
- import ru.kpfu.driving_school.service.TokenService;
- import java.util.*;
- @Service
- public class TokenServiceImpl implements TokenService {
- @Autowired
- CredentialRepository credentialRepository;
- @Override
- public String getToken(String username, String password) throws Exception {
- if (username == null || password == null)
- return null;
- Credential credential = credentialRepository.findOneByLogin(username);
- Map<String, Object> tokenData = new HashMap<>();
- if (password.equals(credential.getPassword())) {
- tokenData.put("clientType", "user");
- tokenData.put("userID", credential.getId().toString());
- tokenData.put("username", credential.getLogin());
- tokenData.put("token_create_date", new Date().getTime());
- Calendar calendar = Calendar.getInstance();
- calendar.add(Calendar.YEAR, 100);
- tokenData.put("token_expiration_date", calendar.getTime());
- JwtBuilder jwtBuilder = Jwts.builder();
- jwtBuilder.setExpiration(calendar.getTime());
- jwtBuilder.setClaims(tokenData);
- String key = "salth";
- String token = jwtBuilder.signWith(SignatureAlgorithm.HS512, key).compact();
- return token;
- } else {
- throw new Exception("Authentication error");
- }
- }
- }
Add Comment
Please, Sign In to add comment