3 changed files with 117 additions and 2 deletions
@ -0,0 +1,41 @@ |
|||||
|
/** |
||||
|
* Copyright (c) 2018 人人开源 All rights reserved. |
||||
|
* |
||||
|
* https://www.renren.io
|
||||
|
* |
||||
|
* 版权所有,侵权必究! |
||||
|
*/ |
||||
|
|
||||
|
package com.elink.esua.epdc.commons.tools.security.jwt; |
||||
|
|
||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
|
||||
|
/** |
||||
|
* Jwt |
||||
|
* |
||||
|
* @author Mark sunlightcs@gmail.com |
||||
|
* @since 1.0.0 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
@ConfigurationProperties(prefix = "jwt") |
||||
|
public class JwtProperties { |
||||
|
private String secret; |
||||
|
private int expire; |
||||
|
|
||||
|
public String getSecret() { |
||||
|
return secret; |
||||
|
} |
||||
|
|
||||
|
public void setSecret(String secret) { |
||||
|
this.secret = secret; |
||||
|
} |
||||
|
|
||||
|
public int getExpire() { |
||||
|
return expire; |
||||
|
} |
||||
|
|
||||
|
public void setExpire(int expire) { |
||||
|
this.expire = expire; |
||||
|
} |
||||
|
} |
@ -0,0 +1,75 @@ |
|||||
|
/** |
||||
|
* Copyright (c) 2018 人人开源 All rights reserved. |
||||
|
* |
||||
|
* https://www.renren.io
|
||||
|
* |
||||
|
* 版权所有,侵权必究! |
||||
|
*/ |
||||
|
|
||||
|
package com.elink.esua.epdc.commons.tools.security.jwt; |
||||
|
|
||||
|
import io.jsonwebtoken.Claims; |
||||
|
import io.jsonwebtoken.Jwts; |
||||
|
import io.jsonwebtoken.SignatureAlgorithm; |
||||
|
import org.joda.time.DateTime; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.util.Date; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* Jwt工具类 |
||||
|
* |
||||
|
* @author Mark sunlightcs@gmail.com |
||||
|
* @since 1.0.0 |
||||
|
*/ |
||||
|
@Component |
||||
|
public class JwtUtils { |
||||
|
private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class); |
||||
|
|
||||
|
@Autowired |
||||
|
private JwtProperties jwtProperties; |
||||
|
|
||||
|
/** |
||||
|
* 生成jwt token |
||||
|
*/ |
||||
|
public String generateToken(Long userId) { |
||||
|
return Jwts.builder() |
||||
|
.setHeaderParam("typ", "JWT") |
||||
|
.setSubject(userId+"") |
||||
|
.setIssuedAt(new Date()) |
||||
|
.setExpiration(DateTime.now().plusSeconds(jwtProperties.getExpire()).toDate()) |
||||
|
.signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret()) |
||||
|
.compact(); |
||||
|
} |
||||
|
|
||||
|
public Claims getClaimByToken(String token) { |
||||
|
try { |
||||
|
return Jwts.parser() |
||||
|
.setSigningKey(jwtProperties.getSecret()) |
||||
|
.parseClaimsJws(token) |
||||
|
.getBody(); |
||||
|
}catch (Exception e){ |
||||
|
logger.debug("validate is token error, token = " + token, e); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
public static String generateEpmetToken(Map<String,Object> claims, String Secret){ |
||||
|
return Jwts.builder() |
||||
|
.setHeaderParam("typ", "JWT") |
||||
|
.setClaims(claims) |
||||
|
.signWith(SignatureAlgorithm.HS512,Secret) |
||||
|
.compact(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* token是否过期 |
||||
|
* @return true:过期 |
||||
|
*/ |
||||
|
public boolean isTokenExpired(Date expiration) { |
||||
|
return expiration.before(new Date()); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue