java - 如何在 Angular 5 中未经授权使用主页中的实体数据
问题描述
你好我想尝试在主页中未经授权使用实体数据,这是我的一些代码
图表组件
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();
}
推荐阅读
- webpack - Webpack 5 入口点现在无法导入
- authentication - 如何管理用于本地开发的 Google Cloud 凭据
- google-apps-script - 如果该行中的两个单元格不匹配,则发送该行数据的电子邮件
- time-series - 我可以将时间序列数据存储在数据仓库中吗?
- c# - foreach 循环将数据库项添加到结果列表的更有效方法?
- django - Django model_formset 无法编辑、保存或删除信息
- azure-blob-storage - 有没有办法通过发布管道激活 Azure Blob 存储故障转移?
- kubernetes - Kubectl 获取 JSON 提取
- ruby-on-rails - 主页索引问题
- visual-c++ - MSVC 单个 cpp 文件永远不会完成编译