java - 如何在 Angular 5中未经授权使用主页中的实体数据

标签 java angular angular5 jhipster

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

图表组件

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/

相关文章:

css - <mat-card> 没有凸起 - 没有边框

javascript - 访问组件内部的模板变量

java - 使用线程编程的方法

java - 套接字批量读取返回一个零数组

java - 使用 crawler4j 发送请求中的 cookie?

Angular 2延迟加载重定向到空白页面

json - 将 JSON 发送到 Web 服务时如何忽略 TypeScript 中的实体字段?

java - 从 ant 脚本执行 java 类文件

angular - sidenav 生成已经声明结束的抽屉

modal-dialog - 在 JHipster 5.0.0 或更高版本中创建一个类似于 LoginModal Popup 的 CustomUpdate Modal Dialog