你好,我想尝试在未经授权的情况下在主页中使用实体的数据,这是我的一些代码
图表组件
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
端点,您应该在配置类文件中,例如 - java/config/SecurityConfiguration
(它扩展了 WebSecurityConfigurerAdapter
类)提供对其的访问。
找到方法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();
}
关于java - 如何在 Angular 5中未经授权使用主页中的实体数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52015543/