有一些用于在PHP中实现JSON Web令牌(JWT)的库,例如php-jwt.我正在编写自己的,非常小而简单的类,但无法弄清楚为什么我的签名未能在此验证,即使我已经尝试坚持标准.我已经尝试了几个小时而且我被卡住了.请帮忙!
我的代码很简单
//build the headers
$headers = ['alg'=>'HS256','typ'=>'JWT'];
$headers_encoded = base64url_encode(json_encode($headers));
//build the payload
$payload = ['sub'=>'1234567890','name'=>'John Doe', 'admin'=>true];
$payload_encoded = base64url_encode(json_encode($payload));
//build the signature
$key = 'secret';
$signature = hash_hmac('SHA256',"$headers_encoded.$payload_encoded",$key);
//build and return the token
$token = "$headers_encoded.$payload_encoded.$signature";
echo $token;
Run Code Online (Sandbox Code Playgroud)
该base64url_encode
函数:
function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
Run Code Online (Sandbox Code Playgroud)
我的标头和有效负载与验证网站的默认JWT 完全匹配,但我的签名不匹配,因此我的标记被标记为无效.这个标准看起来真的很简单,所以我的签名有什么问题?
我的Angular 2应用程序(用typescript编码)有一个简单的身份验证方案:
abc123...
Authorization
标头中发送JWT现在我想添加websockets.我想知道如何在那里验证用户.由于我不控制哪些头发送到websocket服务器(WS),我无法发送JWT.
到目前为止我的想法(尚未实施):
let sock = new WebSocket('wss://example.com/channel/');
open
套接字上的事件.套接字打开后:
type='auth'
payload='JWT_VALUE'
auth
.收到后,服务器读取有效负载,验证JWT_VALUE
并设置isAuthenticated
标志
isAuthenticated
发送任何其他类型的消息,服务器将断开套接字2个问题:服务器资源可以由连接但从不发送JWT的客户端占用,如果客户端未经过身份验证,则更干净的解决方案会阻止握手.
其他想法:
new WebSocket('wss://example.com/channel/<JWT>/')
如何在websockets上验证客户端?假设用户已经通过HTTP登录并且Angular 2应用程序具有JWT令牌.
Angular 2 rc 6
写的 Typescript 2.0.2
我正在尝试学习如此概述的 Ahead-of-Time编译.看起来很简单:
ngc
而不是Typescript编译器来生成.ngfactory.ts
文件platformBrowserDynamic().bootstrapModule()
为platformBrowser().bootstrapModuleFactory()
我不确定如何将第一步应用到我的设置中.我用来gulp-typescript 2.13.6
将我的打字稿编译成JavaScript.
gulpfile.js
var ts = require('gulp-typescript');
var tsProject = ts.createProject('tsconfig.json', {
//Use TS version installed by NPM instead of gulp-typescript's built-in
typescript: require('typescript')
});
gulp.task('build-ts', function () {
return gulp.src(appDev + '**/*.ts')
.pipe(ts(tsProject))
.pipe(gulp.dest(appProd));
});
Run Code Online (Sandbox Code Playgroud)
所以我的问题是; 如何将指令集成到我的工具中?如何gulp-typescript
使用Angular Compiler?我试过了:
var tsProject = ts.createProject('tsconfig.json', {
typescript: require('@angular/compiler') // or '@angular/compiler-cli'
});
Run Code Online (Sandbox Code Playgroud)
这会抛出错误而不会运行ngc
.我也试过了
var tsProject = ts.createProject('tsconfig.json', …
Run Code Online (Sandbox Code Playgroud) 我对是的很陌生。我试图验证字段可以是遵循某个正则表达式的字符串,也可以是此类字符串的数组。
这是检查字符串与我的正则表达式匹配的工作示例
{ field: yup.string().matches(regex) }
Run Code Online (Sandbox Code Playgroud)
现在我想field
如果它有一个这样的字符串数组也是有效的:
{field: yup.array().of(yup.string().matches(regex))}
Run Code Online (Sandbox Code Playgroud)
但我如何将两者结合起来呢?我试过了:
{
field: yup.mixed().when('field', {
is: Array.isArray,
then: yup.array().of(yup.string().matches(regex)),
otherwise: yup.string().matches(regex)
})
}
Run Code Online (Sandbox Code Playgroud)
但我可以理解地得到了循环依赖错误,因为该字段依赖于自身。正确的语法是什么?
根据我的理解Angular 2 rc5
,要从另一个模块(不是AppModule
)作为单个模块提供服务到每个组件,即使是那些延迟加载的模块,我们也不会将该服务包含在该providers
模块的其他模块中.我们改为导出它RouterModule.forRoot()
并导入结果AppModule
根据文件:
SharedModule应仅在根AppModule导入时提供UserService.SharedModule.forRoot方法帮助我们应对这一挑战...... SharedModule没有
providers
...当我们将SharedModule添加到AppModule的导入时,我们调用forRoot.这样,AppModule获得导出的类,SharedModule同时提供单例UserService
提供程序
我真的很挣扎如何为延迟加载的路由提供第三方服务(imports
我的数组中的模块使用的服务AppModule
).我无法控制这个第三方模块,所以我不能只从该NgModule.providers
模块的数组中删除该服务,并将其放入RouterModule.forRoot()
我的服务中.
具体的服务MdIconRegistry
,这是providers
对MdIconModule
的Angular Material 2 alpha 7-3
.此服务用于注册svg图标,然后可以使用<md-icon svgIcon='iconName'>
标记显示在页面上.所以:
MdIconModule
在我的根目录中导入AppModule
AppComponent
该图标可见并且运行良好,但仅限于启动时加载的模块.延迟加载的模块无法看到这些图标,因此我怀疑Angular注入器不会注入相同的MdIconRegistry
服务实例.
tl; dr:我怎样才能从第三方模块中将服务作为我的延迟加载组件的单例?
这是一个演示问题的编码器(编码typescript
).
Angular 4
写的 Typescript 2.3.4
component.ts
/**
* Info that drives the tabs in the template. Array is filled
* in ngOnInit() once data is received from the server
*/
public tabs:Array<{
title:string,
description:string,
data:Array<{name:string}>
}>=[];
Run Code Online (Sandbox Code Playgroud)
component.html
<section *ngFor="let t of tabs">
...
<div *ngFor="let i of t.data">{{i.name}}</div>
^^^^^^
</section>
Run Code Online (Sandbox Code Playgroud)
编译错误
Angular:未定义标识符'name'.
<anonymous>
不包含这样的成员
起初我还以为这是链接到我的其他问题,但因为在形状没有歧义,这是不同的tabs
模式:组件清楚地表明,name
在各成员的属性data
阵列本身的每个成员的属性tabs
阵列.
这里发生了什么?
Angular的NgTemplateOutlet允许您将上下文传递给出口以进行属性绑定.
<ng-container *ngTemplateOutlet="eng; context: {$implicit: 'World'}"></ng-container>
<ng-template #eng let-name><span>Hello {{name}}!</span></ng-template>
Run Code Online (Sandbox Code Playgroud)
Angular *ngIf
允许您根据布尔条件嵌入一个或另一个模板:
<ng-container *ngIf="isConditionTrue; then one else two"></ng-container>
<ng-template #one>This shows when condition is true</ng-template>
<ng-template #two>This shows when condition is false</ng-template>
Run Code Online (Sandbox Code Playgroud)
如何将上下文传递给*ngIf
语法中引用的这些模板?
考虑 Go 中的这个单元测试文件。我正在使用github.com/stretchr/testify/mock
包。
type Person struct {Name string; Age int}
type Doer struct { mock.Mock }
func (d *Doer) doWithThing(arg Person) {
fmt.Printf("doWithThing %v\n", arg)
d.Called(arg)
}
func TestDoer(t *testing.T) {
d := new(Doer)
d.On("doWithThing", mock.Anything).Return()
d.doWithThing(Person{Name: "John", Age: 7})
// I don't care what Age was passed. Only Name
d.AssertCalled(t, "doWithThing", Person{Name: "John"})
}
Run Code Online (Sandbox Code Playgroud)
该测试失败,因为当我未超过年龄时testify
用于Age: 0
比较。我明白了,但我想知道,我该如何断言已通过的部分论点?我希望这个测试能够通过Age
,只要Name = John
我使用ComponentFactoryResolver
来动态创建组件,如文档中所示。
// create a component each time this code is run
public buildComponent() {
const factory = this.componentFactoryResolver.resolveComponentFactory(MyComponent);
const componentRef = this.viewContainerRef.createComponent(factory);
const component = componentRef.instance;
this.componentArray.push(component);
}
Run Code Online (Sandbox Code Playgroud)
这很好用。每次运行该函数时,都会MyComponent
在提供的ViewContainerRef
位置创建一个新函数并将其添加到 DOM 中。现在按照一些用户操作,我想对组件重新排序。例如,我可能想将最后创建的组件在容器中向上移动一个位置。这可以在 Angular 中完成吗?
public moveComponentUp(component) {
// What to do here? The method could also be passed the componentRef
}
Run Code Online (Sandbox Code Playgroud) 使用PhpStorm 2017.3开发node项目;我希望从索引和自动完成建议中排除某些目录。我在其他地方看到了我遵循的指示,但仍然没有骰子。例如,在下图中,该目录rxjs/add
就是其中之一。
在图像的左侧,您可以看到在项目树视图中,该目录未被排除;我从其内容中获得自动完成建议。但是,当我转到 时Settings > Directories
,同一文件夹(请参见图像的右侧)已被列为排除项。不知道该怎么办。这是包node_modules/
的目录rxjs 5.5
。
我实际上正在尝试解决的问题:该库具有来自不同文件的多个具有相同名称的函数。因此,当我使用某个函数并且 IDE 尝试自动添加 ES6 导入语句时,它会向我显示多个选项,每次我都必须仔细阅读文件路径以选择正确的导入。因为正确的导入始终来自相同的两个目录,所以我想告诉 IDE 停止查看其他目录。
angular ×6
typescript ×5
jwt ×2
deployment ×1
go ×1
gulp ×1
if-statement ×1
javascript ×1
json ×1
mocking ×1
module ×1
node-modules ×1
php ×1
phpstorm ×1
sha ×1
templates ×1
testify ×1
unit-testing ×1
validation ×1
websocket ×1
webstorm ×1
yup ×1