CREATE TABLE user
(
id
INT(10) NOT NULL AUTO_INCREMENT,
name
VARCHAR(50) NULL DEFAULT NULL,
age
INT(11) NULL DEFAULT NULL,
sex
INT(10) NULL DEFAULT NULL,
address
VARCHAR(100) NULL DEFAULT NULL,
password
VARCHAR(50) NULL DEFAULT '123456',
role
VARCHAR(50) NULL DEFAULT '1',
salt
VARCHAR(50) NULL DEFAULT 'abc',
PRIMARY KEY (id
)
)
COMMENT='测试用户表'
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=9
;
INSERT INTO user
(id
, name
, age
, sex
, address
, password
, role
, salt
) VALUES (1, '张三', 18, 1, '湖北省武汉市', '123456', '1', 'abc');
INSERT INTO user
(id
, name
, age
, sex
, address
, password
, role
, salt
) VALUES (2, '李四', 21, 2, '湖南省长沙市', '123456', '1', 'abc');
INSERT INTO user
(id
, name
, age
, sex
, address
, password
, role
, salt
) VALUES (3, '王武', 27, 1, '江西省南昌市', '123456', '1', 'abc');
INSERT INTO user
(id
, name
, age
, sex
, address
, password
, role
, salt
) VALUES (5, '李思琪', 18, 2, '广东省广州市', '123456', '1', 'abc');
INSERT INTO user
(id
, name
, age
, sex
, address
, password
, role
, salt
) VALUES (7, 'zz', 10, 1, 'wuhan', 'df10ef8509dc176d733d59549e7dbfaf', '1', 'abc');
INSERT INTO user
(id
, name
, age
, sex
, address
, password
, role
, salt
) VALUES (8, 'zz1', 50, 2, 'cs', '123456', '1', 'abc');
这是使用JWT实现Spring Boot令牌认证的例子 JSON Web Tokens(或简称JWT)也是一种令牌,它不仅是用户唯一的令牌,而且还包含该用户所需的任何信息,即所谓的声明。
1、获取token localhost:8080/oauth/token POST Content-Type : application/json { "clientId":"098f6bcd4621d373cade4e832627b4f6", "userName":"zz", "password":"123456" }
需要开始输入用户名密码 默认:user 密码:启动的时候会打印在后台
得到token { "errcode": 0, "errmsg": "OK", "p2pdata":{ "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoiMSIsInVuaXF1ZV9uYW1lIjoienoiLCJ1c2VyaWQiOiJ6eiIsImlzcyI6InJlc3RhcGl1c2VyIiwiYXVkIjoiMDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjYiLCJleHAiOjE0OTI1ODMxMDEsIm5iZiI6MTQ5MjQxMDMwMX0.NKfgvw8Ik4FFsATvzCRRRnATn8yaZCpXMtoEkH49EJw", "token_type": "bearer", "expires_in": 172800 } }
2、使用token http://localhost:8080/user/getusers?role=1 GET header中填写如下参数: Content-Type: application/xml Authorization: Basic dXNlcjpjY2E3Nzg2Yi1lZTcwLTQ1M2YtOTk3NC04ZjVhZWU2NWJkM2E= //这个不知道为什么改不过来,就使用下面的参数进行取值 MyAuthorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoiMSIsInVuaXF1ZV9uYW1lIjoienoiLCJ1c2VyaWQiOiJ6eiIsImlzcyI6InJlc3RhcGl1c2VyIiwiYXVkIjoiMDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjYiLCJleHAiOjE0OTI1ODMxMDEsIm5iZiI6MTQ5MjQxMDMwMX0.NKfgvw8Ik4FFsATvzCRRRnATn8yaZCpXMtoEkH49EJw
返回值: { "errcode": 0, "errmsg": "OK", "p2pdata": [ { "id": 1, "name": "张三", "password": "123456", "age": "18", "sex": "1", "salt": "abc", "role": "1" }, { "id": 2, "name": "李四", "password": "123456", "age": "21", "sex": "2", "salt": "abc", "role": "1" }, { "id": 3, "name": "王武", "password": "123456", "age": "27", "sex": "1", "salt": "abc", "role": "1" }, { "id": 5, "name": "李思琪", "password": "123456", "age": "18", "sex": "2", "salt": "abc", "role": "1" }, { "id": 7, "name": "zz", "password": "df10ef8509dc176d733d59549e7dbfaf", "age": "10", "sex": "1", "salt": "abc", "role": "1" }, { "id": 8, "name": "zz1", "password": "123456", "age": "50", "sex": "2", "salt": "abc", "role": "1" } ], }
3、不拦截的 http://localhost:8080/user/getuser?id=1 GET 不用传token