我正在使用带有 jwt 的passport.js 来验证我的应用程序中的用户。我正在使用包含其他字段和头像字段的有效负载签署 jwt 令牌,以便在我的应用程序中使用头像。现在,我希望用户能够编辑他们的头像。我实现了这个功能并且在一个问题上工作得很好:即使在 mongodb 中更新了头像字段,但只有在我注销并重新登录后,更改才会显示在应用程序中。(再次重新签名令牌后)
在前端,我使用的是 react + redux。
考虑到这是有效载荷的一部分,我应该如何以正确的方式更新此头像字段?我应该使用另一种方法吗?
代码如下:
登录登录如下:
User.findOne({ email: email }).then(user => {
if (!user) {
return res.status(404).json({
email: "Couldn't find an account."
});
} else {
bcrypt.compare(password, user.password).then(isMatch => {
if (isMatch) {
//User matched
//Create JWT Payload (can contain any user info)
const payload = {
id: user.id,
firstname: user.firstname,
lastname: user.lastname,
email: user.email,
avatar: user.avatar
};
//Sign token
//The sign method from jwt needs a payload(user info), secret and …Run Code Online (Sandbox Code Playgroud) 登录 Native App 时获取的 JWT 不会发出登录 Web App 时获取的 JWT 所发出的“groups”节点。
两个应用程序注册都配置为发出组 “groupMembershipClaims”:“SecurityGroup”
这是一个隐式授权方案
编辑:我用 PierreDuc 给我的答案更新了代码。他的解决方案对我有用。我编辑代码以便每个人都可以看到工作代码
我正在使用 canActivate 和 canActivateChild 来了解是否有人拥有我的网络令牌,我想阻止任何没有令牌的人。我将 localStorage 用于用户数据和令牌。
当我尝试使用用户 id:1 登录时,守卫导航到未找到的页面,但在我刷新页面后,我可以访问该页面。当我使用调试器时,我看到在刷新之前 localStorage 是空的,但在刷新后有值。
为什么会发生这种情况?我该如何解决?这是相关代码:
app-routing.module.ts
const appRoutes: Routes = [
{ path: 'login/:id', canActivate: [AuthGuard], children: [] },
{ path: '', canActivateChild: [AuthGuard], children: [
{ path: '', redirectTo: '/courses', pathMatch: 'full' },
{ path: 'courses', component: CourseListComponent, pathMatch: 'full'},
{ path: 'courses/:courseId', component: CourseDetailComponent, pathMatch: 'full' },
{ path: 'courses/:courseId/unit/:unitId', component: CoursePlayComponent,
children: [
{ path: '', component: CourseListComponent },
{ path: 'lesson/:lessonId', component: CourseLessonComponent, data:{ type: …Run Code Online (Sandbox Code Playgroud) 我在尝试实现设计 jwt 时遇到问题。这是我的设计用户模型:
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:database_authenticatable,
:jwt_authenticatable,
jwt_revocation_strategy: JwtBlacklist
end
Run Code Online (Sandbox Code Playgroud)
这是我的 blacklist.rb 模型。
class JwtBlacklist < ApplicationRecord
include Devise::JWT::RevocationStrategies::Blacklist
self.table_name = 'jwt_blacklist'
end
Run Code Online (Sandbox Code Playgroud)
这就是我得到的。
Caused by:
NameError: uninitialized constant User::JwtBlacklist
Run Code Online (Sandbox Code Playgroud)
希望你能帮我解决这个问题,我是 Rails 新手。非常感谢。
我遇到了 .NET Core 如何在我的生产环境中处理 JSON Web 令牌 (JWT) 身份验证的问题。我正在使用 Postman 进行测试。如果我使用有效的令牌调用我的 API,它可以正常工作并从 API 端点返回预期的响应。但是,如果令牌已过期,我将不会收到预期的 HTTP 401。事实上,我没有收到任何响应,它只是悄悄地失败了。我的生产配置似乎不起作用。如果我在本地开发环境中对此进行测试,我不会遇到任何问题,并且过期的令牌会像预期的那样获得 401 未授权的 HTTP 响应。
我的 Web 应用程序对通过网站登录的人使用 cookie 身份验证,对连接到 API 的人使用 JWT 身份验证。这是 JWT 身份验证的配置。这是在 Startup.cs 中。这是 services.Authentication 的 JWT 配置部分。cookie 身份验证首先设置并且是默认身份验证方案。
.AddJwtBearer(options =>{
options.RequireHttpsMetadata = !_hostingEnvironment.IsDevelopment();
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Settings.Api.JwtBearer.TokenValidation.ValidIssuer,
ValidAudience = Settings.Api.JwtBearer.TokenValidation.ValidAudience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Settings.Api.JwtBearer.TokenValidation.IssuerSigningKey))
};
options.Events = new JwtBearerEvents()
{
OnAuthenticationFailed = context => …Run Code Online (Sandbox Code Playgroud) 我为我的 JupyterHub 应用程序配置了Google 的Identity-Aware Proxy,并想用它来验证我的用户。我该如何实现?
我的 API 在用户登录后返回访问令牌。
所有未来的请求都必须在 Authentication 标头中包含此令牌。
我希望用户即使关闭并重新打开浏览器也能保持登录状态。
我可以安全地将此访问令牌的加密版本存储在 localStorage 中、检索它、在我的 React 客户端代码中对其进行解密并将其发送到 API 吗?
我需要解码我的 JWT 令牌并检查范围是否是“医生”。
我对 GO 知之甚少,但我只需要在我的应用程序中编写一个小片段来扩展现有应用程序,因此它需要用 GO 编写。
这是我尝试解码令牌并检查“Doctor”是否在解码的令牌中,因为我无法专门访问范围。
for k, v := range props {
token_value := fmt.Sprint(v)
token_key := fmt.Sprint(cfg.AuthKey)
if (k == "custom_token_header") && strings.Contains(token_value, token_key) {
if token, _ := jwt.Parse(token_value, nil); token != nil {
parsed_token := fmt.Sprint(token)
log.Infof("Parsed token: " ,parsed_token)
if strings.Contains(parsed_token, "Doctor") {
log.Infof("user is a Doctor, perform checks...")
loc, _ := time.LoadLocation("GMT")
now := time.Now().In(loc)
hr, _, _ := now.Clock()
if hr >= 9 && hr < 5 {
log.Infof("success, inside …Run Code Online (Sandbox Code Playgroud) I am trying to put a JWT Auth to access my API : /api/docs But I am currently getting an error while trying to get the token with this command :
curl -X POST -H "Content-Type: application/json" http://localhost/login_check -d '{"username":"johndoe","password":"test"}'
Run Code Online (Sandbox Code Playgroud)
Of course I replace username and password
Signature key "/var/www/config/jwt/private.pem" does not exist or is not readable. Did you correctly set the "lexik_jwt_authentication.signature_key" configuration key? (500 Internal Server Error)
security.yaml
firewalls:
login:
pattern: ^/login
stateless: true
anonymous: true
provider: fos_userbundle_2 …Run Code Online (Sandbox Code Playgroud) 我使用以下带有 react/redux 教程的登录来在我的 React 应用程序中构建注册/登录功能,但是直到最近我才意识到我现在还需要重置密码/忘记密码功能。
此功能根本不是本教程的一部分,我只是想知道是否有人对我如何进行此操作有任何建议?
让我知道我是否可以分享有关我的应用程序的任何信息来帮助解决这个问题,或者是否有更好的地方来发布此类问题。我推迟在应用程序上分享更多内容,因为我认为这是多余的,因为教程中的信息几乎与我的注册/登录设置方式完全相同。
谢谢!
jwt ×10
reactjs ×3
redux ×2
token ×2
angular ×1
api ×1
asp.net-core ×1
express ×1
go ×1
google-iap ×1
javascript ×1
jupyterhub ×1
jwt-go ×1
passport.js ×1
ruby ×1
symfony ×1
typescript ×1