- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
最近我尝试使用 IdentityServer4 和 React 客户端来设置身份验证。我按照 IdentityServer 文档的添加 JavaScript 客户端
教程(部分)进行操作:https://media.readthedocs.org/pdf/identityserver4/release/identityserver4.pdf还使用 Quickstart7_JavaScriptClient
文件。
缺点是我使用 React 作为前端,而我对 React 的了解不足以使用 React 实现教程中使用的相同功能。
尽管如此,我还是开始阅读并尝试开始使用它。我的 IdentityServer 项目和 API 已设置并且似乎工作正常(也与其他客户端进行了测试)。
我首先将 oidc-client.js 添加到我的 Visual Code 项目中。接下来,我创建了一个在开始时呈现的页面(将其命名为 Authentication.js),这是包含登录、调用 API 和注销按钮的地方。此页面(Authentication.js)如下所示:
import React, { Component } from 'react';
import {login, logout, api, log} from '../../testoidc'
import {Route, Link} from 'react-router';
export default class Authentication extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<div>
<button id="login" onClick={() => {login()}}>Login</button>
<button id="api" onClick={() => {api()}}>Call API</button>
<button id="logout" onClick={() => {logout()}}>Logout</button>
<pre id="results"></pre>
</div>
<div>
<Route exact path="/callback" render={() => {window.location.href="callback.html"}} />
{/* {<Route path='/callback' component={callback}>callback</Route>} */}
</div>
</div>
);
}
}
在 testoidc.js 文件(上面导入的)中,我添加了所有使用的 oidc 函数(示例项目中的 app.js)。路由部分应该使callback.html可用,我已按原样保留该文件(这可能是错误的)。
testoidc.js 文件包含以下功能:
import Oidc from 'oidc-client'
export function log() {
document.getElementById('results').innerText = '';
Array.prototype.forEach.call(arguments, function (msg) {
if (msg instanceof Error) {
msg = "Error: " + msg.message;
}
else if (typeof msg !== 'string') {
msg = JSON.stringify(msg, null, 2);
}
document.getElementById('results').innerHTML += msg + '\r\n';
});
}
var config = {
authority: "http://localhost:5000",
client_id: "js",
redirect_uri: "http://localhost:3000/callback.html",
response_type: "id_token token",
scope:"openid profile api1",
post_logout_redirect_uri : "http://localhost:3000/index.html",
};
var mgr = new Oidc.UserManager(config);
mgr.getUser().then(function (user) {
if (user) {
log("User logged in", user.profile);
}
else {
log("User not logged in");
}
});
export function login() {
mgr.signinRedirect();
}
export function api() {
mgr.getUser().then(function (user) {
var url = "http://localhost:5001/identity";
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onload = function () {
log(xhr.status, JSON.parse(xhr.responseText));
}
xhr.setRequestHeader("Authorization", "Bearer " + user.access_token);
xhr.send();
});
}
export function logout() {
mgr.signoutRedirect();
}
有很多地方出了问题。当我单击登录按钮时,我被重定向到 IdentityServer 的登录页面(这很好)。当我使用有效凭据登录时,我将被重定向到我的 React 应用程序:http://localhost:3000/callback.html#id_token=Token
Identity项目中的这个客户端定义如下:
new Client
{
ClientId = "js",
ClientName = "JavaScript Client",
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
// where to redirect to after login
RedirectUris = { "http://localhost:3000/callback.html" },
// where to redirect to after logout
PostLogoutRedirectUris = { "http://localhost:3000/index.html" },
AllowedCorsOrigins = { "http://localhost:3000" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"api1"
}
}
不过,回调函数似乎从未被调用,它只是停留在回调 URL 上,后面有一个很长的标记..
此外,登录后 getUser 函数一直显示“用户未登录”,并且“调用 API”按钮一直显示没有 token 。很明显事情没有正常工作。我只是不知道哪里出了问题。检查时,我可以看到本地存储中生成了一个 token :
此外,当我单击注销按钮时,我会被重定向到身份主机的注销页面,但是当我单击那里的注销时,我不会被重定向到我的客户端。
我的问题是:
有人能指出我正确的方向吗,这里很可能有更多的事情出了问题,但目前我只是陷入了从哪里开始。
最佳答案
IdentityServer4只是OIDC的后端实现;因此,您所需要做的就是使用给定的 API 在客户端中实现流程。我不知道 oidc-client.js 文件是什么,但它很可能做与您自己实现相同的事情。流程本身非常简单:
client_id
和 redirect_uri
(以及状态、随机数)将用户重定向到身份验证服务器client_id
和 redirect_uri
是否匹配。
redirect_uri
。在您的情况下,URL 将如下所示:https://example.com/cb#access_token=...&id_token=...&stuff-like-nonce-and-state
<实现逻辑的最简单方法是首先在路由器中设置一条路由,该路由解析为执行逻辑的组件。该组件可以是“不可见的”。它甚至不需要渲染任何东西。您可以这样设置路线:
<Route path="/cb" component={AuthorizeCallback} />
然后,在 AuthorizeCallback
组件中实现 OIDC 客户端逻辑。在组件中,你只需要解析URL即可。您可以使用location.hash访问 URL 的 #access_token=...&id_token=...&stuff-like-nonce-and-state
部分。您可以使用URLSearchParams或像 qs 这样的第三方库。然后,只需将值存储在某个地方(sessionStorage、localStorage,如果可能的话,还有 cookies)。您所做的任何其他事情都只是实现细节。例如,在我的一个应用程序中,为了记住用户在应用程序中所在的事件页面,我将该值存储在 sessionStorage 中,然后在 AuthorizeCallback
中使用该存储中的值来重定向用户到正确的页面。因此,Auth 服务器重定向到“/cb”,解析为 AuthorizeCallback
,并且该组件根据用户所在位置重定向到所需位置(如果未设置位置,则重定向到“/”)。
此外,请记住,如果授权服务器的 session cookie 未过期,则在 token 过期或删除时您将不需要重新登录。如果 token 已过期,这很有用,但在您注销时可能会出现问题。这就是为什么当您注销时,您需要立即向授权服务器发送请求以删除/使 token 过期,然后再从存储中删除 token 。
关于reactjs - 在 React 前端中使用 oidc-client.js 和 Identityserver4 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49754595/
问题是,当用户回复彼此的帖子时,我必须这样做: margin-left:40px; 对于 1 级深度 react margin-left:80px; 对于 2 层深等 但是我想让 react div
我试图弄清楚如何将 React Router 与 React VR 连接起来。 首先,我应该使用 react-router dom/native ?目前尚不清楚,因为 React VR 构建在 Rea
我是 React 或一般编码背景的新手。我不确定这些陈述之间有什么区别 import * as react from 'react' 和 import react from 'react' 提前致谢!
我正在使用最新的稳定版本的 react、react-native、react-test-renderer、react-dom。 然而,react-native 依赖于 react@16.0.0-alp
是否 react 原生 应用程序开发可以通过软件架构实现,例如 MVC、MVP、MVVM ? 谢谢你。 最佳答案 是的。 React Native 只是你提到的那些软件设计模式中的“V”。如果你考虑其
您好我正在尝试在我的导航器右按钮中绑定(bind)一个功能, 但它给出了错误。 这是我的代码: import React, { Component } from 'react'; import Ico
我使用react native创建了一个应用程序,我正在尝试生成apk。在http://facebook.github.io/react-native/docs/signed-apk-android.
1 [我尝试将分页的 z-index 更改为 0,但没有成功] 这是我的codesandbox的链接:请检查最后一个选择下拉列表,它位于分页后面。 https://codesandbox.io/s/j
我注意到 React 可以这样导入: import * as React from 'react'; ...或者像这样: import React from 'react'; 第一个导入 react
我是 react-native 的新手。我正在使用 React Native Paper 为所有屏幕提供主题。我也在使用 react 导航堆栈导航器和抽屉导航器。首先,对于导航,论文主题在导航组件中不
我有一个使用 Ignite CLI 创建的 React Native 应用程序.我正在尝试将 TabNavigator 与 React Navigation 结合使用,但我似乎无法弄清楚如何将数据从一
我正在尝试在我的 React 应用程序中进行快照测试。我已经在使用 react-testing-library 进行一般的单元测试。然而,对于快照测试,我在网上看到了不同的方法,要么使用 react-
我正在使用 react-native 构建跨平台 native 应用程序,并使用 react-navigation 在屏幕之间导航和使用 redux 管理导航状态。当我嵌套导航器时会出现问题。 例如,
由于分页和 React Native Navigation,我面临着一种复杂的问题。 单击具有类别列表的抽屉,它们都将转到屏幕 问题陈述: 当我随机点击类别时,一切正常。但是,在分页过程中遇到问题。假
这是我的抽屉导航: const DashboardStack = StackNavigator({ Dashboard: { screen: Dashboard
尝试构建 react-native android 应用程序但出现以下错误 info Running jetifier to migrate libraries to AndroidX. You ca
我目前正在一个应用程序中实现 React Router v.4,我也在其中使用 Webpack 进行捆绑。在我的 webpack 配置中,我将 React、ReactDOM 和 React-route
我正在使用 React.children 渲染一些带有 react router 的子路由(对于某个主路由下的所有子路由。 这对我来说一直很好,但是我之前正在解构传递给 children 的 Prop
当我运行 React 应用程序时,它显示 export 'React'(导入为 'React')在 'react' 中找不到。所有页面错误 see image here . 最佳答案 根据图像中的错误
当我使用这个例子在我的应用程序上实现 Image-slider 时,我遇到了这个错误。 import React,{Component} from 'react' import {View,T
我是一名优秀的程序员,十分优秀!