jwt service

This commit is contained in:
aisensiy 2017-08-08 11:01:13 +08:00
parent ee946e371b
commit 197130648c
4 changed files with 100 additions and 0 deletions

View File

@ -29,6 +29,7 @@ dependencies {
compile('org.springframework.boot:spring-boot-starter-hateoas') compile('org.springframework.boot:spring-boot-starter-hateoas')
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0') compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0')
compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-web')
compile('io.jsonwebtoken:jjwt:0.7.0')
compileOnly('org.projectlombok:lombok') compileOnly('org.projectlombok:lombok')
runtime('com.h2database:h2') runtime('com.h2database:h2')
testCompile 'io.rest-assured:rest-assured:3.0.2' testCompile 'io.rest-assured:rest-assured:3.0.2'

View File

@ -2,6 +2,10 @@ package io.spring.application;
import io.spring.application.user.UserData; import io.spring.application.user.UserData;
import java.util.Optional;
public interface JwtService { public interface JwtService {
String toToken(UserData userData); String toToken(UserData userData);
Optional<String> getSubFromToken(String token);
} }

View File

@ -0,0 +1,49 @@
package io.spring.infrastructure.service;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.SignatureException;
import io.spring.application.JwtService;
import io.spring.application.user.UserData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Optional;
@Service
public class DefaultJwtService implements JwtService {
private String secret;
private int sessionTime;
public DefaultJwtService(@Value("${jwt.secret}") String secret,
@Value("${jwt.sessionTime}") int sessionTime) {
this.secret = secret;
this.sessionTime = sessionTime;
}
@Override
public String toToken(UserData userData) {
return Jwts.builder()
.setSubject(userData.getUsername())
.setExpiration(expireTimeFromNow())
.signWith(SignatureAlgorithm.HS512, secret)
.compact();
}
@Override
public Optional<String> getSubFromToken(String token) {
try {
Jws<Claims> claimsJws = Jwts.parser().setSigningKey(secret).parseClaimsJws(token);
return Optional.ofNullable(claimsJws.getBody().getSubject());
} catch (Exception e) {
return Optional.empty();
}
}
private Date expireTimeFromNow() {
return new Date(System.currentTimeMillis() + sessionTime * 1000);
}
}

View File

@ -0,0 +1,46 @@
package io.spring.infrastructure.service;
import io.spring.application.JwtService;
import io.spring.application.user.UserData;
import org.junit.Before;
import org.junit.Test;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.*;
public class DefaultJwtServiceTest {
private JwtService jwtService;
@Before
public void setUp() throws Exception {
jwtService = new DefaultJwtService("123123", 3600);
}
@Test
public void should_generate_and_parse_token() throws Exception {
String username = "aisensiy";
UserData userData = new UserData("aisensiy@163.com", username, "", "");
String token = jwtService.toToken(userData);
assertThat(token, notNullValue());
Optional<String> optional = jwtService.getSubFromToken(token);
assertThat(optional.isPresent(), is(true));
assertThat(optional.get(), is(username));
}
@Test
public void should_get_null_with_wrong_jwt() throws Exception {
Optional<String> optional = jwtService.getSubFromToken("123");
assertThat(optional.isPresent(), is(false));
}
@Test
public void should_get_null_with_expired_jwt() throws Exception {
String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhaXNlbnNpeSIsImV4cCI6MTUwMjE2MTIwNH0.SJB-U60WzxLYNomqLo4G3v3LzFxJKuVrIud8D8Lz3-mgpo9pN1i7C8ikU_jQPJGm8HsC1CquGMI-rSuM7j6LDA";
assertThat(jwtService.getSubFromToken(token).isPresent(), is(false));
}
}