Java 如何使用实体';在angular 5中未经授权在主页中添加数据

Java 如何使用实体';在angular 5中未经授权在主页中添加数据,java,angular,angular5,jhipster,Java,Angular,Angular5,Jhipster,您好,我想尝试在angular中未经授权使用主页中的实体数据,这是我的一些代码 图表组件 isSaving: boolean; mass = []; directions: Direction[]; constructor(private jhiAlertService: JhiAlertService, private directionService: DirectionService, ) { } ngOnInit() { // res: HttpRe

您好,我想尝试在angular中未经授权使用主页中的实体数据,这是我的一些代码

图表组件

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);
}
isSaving:boolean;
质量=[];
方向:方向[];;
建造商(私人jhiAlertService:jhiAlertService,
私人定向服务:定向服务,
) {
}
恩戈尼尼特(){
//res:HttpResponse;
this.isSaving=false;
this.directionService.query()
.订阅((res:HttpResponse)=>{
这个方向=物体;
},(res:HttpErrorResponse)=>this.onError(res.message));
}
私有onError(错误:任何){
this.jhiAlertService.error(error.message,null,null);
}
ChartsComponent.html

<div *ngFor="let d of directions">
{{d.id}} {{d.name}}
</div>

{{d.id}{{d.name}
在home.component.html中,我只调用

但是控制台
GET中有一个错误http://localhost:9060/api/directions 401(未经授权)


提前感谢您

看起来您使用的是
JHipster
(通过标签)
JHipster
在引擎盖下使用
Spring Boot
,为了让您匿名访问一些
REST
端点,您应该在配置类文件中,比如-
java/config/SecurityConfiguration
(它扩展了
WebSecurityConfigurerAdapter
类)提供对它的访问

找到方法
protectedvoidconfigure(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();
}

看起来您使用的是
JHipster
(通过标记)
JHipster
在引擎盖下使用
Spring Boot
,为了让您匿名访问一些
REST
端点,您应该在配置类文件中,比如-
java/config/SecurityConfiguration
(它扩展了
WebSecurityConfigurerAdapter
类)提供对它的访问

找到方法
protectedvoidconfigure(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无法为您提供任何帮助,但是我可以为api/方向提供访问权限吗?是的,但是您必须向后端提供访问权限的证明,即您需要使用授权。我同意@user184994。因为如果你不提供证据,意味着你的API将是开放的,不安全的。另一种选择是将你的数据呈现到模板中,然后从那里读取。这将取决于后端,Angular没有什么可以做的,但是我可以提供API/方向的访问权限吗是的,但是你必须向后端提供访问权限的证据,也就是说,您需要使用授权我同意@user184994。因为如果你不提供证据,意味着你的API将是开放的,不安全的。另一个选择是将你的数据呈现到模板中并从那里读取。