Java登錄功能實現(xiàn)token生成與驗證
更新時間:2021年12月01日 16:51:03 作者:穆晟銘
這篇文章介紹了Java登錄功能實現(xiàn)token生成與驗證,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
一、token與cookie相比較的優(yōu)勢
- 1、支持跨域訪問,將token置于請求頭中,而cookie是不支持跨域訪問的;
- 2、無狀態(tài)化,服務(wù)端無需存儲token,只需要驗證token信息是否正確即可,而session需要在服務(wù)端存儲,一般是通過cookie中的sessionID在服務(wù)端查找對應(yīng)的session;
- 3、無需綁定到一個特殊的身份驗證方案(傳統(tǒng)的用戶名密碼登陸),只需要生成的token是符合我們預(yù)期設(shè)定的即可;
- 4、更適用于移動端(Android,iOS,小程序等等),像這種原生平臺不支持cookie,比如說微信小程序,每一次請求都是一次會話,當(dāng)然我們可以每次去手動為他添加cookie,詳情請查看博主另一篇博客;
- 5、避免CSRF跨站偽造攻擊,還是因為不依賴cookie;
二、基于JWT的token認(rèn)證實現(xiàn)
JWT:JSON Web Token,其實token就是一段字符串,由三部分組成:Header,Payload,Signature
1、引入依賴
<dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.8.2</version> </dependency>
2、設(shè)置密鑰和生存時間
//設(shè)置過期時間 private static final long EXPIRE_DATE=30*60*100000; //token秘鑰 private static final String TOKEN_SECRET = "ZCEQIUBFKSJBFJH2020BQWE";
3、實現(xiàn)簽名方法
public static String token (String username,String password){ String token = ""; try { //過期時間 Date date = new Date(System.currentTimeMillis()+EXPIRE_DATE); //秘鑰及加密算法 Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET); //設(shè)置頭部信息 Map<String,Object> header = new HashMap<>(); header.put("typ","JWT"); header.put("alg","HS256"); //攜帶username,password信息,生成簽名 token = JWT.create() .withHeader(header) .withClaim("username",username) .withClaim("password",password).withExpiresAt(date) .sign(algorithm); }catch (Exception e){ e.printStackTrace(); return null; } return token; }
4、驗證token
public static boolean verify(String token){ /** * @desc 驗證token,通過返回true * @create 2019/1/18/018 9:39 * @params [token]需要校驗的串 **/ try { Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET); JWTVerifier verifier = JWT.require(algorithm).build(); DecodedJWT jwt = verifier.verify(token); return true; }catch (Exception e){ e.printStackTrace(); return false; } }
5、測試
直接用生成的token去驗證,成功
public static void main(String[] args) { String username ="zhangsan"; String password = "123"; String token = token(username,password); System.out.println(token); boolean b = verify(token); System.out.println(b); }
三、完整的Token工具類代碼
package xxx.utils; //你的包 import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; import com.auth0.jwt.interfaces.DecodedJWT; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @desc 使用token驗證用戶是否登錄 * @author zm **/ public class TokenUtils { //設(shè)置過期時間 private static final long EXPIRE_DATE=30*60*100000; //token秘鑰 private static final String TOKEN_SECRET = "ZCfasfhuaUUHufguGuwu2020BQWE"; public static String token (String username,String password){ String token = ""; try { //過期時間 Date date = new Date(System.currentTimeMillis()+EXPIRE_DATE); //秘鑰及加密算法 Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET); //設(shè)置頭部信息 Map<String,Object> header = new HashMap<>(); header.put("typ","JWT"); header.put("alg","HS256"); //攜帶username,password信息,生成簽名 token = JWT.create() .withHeader(header) .withClaim("username",username) .withClaim("password",password).withExpiresAt(date) .sign(algorithm); }catch (Exception e){ e.printStackTrace(); return null; } return token; } public static boolean verify(String token){ /** * @desc 驗證token,通過返回true * @params [token]需要校驗的串 **/ try { Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET); JWTVerifier verifier = JWT.require(algorithm).build(); DecodedJWT jwt = verifier.verify(token); return true; }catch (Exception e){ e.printStackTrace(); return false; } } public static void main(String[] args) { String username ="zhangsan"; String password = "123"; String token = token(username,password); System.out.println(token); boolean b = verify("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXNzd22yZCI6IjEyMyIsImV4cCI6MTU3ODE5NzQxMywidXNlcm5hbWUiOiJ6aGFuZ3NhbiJ9.IyTZT0tISQQZhGhsNuaqHGV8LD7idjUYjn3MGbulmJg"); System.out.println(b); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java深入了解數(shù)據(jù)結(jié)構(gòu)之優(yōu)先級隊列(堆)
普通的隊列是一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),元素在隊列尾追加,而從隊列頭刪除。在優(yōu)先隊列中,元素被賦予優(yōu)先級。當(dāng)訪問元素時,具有最高優(yōu)先級的元素最先刪除。優(yōu)先隊列具有最高級先出 (first in, largest out)的行為特征。通常采用堆數(shù)據(jù)結(jié)構(gòu)來實現(xiàn)2022-01-01解決程序包org.springframework.test.context不存在
這篇文章主要介紹了解決程序包org.springframework.test.context不存在的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09