我已经开发了REST API和两个JavaScript客户端(单页应用和本机应用-基于电子)。在这两个客户端中,我的用户都通过OAuth2流进行身份验证:
现在,我想实现csrf保护。所以我在后端(春季)实现了它:
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
.authorizeRequests().antMatchers("/", "/index.html", "/token/**").permitAll()
.anyRequest().authenticated();
}
Run Code Online (Sandbox Code Playgroud)
我的SPA完美运行,Angular从cookie读取XSRF-TOKEN并将其发送到X-XSRF-TOKEN标头中。电子应用程序出现问题。它无权访问Cookie(由于来源不同-电子在file://url 上运行),因此无法设置X-XSRF-TOKEN标头。
我该如何处理?有什么方法可以使“跨域” cookie无效?或者,也许我可以通过电子魔术电子API以某种方式更改cookie的值(如果它可以访问文件系统,也许可以访问在计算机上创建的任何cookie)?
我在gitlab上托管了项目存储库.我正在使用gitlab-ci从我的项目构建docker容器.我想要实现的是将该容器部署到heroku.
我试图遵循这个问题的解决方案:如何使用Jhipster,Docker,Gitlab和Heroku构建,测试和部署
这是我的.gitlab-ci.yaml样子:
stages:
- build
- package
- deploy
build_npm:
image: node:latest
stage: build
script:
- npm install
- npm run build:prod
artifacts:
paths:
- dist/
build_image:
image: docker:latest
services:
- docker:dind
stage: package
script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
- docker build -t registry.gitlab.com/maciejsobala/myApp .
- docker push registry.gitlab.com/maciejsobala/myApp:latest
deploy_to_heroku:
stage: deploy
services:
- docker:dind
script:
- gem install dpl
- docker run registry.gitlab.com/maciejsobala/myApp:latest
- dpl --provider=heroku --app= myApp --api-key=$HEROKU_API_KEY
Run Code Online (Sandbox Code Playgroud)
我想要实现的是,有3个阶段:
在我的角度2项目中,我无法通过导航到相同的URL但使用不同的查询参数来重新加载页面.我正在使用resolve在加载页面时预取数据.
当URL(查询参数)发生变化时,我试图让解析器重新获取数据.我该怎么做呢?
我想要实现的是我想在angular2中全局发出一个自定义事件并让多个组件监听它,而不仅仅是父子模式
在我的事件源组件中,我有
export class EventSourceComponent{
@Output() testev = new EventEmitter();
onbtnclick(){
this.testev.emit("i have emitted an event")
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想要其他组件来获取此事件
export class CmpTwoComponent{
//here get the emitted event with data
}
Run Code Online (Sandbox Code Playgroud)
我如何实现上述目标?
让我们假设我有以下模型:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MyModel {
private Object field; //required field
private Object anotherField; //optional field
}
Run Code Online (Sandbox Code Playgroud)
现在我想验证我的 REST 端点是否正常工作(只需要必填字段),所以我想执行以下 2 个请求并检查它们是否导致 200:
{
"field": "someValue",
"anotherField": null
}
Run Code Online (Sandbox Code Playgroud)
和:
{
"field": "someValue"
}
Run Code Online (Sandbox Code Playgroud)
第一个,代码很简单:
MyModel payload = MyModel.builder().field("someValue").build();
when().contentType(ContentType.JSON).body(payload).put("http://my-service.com/myendpoint/").then().statusCode(200);
Run Code Online (Sandbox Code Playgroud)
但第二个开始有点蹩脚。如果我不为 提供值anotherField,默认情况下它将为空,这就是将通过 REST(第一个 TC)发送的内容。但此刻我不想发送这个字段。换句话说:
nulls. 有什么方法可以实现吗?我需要获取页面标题并将其写入变量。我正在使用打字稿代码:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'home',
templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {
title: string;
ngOnInit() {
console.log("Title is " + this.title);
this.title === document.title;
}
}
Run Code Online (Sandbox Code Playgroud)
但我在控制台上收到“标题未定义”。
我也尝试过:
title: string;
ngOnInit() {
this.title === this.titleService.getTitle();
console.log("Title is " + this.title);
}
public constructor(private titleService: Title) { }
getTitle() {
this.titleService.getTitle();
}
Run Code Online (Sandbox Code Playgroud)
但结果是一样的。获取页面标题的正确方法是什么?
我试图在我的Angular应用程序中包含外部模块(托管在git/npm存储库中)作为延迟加载的模块.
我用ngc编译器编译我的外部模块:
node_modules/.bin/ngc -p tsconfig-aot.json
这是我的编译器配置的外观:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"baseUrl": "src",
"declaration": true,
"outDir": "./release/src"
},
"files": [
"./src/index.ts"
],
"angularCompilerOptions": {
"genDir": "release",
"skipTemplateCodegen": true,
"entryModule": "index#ExternalModule",
"skipMetadataEmit": false,
"strictMetadataEmit": true
}
}
Run Code Online (Sandbox Code Playgroud)
在我的主应用程序中,我懒得加载给定模块:
RouterModule.forRoot([
{ path: '', component: HomeComponent, pathMatch: 'full'},
{ path: 'lazy', loadChildren: './lazy/lazy.module#LazyModule'},
{ path: 'external', loadChildren: '@angular-universal-serverless/external-module/release#ExternalModule'}
])
Run Code Online (Sandbox Code Playgroud)
出于编译目的,我使用的是@ ngtools/webpack插件.
JIT编译没有任何问题,但AOT编译给我错误:
ERROR in ./src/ngfactory lazy
Module not found: Error: Can't resolve '/path/to/my/project/angular-universal-serverless/src/ngfactory/node_modules/@angular-universal-serverless/external-module/release/src/index.js' in '/Users/mtreder/Documents/private/work/angular-universal-serverless/src/ngfactory'
@ ./src/ngfactory lazy
@ ./~/@angular/core/@angular/core.es5.js
@ ./src/main.server.aot.ts
Run Code Online (Sandbox Code Playgroud)
所以我决定检查 …
我有哈希问题,在我构建它时在我的工作项目上,在测试项目上一切正常。我已经在谷歌中阅读了这个问题: Angular2 without hash in the url https://forum.ionicframework.com/t/url-routing-without-hash/81140 但我没有找到答案。当我添加
{provide: LocationStrategy, useClass: HashLocationStrategy}
Run Code Online (Sandbox Code Playgroud)
一切正常,但带有哈希值。当我添加
{provide: LocationStrategy, useClass: PathLocationStrategy}
Run Code Online (Sandbox Code Playgroud)
它仅适用于本地版本。但是工作版本不起作用http://joxi.ru/n2YLOaMijK7Pam 我怎样才能删除这个哈希http://joxi.ru/RmzBzxDTWbjeQm它对我的构建项目有什么作用?
app.module.ts
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {AppComponent} from './app.component';
import {MaterialModule} from '../shared/mat.module';
import {UserModule} from './user/user.module';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {AppRoutingModule} from './app-routing.module';
import {ToolbarComponent} from './toolbar/toolbar.component';
import {HashLocationStrategy, LocationStrategy, PathLocationStrategy} from '@angular/common';
import { TestingRComponent } from './testing-r/testing-r.component';
@NgModule({
declarations: [
AppComponent,
ToolbarComponent,
TestingRComponent
],
imports: [
BrowserModule,
MaterialModule,
UserModule, …Run Code Online (Sandbox Code Playgroud) 我的应用程序正在使用 Angular 和 ngrx 商店。我在商店中使用选择器来获取组件构造函数中应用程序状态的用户名属性:
测试组件.ts
export class TestComponent {
username$: Observable<string>;
constructor(private store: Store<fromRoot.AppState>) {
this.username$ = this.store.select(fromRegistration.getUsername);
}
testMethod() {
//I need the value of the username observable here as a string
}
}
Run Code Online (Sandbox Code Playgroud)
这工作正常。我需要在组件的模板中打印用户名,这是我使用异步管道完成的:
测试组件.html
<div>
{{username | async}}
</div>
Run Code Online (Sandbox Code Playgroud)
这也正常工作。
现在,我需要从 TestComponent 中的一个方法调用一个服务,该方法将用户名作为参数发送。我怎样才能做到这一点?我是否需要停止使用异步管道并订阅选择器,然后将 username 属性(然后将其声明为字符串)分配给 observable 的值?
注意:用户名 observable 只有 1 个值,一旦我得到 1 个值,我就可以停止观看它。
我知道通过使用异步管道组件会自动从用户名 observable 取消订阅,这什么时候发生,组件如何知道我不再需要观看它?
如果我不能使用异步管道,我应该如何以及何时取消订阅?
我用NG-工具与ng add @ng-toolkit/universal加角通用的支持,我的项目。
我能够创建没有错误的 prod 构建,而且我能够再次运行服务器,没有任何错误。当请求到达它时,它只会“卡住”(nodeJS 不呈现任何输出)。
我发现,我的一个组件正在破坏服务器端渲染。我发现问题出在 Mat-Carousel 上:
成分:
export class BannerComponent {
slides: any[] = [
// tslint:disable-next-line:max-line-length
{ image: 'assets/banner/banner-one.png' },
// tslint:disable-next-line:max-line-length
{ image: 'assets/banner/banner-two.png' },
// tslint:disable-next-line:max-line-length
{ image: 'assets/banner/banner-three.png' }
];
}
Run Code Online (Sandbox Code Playgroud)
模板:
<section class="sec-space-b" id="banner">
<mat-carousel
timings="250ms ease-in"
[autoplay]="true"
interval="5000"
color="accent"
maxWidth="auto"
proportion="25"
slides="5"
[loop]="true"
[hideArrows]="false"
[hideIndicators]="false"
[useKeyboard]="true"
[useMouseWheel]="false"
orientation="ltr"
>
<mat-carousel-slide
#matCarouselSlide
*ngFor="let slide of slides; let i = index"
overlayColor="#00000000"
[image]="slide.image"
[hideOverlay]="false"
></mat-carousel-slide>
</mat-carousel>
</section>
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?我可以以某种方式从服务器端构建中排除特定组件吗?