/**
* created by qhong on 2018/6/7 15:34
* 标注该注解的,就不需要登录
**/
@target({elementtype.method,elementtype.type})
@retention(retentionpolicy.runtime)
@documented
public @interface authignore {
}
loginuser:
@target(elementtype.parameter)
@retention(retentionpolicy.runtime)
public @interface loginuser {
}
jwtutil:
@configurationproperties(prefix = "jwt")
@component
public class jwtutils {
private logger logger = loggerfactory.getlogger(getclass());
private string secret;
private long expire;
private string header;
/**
* 生成jwt token
*/
public string generatetoken(long userid) {
date nowdate = new date();
//过期时间
date expiredate = new date(nowdate.gettime() + expire * 1000);
return jwts.builder()
.setheaderparam("typ", "jwt")
.setsubject(userid+"")
.setissuedat(nowdate)
.setexpiration(expiredate)
.signwith(io.jsonwebtoken.signaturealgorithm.hs512, secret)
.compact();
}
public claims getclaimbytoken(string token) {
try {
return jwts.parser()
.setsigningkey(secret)
.parseclaimsjws(token)
.getbody();
}catch (exception e){
logger.debug("validate is token error ", e);
return null;
}
}
/**
* token是否过期
* @return true:过期
*/
public boolean istokenexpired(date expiration) {
return expiration.before(new date());
}
public string getsecret() {
return secret;
}
public void setsecret(string secret) {
this.secret = secret;
}
public long getexpire() {
return expire;
}
public void setexpire(long expire) {
this.expire = expire;
}
public string getheader() {
return header;
}
public void setheader(string header) {
this.header = header;
}
}