假如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

API安全验证之JWT(JSON WEB TOKEN) OLCMS的更多相关文章

  1. [更新]一份包含: 采用RSA JWT(Json Web Token, RSA加密)的OAUTH2.0,HTTP BASIC,本地数据库验证,Windows域验证,单点登录的Spring Security配置文件

    没有任何注释,表怪我(¬_¬) 更新: 2016.05.29: 将AuthorizationServer和ResourceServer分开配置 2016.05.29: Token获取采用Http Ba ...

  2. Java JWT: JSON Web Token

    Java JWT: JSON Web Token for Java and Android JJWT aims to be the easiest to use and understand libr ...

  3. JWT(JSON Web Token) 【转载】

    JWT(JSON Web Token) 什么叫JWTJSON Web Token(JWT)是目前最流行的跨域身份验证解决方案. 一般来说,互联网用户认证是这样子的. 1.用户向服务器发送用户名和密码. ...

  4. ( 转 ) 什么是 JWT -- JSON WEB TOKEN

    什么是JWT Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点 ...

  5. 关于JWT(Json Web Token)的思考及使用心得

    什么是JWT? JWT(Json Web Token)是一个开放的数据交换验证标准rfc7519(php 后端实现JWT认证方法一般用来做轻量级的API鉴权.由于许多API接口设计是遵循无状态的(比如 ...

  6. 如何在SpringBoot中集成JWT(JSON Web Token)鉴权

    这篇博客主要是简单介绍了一下什么是JWT,以及如何在Spring Boot项目中使用JWT(JSON Web Token). 1.关于JWT 1.1 什么是JWT 老生常谈的开头,我们要用这样一种工具 ...

  7. 什么是JWT(Json Web Token)

    什么是 JWT (Json Web Token) 用户认证是计算机安全领域一个永恒的热点话题. JWT 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519). 该to ...

  8. JWT(Json Web Token)认证

    目录 JWT(Json Web Token) JWT的数据结构 JWT的用法 JWT验证流程

  9. 温故知新,.Net Core遇见JWT(JSON Web Token)授权机制方案

    什么是JWT JWT (JSON Web Token) 是一个开放标准,它定义了一种以紧凑和自包含的方法,用于在双方之间安全地传输编码为JSON对象的信息. 因此,简单来说,它是JSON格式的加密字符 ...

随机推荐

  1. C++ 变量、常量、符号常量

    变量: int i = 0;  // i 是变量 i=5;         //i 可以修改 变量就是程序内一个内存位置的符号名,在该内存位置可以保存数据,并可以通过符号名对该内存地址存放的数据进行访 ...

  2. Struts2转换器

    为什么进行类型转换 在基于HTTP协议的Web应用中 客户端请求的所有内容都以文本编码方式传输到服务器端 服务器端的编程语言却有着丰富的数据类型 继承StrutsTypeConverter抽象类 继承 ...

  3. Struts2入门问题

    一 使用Struts 2 开发程序的基本步骤 加载Struts2 类库 配置web.xml文件 开发视图层页面 开发控制层Action 配置struts.xml文件 部署.运行项目 第一步先导架包:在 ...

  4. 07_jQuery对象初识(五)事件(非常重要)

    1. 目前为止学过的绑定事件的方式 1. 在标签里面写 onclick=foo(this); 2. 原生DOM的JS绑定 DOM对象.onclick=function(){...} 3. jQuery ...

  5. linux命令补全工具

    一:下载bash-competion工具 https://files.cnblogs.com/files/zgngg/bash-completion.zip 二:解压  unzip bash-comp ...

  6. 爬虫(二)建立代理ip池

    之前我们说网站反爬虫的一个常用方法是检测ip,限制访问频率.所以我们要通过设置代理ip的办法绕过这个限制.有不少提供免费代理ip的网站,像https://www.xicidaili.com/nt/,我 ...

  7. leetcode 996. Number of Squareful Arrays

    给定一个长度小于 12 的数组 要求排列方式的种数 使得相邻和为完全平方 不考虑数学结构 将问题转化为 一笔画问题 和为完全平方代表 之间存在通路 回溯法 N^N 记忆化搜索 NN 2^N 判断是否是 ...

  8. ElasticSearch入门介绍之安装部署(二)

    散仙,在上篇文章对ElasticSearch整体入门作了个介绍,那么本篇我们来看下,如何安装,部署es,以及如何安装es的几个比较常用的插件. es的安装和部署,是非常简单方便的,至少这一点散仙在es ...

  9. 11.Hibernate一对多关系

    创建JavaBean 一方: Customer private long cust_id; private String cust_name; private long cust_user_id; p ...

  10. P1009阶乘之和

    n=int(input()) ans=0 for i in range(1,n+1): t=1 for j in range(1,i+1): t*=j; ans+=t; print(ans)