Node.js 在spring boot中使用Outh服务器uri验证nodejs资源服务器中的访问令牌 我有OAuth2服务器[Spring boot],它使用密码凭据方法验证客户端 前端客户端[Vuejs]获得的访问令牌使用该令牌访问资源服务器[Spring boot]
在这里,当前端客户端将访问令牌传递给资源服务器时,资源服务器使用以下代码与OAuth2服务器交叉验证它Node.js 在spring boot中使用Outh服务器uri验证nodejs资源服务器中的访问令牌 我有OAuth2服务器[Spring boot],它使用密码凭据方法验证客户端 前端客户端[Vuejs]获得的访问令牌使用该令牌访问资源服务器[Spring boot],node.js,spring-boot,oauth-2.0,Node.js,Spring Boot,Oauth 2.0,在这里,当前端客户端将访问令牌传递给资源服务器时,资源服务器使用以下代码与OAuth2服务器交叉验证它 @配置 @EnableResourceServer 公共类OAuth2ResourceServerConfigRemoteTokenService扩展了ResourceServerConfigurerAdapter{ @初级的 @豆子 公共远程令牌服务令牌服务(){ final RemoteTokenServices tokenService=新的RemoteTokenServices();
@配置
@EnableResourceServer
公共类OAuth2ResourceServerConfigRemoteTokenService扩展了ResourceServerConfigurerAdapter{
@初级的
@豆子
公共远程令牌服务令牌服务(){
final RemoteTokenServices tokenService=新的RemoteTokenServices();
tokenService.setCheckTokenEndpointUrl(“https://localhost:9088/oauth/check_token");
setClientId(“fooclientdpassword”);
tokenService.setClientSecret(“密码”);
返回令牌服务;
}
}
tokenService.setCheckTokenEndpointUrl(“https://localhost:9088/oauth/check_token");代码>尝试express-oauth2-bearer库,它工作正常
首先,安装它:
npm i express-oauth2-bearier—保存
并在客户端中使用此代码:
库需要以下值来验证请求:
- 颁发者基本URL:授权服务器的基本URL。如果您使用的是Auth0,则这是您的租户域与
https://
(如https://tenant.auth0.com
)可在中应用程序的设置选项卡上找到
- 允许的访问群体:访问令牌允许的访问群体标识符(或由逗号分隔的多个标识符)。如果您使用的是Auth0,则这是在中的API设置选项卡上找到的标识符
可以在应用程序根目录中的.env
文件中配置:
# .env
ISSUER_BASE_URL=https://YOUR_DOMAIN
ALLOWED_AUDIENCES=https://api.yourapplication.com
。。。或者在您的应用程序代码中:
app.use(auth({
发行人背景:'https://tenant.auth0.com',
允许观众:'https://api.yourapplication.com'
}));
OpenID策略是令牌验证的默认策略。使用.env
文件中设置的配置值,以下代码将所有正在进行的路由的请求限制为具有https://api.yourapplication.com
受众和阅读:产品
范围:
const{auth,requiredScopes}=require('express-oauth2-bearer');
app.use(auth());
app.get(“/products”,
所需范围(“读取:产品”),
(请求、回复)=>{
console.dir(请求授权声明);
res.sendStatus(200);
});
如果访问令牌不希望像OpenID Connect ID令牌那样进行签名,请添加带有回调的auth
中间件以进行验证,如下所示:
const{auth,requiredScopes}=require('express-oauth2-bearer');
const validateAccesToken=异步(令牌)=>{
const token=wait db.tokens.find(令牌);
if(token.expired){return;}
返回令牌;
};
app.use(auth(validateAccessToken));
app.get(“/products”,
所需范围(“读取:产品”),
(请求、回复)=>{
console.dir(请求授权声明);
res.sendStatus(200);
});