【发布时间】:2019-10-14 04:26:17
【问题描述】:
error message in google chrome当我尝试删除一个项目时,我收到了一个错误。
当我向后端添加 Spring Security 时,我遇到了 csrf 的问题,并且无法在 Angular 中显示来自数据库的数据。所以,我做了一些步骤:
我已将@WebSecurityConfigurerAdapter 添加到我的后端。后来在Angular中设置了Interceptor,之后我就可以在后台使用spring security加载我的数据了。
@EnableWebSecurity
@Configuration
public class SpringSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS,"/**").permitAll()
.anyRequest().authenticated()
.and()
//.formLogin().and()
.httpBasic();
}
}
我使用 Interceptor 的 Angular 部分服务
import { Injectable } from '@angular/core';
import {HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class HttpInterceptorService implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler) {
const username = 'damian';
const password = 'damian';
const basicAutHeaderString = 'Basic ' + window.btoa(username + ':' + password);
request = request.clone(
{
setHeaders: {
Authorization: basicAutHeaderString
}
}
);
return next.handle(request);
}
constructor() { }
}
从后端获取单个项目:
deleteSingleTodo(id) {
return this.http.delete<Todo>(`http://localhost:8081/${id}`);
}
我的删除方法是由(点击)按钮触发的:
deleteTodo(id) {
console.log(`delete todo ${id}` )
this.getDataService.deleteSingleTodo( id).subscribe (
response => {
console.log(response);
this.message = `Delete of Todo ${id} Successful!`;
this.refreshTodos();
}
);
}
I also provided HTTP_INTERCEPTORS in app.module.ts
providers: [
{ provide: HTTP_INTERCEPTORS, `useClass: HttpInterceptorService, multi:` true}
],[my error message in google][1]
【问题讨论】:
-
请永远不要将错误消息添加为图像。您在哪里配置 CORS 标头?
-
好的,谢谢你的建议。我在设置拦截器之前得到了标题,现在当我使用拦截器时,它应该在整个应用程序中可见,但只有从数据库中获取所有项目才有效。我是这个全栈世界的初学者。
标签: angular spring-boot cors