当前位置:网站首页>Simple user-defined authentication interface rules
Simple user-defined authentication interface rules
2022-07-29 04:50:00 【Bitter and sweet】
Interface authentication is divided into two parts
1. token For timestamps [ But the requirement is 80-120 A timestamp in the time range before minutes ], The default setting is 90 Minutes ago , be token Valid for 30 minute , The server determines the valid range of the timestamp ; The range can be customized
- token type Long Extract the current time 90 Time stamp of minutes ago
- sign type String Time stamp token Conduct md5 after , Take before 8 position
2. Signature sign, Yes token Value for md5( Salt can be added ), Then extract md5 Before the last string 8 position ( Rules can be customized )
It works URL Generated after , There is usually a validity period , The default setting is 30 minute ; This method is simple, crude and effective , Ground beetle algorithm , Ordinary small project cooperation is enough .
package com.learnworm.common.tools;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;
/**
* @Author :learnworm
* @Desc : Md5 Tool class
* @Date : 2021-12-14 10:55
**/
public class Md5Tool {
private static final int DEFAULT_EXPIRE_TIME = 90;
private static final int DEFAULT_EXPIRE_TIMEOUT = 30;
/**
* java Native MD5 encryption algorithm
* @param plainText
* @return
*/
public String md5s(String plainText) {
String str = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
str = buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return str;
}
/**
* @desc: Self created time stamp verification algorithm for ground beetle
* @param min
* @return
* Date
*/
public Long createTimestamp(int min) {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.MINUTE, -min);
return c.getTime().getTime();
}
/**
* @desc Generate signature string
* @return
*/
public String createSign(){
Long timestamp = createTimestamp(DEFAULT_EXPIRE_TIME);
return md5s(timestamp.toString()).substring(0,8);
}
/**
* @desc Generate signature string
* @return
*/
public String createSign(Long timestamp){
if (timestamp==null){
timestamp = createTimestamp(DEFAULT_EXPIRE_TIME);
}
return md5s(timestamp.toString()).substring(0,8);
}
/**
* Time difference - minute
* @desc: getMinutesDiff
* @param stopDate
* @param startDate
* @return
* int
*/
public int getMinutesDiff(Date stopDate, Long startDate) {
long t2 = stopDate.getTime();
long t1 = startDate;
int diff = (int) ((t2 - t1) / 60000L);
diff += (t2 > t1 + diff * 60000L ? 1 : 0);
return diff;
}
/**
* @ Check if it is valid
* @param timestamp
* @return
*/
public boolean checkToken(Long timestamp){
boolean isValid = false;
Date date = new Date();
int diff = getMinutesDiff(date,timestamp);
System.out.println(timestamp+"===checkToken===="+diff);
if (diff>DEFAULT_EXPIRE_TIME-10 && diff < DEFAULT_EXPIRE_TIME+DEFAULT_EXPIRE_TIMEOUT){
isValid = true;
}
System.out.println(isValid);
return isValid;
}
/**
* @ Check if it is valid
* @param timestamp
* @return
*/
public boolean checkSign(Long timestamp,String sign){
boolean isValid = false;
String calcSign = createSign(timestamp);
if (calcSign.equals(sign)){
isValid = true;
}
System.out.println(calcSign+"===checkSign====="+sign+"====="+isValid);
return isValid;
}
public static void main(String agrs[]) {
Md5Tool md51 = new Md5Tool();
int point = 90;
String sign = null;
Long token = null;
token = md51.createTimestamp(point);
sign = md51.md5s(token.toString());
System.out.println(token+"========"+sign.substring(0,8));
}
}
边栏推荐
- mujoco和mujoco_py安装以及解决libXcursor.so.1:NO such dictionary
- RecyclerView通过DPAD按键上下切换焦点 切换到界面外的控件时焦点会左右乱跳
- Flink+Iceberg环境搭建及生产问题处理
- Numpy basic learning
- UE plays video in scene or UMG
- Ethernet of network
- 让你的正则表达式可读性提高一百倍
- Use openmap and ArcGIS to draw maps and transportation networks of any region, and convert OMS data into SHP format
- GCC基础知识
- File operation (Advanced C language)
猜你喜欢
Command line interactive tools (latest version) inquirer practical tutorial
在线教育的推荐系统
(heap sort) heap sort is super detailed, I don't believe you can't (C language code implementation)
mujoco和mujoco_py安装以及解决libXcursor.so.1:NO such dictionary
Christmas tree web page and Christmas tree application
There are objections and puzzles about joinpoint in afterreturning notice (I hope someone will leave a message)
Star a pathfinding in LAYA
谷歌浏览器 打开网页出现 out of memory
STL source code analysis (Hou Jie) notes -- Classification and testing of stl containers
Build auto.js script development environment
随机推荐
Learn matlab to draw geographical map, line scatter bubble density map
OpenCV环境搭建
Ethernet of network
Use more flexible and convenient Rogowski coil
Oracle 插入数据
Climbing the pit of traffic flow prediction (III): using pytorch to realize LSTM to predict traffic flow
让你的正则表达式可读性提高一百倍
img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
Flutter 手势监听和画板实现
Mongo shell interactive command window
Build auto.js script development environment
[c language] PTA 7-48 find the number of combinations
Mysql:the user specified as a definer ('root '@'%) does not exist
IOS interview preparation - Online
What are the core features of the digital transformation of state-owned construction enterprises?
Pyqt5 learning pit encounter and pit drainage (3) background picture coverage button style and check button status
Leetcode 763. partition labels divide alphabetic intervals (medium)
Solution to the fourth game of 2022 Hangzhou Electric Multi school league
Pycharm reports an error when connecting to the virtual machine database
Christmas tree web page and Christmas tree application