-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
升级nutzboot至2.3.5.v20190516;添加JWT验证工具类
- Loading branch information
Showing
10 changed files
with
192 additions
and
601 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
src/main/java/io/nutz/nutzsite/common/utils/JWTUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package io.nutz.nutzsite.common.utils; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.JwtBuilder; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import io.jsonwebtoken.security.Keys; | ||
import org.nutz.mvc.Mvcs; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.io.*; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.security.Key; | ||
import java.util.Date; | ||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* jwt验证工具类 | ||
* @author Hamming_Yu on 2018/11/13. | ||
*/ | ||
public class JWTUtil { | ||
private static Logger log = LoggerFactory.getLogger(JWTUtil.class); | ||
|
||
//We will sign our JWT with our ApiKey secret | ||
private static Key key; | ||
private static String issuer="nutzsite"; | ||
|
||
/** | ||
* 一天 | ||
*/ | ||
private static long tokenValidityInSeconds = 1800L; | ||
/** | ||
* 一个月失效 | ||
*/ | ||
private static long tokenValidityInSecondsForRememberMe = 2592000L; | ||
|
||
static { | ||
//初始化api.key 文件存放位置 | ||
Path fpath= Paths.get("api.key"); | ||
//创建文件 | ||
if(!Files.exists(fpath)) { | ||
try { | ||
Files.createFile(fpath); | ||
// Files.createDirectory(fpath); | ||
key = Keys.secretKeyFor(SignatureAlgorithm.HS256); | ||
try ( ObjectOutputStream keyOut = new ObjectOutputStream(new FileOutputStream(fpath.toFile()))){ | ||
keyOut.writeObject(key); | ||
keyOut.close(); | ||
} catch (IOException e) { | ||
log.debug(e.getMessage()); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
}else { | ||
try (ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(fpath.toFile()))){ | ||
key = (Key) keyIn.readObject(); | ||
keyIn.close(); | ||
} catch (IOException e) { | ||
log.debug(e.getMessage()); | ||
} catch (ClassNotFoundException e) { | ||
log.debug(e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 创建token | ||
* @param id | ||
* @return | ||
*/ | ||
public static String createJWT(String id) { | ||
// long nowMillis = System.currentTimeMillis(); | ||
Date exp = DateUtils.addMonths(new Date(),1) ; | ||
//Let's set the JWT Claims | ||
JwtBuilder builder = Jwts.builder().setId(id) | ||
.setIssuedAt(new Date()) | ||
.setSubject(id) | ||
.setIssuer(issuer) | ||
.signWith(key); | ||
builder.setExpiration(exp); | ||
|
||
//Builds the JWT and serializes it to a compact, URL-safe string | ||
return builder.compact(); | ||
} | ||
|
||
/** | ||
* 验证token | ||
* @param token | ||
* @return | ||
*/ | ||
public static boolean verifyToken(String token) { | ||
try { | ||
Claims claims = Jwts.parser() | ||
.setSigningKey(key) | ||
.parseClaimsJws(token).getBody(); | ||
if(!issuer.equals(claims.getIssuer()) && | ||
!claims.getIssuer().equals( claims.getSubject() ) ){ | ||
return false; | ||
} | ||
return true; | ||
} catch (Exception e) { | ||
log.debug(e.getMessage()); | ||
// e.printStackTrace(); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* 获取ID | ||
* @return | ||
*/ | ||
public static String getId() { | ||
HttpServletRequest request = Mvcs.getReq(); | ||
Map<String, String> map = new HashMap<String, String>(); | ||
Enumeration headerNames = request.getHeaderNames(); | ||
while (headerNames.hasMoreElements()) { | ||
String key = (String) headerNames.nextElement(); | ||
String value = request.getHeader(key); | ||
map.put(key, value); | ||
} | ||
try{ | ||
String token=map.get("authorization"); | ||
if(verifyToken(token)){ | ||
Claims claims = Jwts.parser() | ||
.setSigningKey(key) | ||
.parseClaimsJws(token).getBody(); | ||
return claims.getId(); | ||
} | ||
}catch (Exception e){ | ||
log.debug(e.getMessage()); | ||
e.printStackTrace(); | ||
|
||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Sample method to validate and read the JWT | ||
* @param jwt | ||
*/ | ||
public static void parseJWT(String jwt) { | ||
//This line will throw an exception if it is not a signed JWS (as expected) | ||
Claims claims = Jwts.parser() | ||
.setSigningKey(key) | ||
.parseClaimsJws(jwt).getBody(); | ||
// System.out.println("ID: " + claims.getId()); | ||
// System.out.println("Subject: " + claims.getSubject()); | ||
// System.out.println("Issuer: " + claims.getIssuer()); | ||
// System.out.println("Expiration: " + claims.getExpiration()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.