Spring Boot+Spring Security+JWT 实现 RESTful Api 认证(一)
Spring Boot 2、Spring Security 5、JWT
IDEA+JDK8.0+MySQL5.0+
Spring Boot 2 + Spring Security 5 + JWT 实现给RestApi增加认证控制
{
"timestamp": 1567564486909,
"status": 401,
"error": "Unauthorized",
"message": "Full authentication is required to access this resource",
"path": "/users/userList"
}
curl http://localhost:8080/users/userList
原因就是因为这个url没有授权,所以返回401
curl -H "Content-Type: application/json" -X POST -d ‘{
"username": "admin",
"password": "password"
}‘ http://localhost:8080/users/signup
curl -i -H "Content-Type: application/json" -X POST -d ‘{
"username": "admin",
"password": "password"
}‘ http://localhost:8080/login
温馨提醒:这里的login方法是spring specurity框架提供的默认登录url
4.1将请求中的XXXXXX替换成拿到的token
4.2这次可以成功调用接口了
curl -H "Content-Type: application/json"
-H "Authorization: Bearer XXXXXX"
"http://localhost:8080/users/userList"
温馨提示:这里的登录接口还是使用的默认地址,如果你的token过期了,需要你重新登录生成新的token.
https://gitee.com/micai-code/springboot-springsecurity-jwt-demo.git
在使用的过程中,如有问题,可以添加真正讨论技术的QQ交流群,QQ群号为:715224124
Spring Boot+Spring Security+JWT 实现 RESTful Api 认证(一)
原文:https://www.cnblogs.com/sxdtzhaoxinguo/p/13792097.html