token问题
This commit is contained in:
parent
a375fb10a8
commit
c85f82f385
|
|
@ -122,6 +122,15 @@ public class LoginFilter implements Filter {
|
||||||
response.getWriter().flush();//流里边的缓存刷出
|
response.getWriter().flush();//流里边的缓存刷出
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
JSONObject jsonObject1 = TokenUtil.parseParamFromToken(token);
|
||||||
|
if (jsonObject1.containsKey("status")){
|
||||||
|
Result result = new Result(CodeEnum.TOKEN_EXPIRED);
|
||||||
|
String jsonString = JSONObject.toJSONString(result);
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(jsonString, JSONObject.class);
|
||||||
|
response.getWriter().print(jsonObject);
|
||||||
|
response.getWriter().flush();//流里边的缓存刷出
|
||||||
|
return;
|
||||||
|
}
|
||||||
chain.doFilter(req, resp);
|
chain.doFilter(req, resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import io.jsonwebtoken.Claims;
|
||||||
import io.jsonwebtoken.JwtBuilder;
|
import io.jsonwebtoken.JwtBuilder;
|
||||||
import io.jsonwebtoken.Jwts;
|
import io.jsonwebtoken.Jwts;
|
||||||
import io.jsonwebtoken.SignatureAlgorithm;
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
|
@ -18,6 +19,7 @@ import java.util.Map;
|
||||||
* @author admin
|
* @author admin
|
||||||
* @date 2020/8/6 13:04
|
* @date 2020/8/6 13:04
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class TokenUtil {
|
public class TokenUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -51,8 +53,8 @@ public class TokenUtil {
|
||||||
}
|
}
|
||||||
public static String generateJfToken(String openId,String userSign) throws Exception {
|
public static String generateJfToken(String openId,String userSign) throws Exception {
|
||||||
Map<String, Object> claims = new HashMap<>(1);
|
Map<String, Object> claims = new HashMap<>(1);
|
||||||
claims.put("openId",openId);
|
claims.put("openId",openId);
|
||||||
claims.put("userSign",userSign);
|
claims.put("userSign",userSign);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -66,7 +68,7 @@ public class TokenUtil {
|
||||||
private static String generateToken(Map<String, Object> claims) throws Exception {
|
private static String generateToken(Map<String, Object> claims) throws Exception {
|
||||||
return Jwts.builder()
|
return Jwts.builder()
|
||||||
.setClaims(claims)
|
.setClaims(claims)
|
||||||
.setExpiration(new Date(System.currentTimeMillis()+EXPIRE_DATE))
|
// .setExpiration(new Date(System.currentTimeMillis()+EXPIRE_DATE))
|
||||||
.setIssuedAt(new Date())
|
.setIssuedAt(new Date())
|
||||||
.signWith(SignatureAlgorithm.HS256,TOKEN_SECRET)
|
.signWith(SignatureAlgorithm.HS256,TOKEN_SECRET)
|
||||||
.compact();
|
.compact();
|
||||||
|
|
@ -106,20 +108,27 @@ public class TokenUtil {
|
||||||
* @return 用户Id
|
* @return 用户Id
|
||||||
*/
|
*/
|
||||||
public static JSONObject parseParamFromToken(final String token) {
|
public static JSONObject parseParamFromToken(final String token) {
|
||||||
Claims claims = Jwts.parser()
|
JSONObject jsonObject=new JSONObject();
|
||||||
.setSigningKey(TOKEN_SECRET)
|
try{
|
||||||
.parseClaimsJws(token)
|
Claims claims = Jwts.parser()
|
||||||
.getBody();
|
.setSigningKey(TOKEN_SECRET)
|
||||||
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(claims);
|
.parseClaimsJws(token)
|
||||||
|
.getBody();
|
||||||
|
jsonObject = (JSONObject) JSONObject.toJSON(claims);
|
||||||
|
}catch (Exception e){
|
||||||
|
jsonObject.put("status","-4");
|
||||||
|
log.info("token解析失败{}",e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
return jsonObject;
|
return jsonObject;
|
||||||
}
|
}
|
||||||
public static void main(String[] args){
|
// public static void main(String[] args){
|
||||||
System.out.println(refreshToken("eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE1OTY4Nzc5MjEsInN1YiI6ImRkZGRkIiwiaWF0IjoxNTk2Njk3OTIxfQ.lrg3KF9h9izbmyD2q5onqnZIKBqanWy9xCcroFpjxPKmJz6kz27G9lVlFpVanrL1I4SFf3Dz3q3Xu01DX2T_dw"));
|
// System.out.println(refreshToken("eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE1OTY4Nzc5MjEsInN1YiI6ImRkZGRkIiwiaWF0IjoxNTk2Njk3OTIxfQ.lrg3KF9h9izbmyD2q5onqnZIKBqanWy9xCcroFpjxPKmJz6kz27G9lVlFpVanrL1I4SFf3Dz3q3Xu01DX2T_dw"));
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
Calendar cld = Calendar.getInstance();
|
// Calendar cld = Calendar.getInstance();
|
||||||
cld.setTime(new Date());
|
// cld.setTime(new Date());
|
||||||
cld.set(Calendar.DATE, cld.get(Calendar.DATE)-1);
|
// cld.set(Calendar.DATE, cld.get(Calendar.DATE)-1);
|
||||||
System.out.println(sdf.format(cld.getTime()));
|
// System.out.println(sdf.format(cld.getTime()));
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue