背景简述
本文意在搭建一个通用的应用后端服务环境, 账号验证是应用的基础环境之一.
OAuth2可提供安全的验证环境, 以access_token作为访问安全资源的令牌, 作为单一的应用端与后端的交互方式, 采用password类型会更简洁一点, 若是要实现类似Google, Facebook或新浪微博的第三方登录平台模式, 请选用Authorisation code grant.
OAuth类型参考说明: https://github.com/lucadegasperi/oauth2-server-laravel/wiki/Choosing-a-Grant
本文选用的OAuth2授权方式为: password, 需搭配 refresh_token使用, 在access_token过期后, 使用refresh_token申请新的access_token, 无需重新登录, 达到应用端登录一次,一直有效的效果.
若refresh_token失效, 则需用户重新登录, 这对对应着用户长期没有使用应用, 需让用户重新登录授权的场景. 一般时间可以设成一个月, 根据实际需求设置.
环境:
Laravel 5.1 安装请参考博文:
专为Laravel定制的OAuth2实现 oauth2-server-laravel : https://github.com/lucadegasperi/oauth2-server-laravel/wiki
oauth2-server-laravel PasswordGrant安装及配置请参考官网文档, 已经很清楚了:
安装配置: https://github.com/lucadegasperi/oauth2-server-laravel/wiki/Laravel-5-Installation
使用: https://github.com/lucadegasperi/oauth2-server-laravel/wiki/Implementing-an-Authorization-Server-with-the-Password-Grant
本文重点:
官网只说明了如何配置以及添加使用代码, 但未说明如何调用, 这可能会拦住不少菜鸟.
ps: 这里纠正一下官网文档的一个"手误":
'\App\PasswordVerifier@verify' 改成 '\App\PasswordGrantVerifier@verify' 与后面的类定义一致<br />
测试数据准备
测试前,需要有一个AppID和AppSecret, 和第三方平台一样, 这是为了能识别是某一个应用的访问
操作如下: 在oauth_clients表中加入一条app信息, 如:
模拟HTTP请求
这里模拟一下HTTP请求的过程, 直接贴图, 大家懂的
授权获取access_token, 注意参数中的 client_id及client_secret需与oath_clients里的一致.
access_token过期后, 用refresh_token进行更新, 返回新的 access_token 及 refresh_token

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment