node.js - OAuth2:在 Node 中制作的资源服务器中处理来自 Spring Boot 中制作的身份验证服务器的 access_token
问题描述
我正在 Node 中制作 OAuth2 资源服务器,我需要处理来自 AS 的 de access_token(在 Spring Boot 中制作,我无权访问代码,因为它是由另一个团队制作的)和客户端侧面是用 Angular 6 制作的。
客户端将令牌存储在 cookie 中,并且必须发送到节点中的我的 api 以允许执行请求,这就是我卡住的地方
我一直在尝试一些带有 passport.js 的库,但我在 Node 方面没有太多经验,所以我无法以正确的方式处理 cookie。现在,我遇到了 500 错误,控制台向我抛出了一个策略错误“也许你忘了使用 cookie-parser?”,显然,我曾尝试使用 cookie-parser,但似乎我做错了什么
我在 index.js 中的自定义 cookie 方法(护照)
export const cookie = ({ required } = {}) => (req, res, next) =>
passport.authenticate('cookie', { session: false }, () => {
res.cookie('access_token')
})(req, res, next)
passport.use('cookie', new CookieStrategy({
cookieName: 'access_token'
},
function (token, done) {
User.findByToken({ token: token }, function (err, user) {
if (err) { return done(err) }
if (!user) { return done(null, false) }
return done(null, user)
})
}))
我的 GET 方法来检索客户列表:
router.get('/',
cookie({ required: true }),
query(),
index)
我尝试将其作为不记名令牌进行处理,因为在 Angular 中,我将令牌发送到带有从 cookie 中获取令牌的标头的 url 的 api(我在 Spring Boot 中制作了相同的资源服务器,并且在那里工作正常)。这是令牌的代码,而不是 cookie:
export const token = ({ required } = {}) => (req, res, next) =>
passport.authenticate('token', { session: false }, (err, user, info) => {
if (err || (required && !user) || (required)) {
return res.status(401).end()
}
req.logIn(user, { session: false }, (err) => {
if (err) return res.status(401).end()
next()
})
})(req, res, next)
passport.use('token', new JwtStrategy({
secretOrKey: jwtSecret,
jwtFromRequest: ExtractJwt.fromExtractors([
ExtractJwt.fromHeader('Authorization: Bearer '),
ExtractJwt.fromAuthHeaderAsBearerToken(),
ExtractJwt.fromUrlQueryParameter('access_token'),
ExtractJwt.fromBodyField('access_token'),
ExtractJwt.fromAuthHeaderWithScheme('Bearer')
])
}, ({ id }, done) => {
User.findById(id).then((user) => {
done(null, user)
return null
}).catch(done)
}))
我只需要更改 router.get "cookie" 为 "token" 这些是请求的标头:
Host: localhost:9000
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0
Accept: application/json, text/plain, */*
Accept-Language: es,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://localhost:8080/ui/login
Content-type: application/x-www-form-urlencoded; charset=utf-8
Authorization: Bearer some_token
Origin: http://localhost:8080
DNT: 1
Connection: keep-alive
我希望客户端在 Angular 中发送的令牌适用于我在节点中的资源服务器。
太感谢了!
解决方案
推荐阅读
- json - 对于 JSON 模式,如果设置了“模式”关键字,是否可以缺少“类型”关键字?
- javascript - reactjs createElement中的意外行为
- javascript - 无需滚动页面即可收集完整的按钮列表(DevTools Google Chrome)
- ios - 为什么这段代码会重新渲染 SwiftUI 视图的主体?
- javascript - AngularJs 副本:不放在表格的第二行
- pyspark - Spark优化编码
- javascript - 我有这两个吨位计算器。第一个工作得很好。我该如何修复第二个?我的 Javascript 有问题吗?
- angular - APP_INITIALIZER 不返回
- java - 如何创建相同对象的副本(例如标签)?
- filter - 使用 ffmpeg 将所有帧聚合为一张图像