问:基于令牌的身份验证 API 和 Javascript,你能保护其他客户端文件不被访问吗?

Q: Token based auth API and Javascript, can you protect other clientside files from being accessed?

问题

如果您使用与以下示例类似的设置:

是否可以保护其他客户端文件不被访问?举例来说,我有一个包含某些文件的目录,您需要某个角色才能访问它们。

是否可以在登录前保护我的 SPA 不被访问?

或者是否有更好的解决方案,让您最终在服务器、SPA 和 OIDCClient 中获得受保护的 api、folders/files 和静默更新机制?

@dmccaffery 通过回答我的问题帮助了我,这是他对感兴趣的人的回答。

总而言之,将 OIDCClient 用于 SPA 肯定是可行的方法。公开需要授权的东西应该使用 API 来完成。可以使用 Route guard 来保护 Angular 应用程序的某些部分。

其工作方式如下:

The access token is either a JWT or a bearer token (usually) and is added by the oidc client to every HTTP request in an authorization header — when a web API receives a reques, the bearer token authorization middleware will parse this HTTP header and will call the token introspection endpoint (and potentially the user info endpoint) to have the token validated and the users claims retrieved… if the token was manipulated by the client, it will not be valid, and an HTTP error will be returned (usually a 403). If the token was valid, a claims identity is created and assigned to the http request context. The API will now have a thread with an identity already assigned to it that represents that user.

他还指出了 2 个可能有用的 pluralsight 课程: https://www.pluralsight.com/courses/building-securing-restful-api-aspdotnet https://www.pluralsight.com/courses/oauth2-openid-connect-angular-aspdotnet