我想使用中间件加密从Adonisjs5响应发送的数据。
我从服务类api返回响应数据,以便以这种方式对前端作出反应:
public async login(username:string,password:string)
{
return {'status':'success','data':{'username':username,'password':password }};
}现在,我想在adonis js 5中间件中获取这些数据,并首先对其进行加密,然后发送到客户端。我无法在adonis js 5中间件中获得返回的响应数据。请帮帮我。
发布于 2022-06-25 13:45:31
EncryptMiddleware。来源node ace make:middleware EncryptMiddlewarekernel.ts文件中将您的中间件文件注册为全局或命名中间件。来源/*
|--------------------------------------------------------------------------
| Global middleware
|--------------------------------------------------------------------------
*/
Server.middleware.register([() => import('App/Middleware/EncryptMiddleware')])或
/*
|--------------------------------------------------------------------------
| Named middleware
|--------------------------------------------------------------------------
*/
Server.middleware.registerNamed({ encryptResponse: () => import('App/Middleware/EncryptMiddleware') })Route.get('dashboard', 'DashboardController.index').middleware('encryptResponse') // EncryptMiddleware类中的App/Middleware/EncryptMiddleware中。来源import Encryption from '@ioc:Adonis/Core/Encryption'
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
export default class EncryptMiddleware {
public async handle(
{ response, request }: HttpContextContract,
next: () => Promise<void>
) {
// Some encryption logic => e.g. encrypt body here
const encryptedResponse = Encryption.encrypt(request.body())
// Send encrypted data to the response
response.send(encryptedResponse)
await next()
}
}https://stackoverflow.com/questions/72727819
复制相似问题