123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package cn.gygxzc.envir.config.jwt
- import io.jsonwebtoken.Jwts
- import io.jsonwebtoken.SignatureAlgorithm
- import java.security.Key
- import java.util.*
- /**
- * Created by niantuo on 2018/9/21.
- * 签发jwt方法,
- */
- object JwtUtils {
- /**
- * 暂时如此,后期可以考虑ip,域名这些加强验证
- */
- fun sign(payload: JwtPayload, jwtConfig: JwtConfig): String {
- val calendar = Calendar.getInstance(Locale.CANADA)
- calendar.add(Calendar.HOUR_OF_DAY, jwtConfig.expired.toHours().toInt())
- return Jwts.builder()
- .setId(payload.id)
- .setSubject(payload.username)
- .setExpiration(calendar.time)
- .signWith(jwtConfig.key,SignatureAlgorithm.HS256)
- .compact()
- }
- /**
- * 解析出放进去的结果
- */
- @Throws(Exception::class)
- fun parseJwt(key: Key, jwtToken: String): JwtPayload {
- val parser = Jwts.parser()
- .setSigningKey(key)
- .parseClaimsJws(jwtToken)
- val id = parser.body.id
- val username = parser.body.subject
- return JwtPayload(id, username)
- }
- }
|