angular 5 如何在首页私自使用实体数据
How to use entity's data in home page without authorization in angular 5
你好,我想尝试在 angular 中未经授权使用主页中的实体数据,这是我的一些代码
ChartsComponent
isSaving: boolean;
mass = [];
directions: Direction[];
constructor(private jhiAlertService: JhiAlertService,
private directionService: DirectionService,
) {
}
ngOnInit() {
// res: HttpResponse<Direction[]>;
this.isSaving = false;
this.directionService.query()
.subscribe((res: HttpResponse<Direction[]>) => {
this.directions = res.body;
}, (res: HttpErrorResponse) => this.onError(res.message));
}
private onError(error: any) {
this.jhiAlertService.error(error.message, null, null);
}
ChartsComponent.html
<div *ngFor="let d of directions">
{{d.id}} {{d.name}}
</div>
在 home.component.html 中我只调用 <jhi-charts></jhi-charts>
但控制台出现错误GET http://localhost:9060/api/directions 401 (Unauthorized)
提前致谢
您似乎使用了 JHipster
(按标签)。 JHipster
在幕后使用 Spring Boot
,并让您匿名访问某些 REST
端点,您应该在配置 class 文件中像 - java/config/SecurityConfiguration
(它扩展WebSecurityConfigurerAdapter
class) 提供访问权限。
找出方法protected void configure(HttpSecurity http)
,加.antMatchers("/api/**").permitAll()
给予权限。
例如,我的配置如下:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/login").permitAll()
.antMatchers("/admin/**").hasAnyAuthority(rolesForAdmin)
.antMatchers("/subscriber-register/**").permitAll()
.and()
.csrf().disable().httpBasic().disable().cors();
}
你好,我想尝试在 angular 中未经授权使用主页中的实体数据,这是我的一些代码
ChartsComponent
isSaving: boolean;
mass = [];
directions: Direction[];
constructor(private jhiAlertService: JhiAlertService,
private directionService: DirectionService,
) {
}
ngOnInit() {
// res: HttpResponse<Direction[]>;
this.isSaving = false;
this.directionService.query()
.subscribe((res: HttpResponse<Direction[]>) => {
this.directions = res.body;
}, (res: HttpErrorResponse) => this.onError(res.message));
}
private onError(error: any) {
this.jhiAlertService.error(error.message, null, null);
}
ChartsComponent.html
<div *ngFor="let d of directions">
{{d.id}} {{d.name}}
</div>
在 home.component.html 中我只调用 <jhi-charts></jhi-charts>
但控制台出现错误GET http://localhost:9060/api/directions 401 (Unauthorized)
提前致谢
您似乎使用了 JHipster
(按标签)。 JHipster
在幕后使用 Spring Boot
,并让您匿名访问某些 REST
端点,您应该在配置 class 文件中像 - java/config/SecurityConfiguration
(它扩展WebSecurityConfigurerAdapter
class) 提供访问权限。
找出方法protected void configure(HttpSecurity http)
,加.antMatchers("/api/**").permitAll()
给予权限。
例如,我的配置如下:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/login").permitAll()
.antMatchers("/admin/**").hasAnyAuthority(rolesForAdmin)
.antMatchers("/subscriber-register/**").permitAll()
.and()
.csrf().disable().httpBasic().disable().cors();
}