假如www.olcms.com/getUserInfo获取用户信息,你怎么知道当前用户是谁?有人说登陆时候我把他UID写入session了,如果是API接口,没有session怎么办,那么就需要把UID带到参数里面,如果直接带里面,安全怎么办?所以我们需要加密成别人看不懂的字符串,这就是JWT(JSON WEB TOKEN),你可以把它理解为微信SDK中的access token(其实本身就是一样的东西).JWT加密和解密你自己写也行,不过没有必要重复造轮子,我们在github上搜一下jwt,我搜到一个lcobucci/jwt,看起来用的人也挺多,好,下来我们大概用tp来演示下

下载tp3.2.3

安装lcobucci/jwt

新建composer.json

    {
"name": "olcms jwt demo",
"description": "just a jwt demo with tp",
"type": "demo",
"keywords": ["jwt","tp"],
"homepage": "https://www.olcms.com/",
"license": "Apache2",
"authors": [
{
"name": "olcms",
"email": "admin@olcms.com"
}
],
"require": {
"lcobucci/jwt" : "*"
}
}

composer update composer安装看 https://www.olcms.com/2015

打开index.php,在载入tp前载入comoposer的自动加载

//composer
require 'vendor/autoload.php'; // 引入ThinkPHP入口文件
require './ThinkPHP/ThinkPHP.php';

生成和使用jwt

IndexController.class.php

namespace Home\Controller;

use Think\Controller;
use Lcobucci\JWT\Builder; class IndexController extends Controller { public function index(){
$token = (new Builder())
->set('uid', 1) // Configures a new claim, called "uid"
->getToken(); // Retrieves the generated token
echo $token; // The string representation of the object is a JWT string (pretty easy, right?)
} }

浏览器访问,我们看到生成的jwteyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJ1aWQiOjF9.刷新一下,发现什么?没变,恩,不够安全,我们再修改下代码

namespace Home\Controller;

use Think\Controller;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256; class IndexController extends Controller { public function index(){
$signer = new Sha256();
$token = (new Builder())
->set('uid', 1) // Configures a new claim, called "uid"
->setExpiration(time() + 3600)
->sign($signer, 'olcms') // creates a signature using "testing" as key
->getToken(); // Retrieves the generated token
echo $token; // The string representation of the object is a JWT string (pretty easy, right?)
} }

生成的jwteyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjEsImV4cCI6MTQ2MDYwNjk0Mn0.GdbEXStqQR-5zofQVmorrB4U3yuyCYDdX-jFu58dPpY每次刷新也变- -

从jwt中获取信息

namespace Home\Controller;

use Think\Controller;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Parser; class IndexController extends Controller { public function index(){
$signer = new Sha256();
$token = (new Builder())
->set('uid', 1) // Configures a new claim, called "uid"
->setExpiration(time() + 3600)
->sign($signer, 'olcms') // creates a signature using "testing" as key
->getToken(); // Retrieves the generated token
echo $token; // The string representation of the object is a JWT string (pretty easy, right?) //从jwt获取信息
$token = (new Parser())->parse((string) $token); // Parses from a string
echo $token->getClaim('uid'); // will print "1"
} }

大概逻辑

用户登录,服务器生成jwt,放入memcache等缓存并返回jwt,client所有请求都必须带jwt

最新文章

  1. SQL Server:APPLY表运算符
  2. 学习C:打印输入中单词长度的水平方向直方图
  3. 初识Web 服务(即Web Service)
  4. Android系统兼容性问题(持续更新)
  5. 解决Ext.form.DateField在浏览器中显示可能有问题
  6. 转】Maven学习总结(九)——使用Nexus搭建Maven私服
  7. C# 微信支付证书使用
  8. [DOM]有一种节点叫做文本节点
  9. Eclipse 各种包说明
  10. ☀【JS】eval
  11. python 下的数据结构与算法---2:大O符号与常用算法和数据结构的复杂度速查表
  12. 指定字符串加密(对称加密DES)
  13. 一张图搞定Java设计模式——工厂模式! 就问你要不要学!
  14. MarkdownPad2测试
  15. JavaScript push() 方法
  16. leetcode之旅(9)-Reverse Linked List
  17. redis的持久化之RDB的配置和原理
  18. AddIn 中当前完整文件名的获取
  19. C# 数组,对象实例化并赋值
  20. springMVC学习之路4-最后的征程:整合hibernate

热门文章

  1. 深入浅出 Java Concurrency (40): 并发总结 part 4 性能与伸缩性[转]
  2. Spring MVC(十一)--使用字符串实现重定向
  3. centos7 yum 安装tomcat7
  4. nprogress 转
  5. python 第三方库的安装方法
  6. Unity IoC Base On MVC
  7. nginx部署为HTTP代理支持CONNECT模式
  8. 11-3-while
  9. Elasticsearch & Kibana with Shield
  10. 阿里云数据库再获学术顶会认可,一文全览VLDB最新亮点