Spring引导、JWT和angular不工作:方法put的HTTP状态代码403错误
我是新来的角度和春季安全。我在从angular更新数据时遇到了问题,但我可以在测试api时使用Dominasure进行更新(我在头中传递了令牌) 我正在将令牌传递给put方法,但我的访问被拒绝403 这是我在角度服务中的功能Spring引导、JWT和angular不工作:方法put的HTTP状态代码403错误,angular,spring-boot,rest,http,spring-security,Angular,Spring Boot,Rest,Http,Spring Security,我是新来的角度和春季安全。我在从angular更新数据时遇到了问题,但我可以在测试api时使用Dominasure进行更新(我在头中传递了令牌) 我正在将令牌传递给put方法,但我的访问被拒绝403 这是我在角度服务中的功能 private headers = new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': this.authService.jwt }); updateUs
private headers = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': this.authService.jwt
});
updateUserById(id: String): Observable<any> {
console.log(this.headers);
return this.http.put(this.host_3 + '/' + id, {headers: this.headers});
}
这是我在spring controller中的后端函数
onSubmit(){
this.updateCustomer();
}
updateCustomer(){
console.log("inside update"+this.userr);
this.coordservice.updateUserById(this.userr).
subscribe(data => console.log("data"+data), error => console.log(error));
}
@PutMapping(value = "/customersList/updateCustomer/{idCustomer}")
public ResponseEntity<Customer> update(@PathVariable(name = "idCustomer") String idCustomer, @RequestBody Customer customer){
this.customerService.updateCustomer(idCustomer,customer);
return new ResponseEntity<Customer>(customer, HttpStatus.NO_CONTENT);
}
注意:我可以使用http.get()获取数据,但对于方法put i get access denied您可能还需要添加选项方法作为允许的方法,如下所示
response.addHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE, PATCH");
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/public-api-such-as-login").permitAll()
.anyRequest().authenticated();
}
对于CORS,浏览器将发送选项请求,以验证其是否正在发出更正服务器的请求(称为预授权请求),如果它将成功响应,则实际请求已启动
CSRF令牌在安全性方面也发挥着重要作用。您可能需要根据自己的要求对其进行配置 要禁用,可以使用
.csrf().disable()
,如下所示
response.addHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE, PATCH");
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/public-api-such-as-login").permitAll()
.anyRequest().authenticated();
}
如果您想包括CSRF,您可以使用以下CookieSRFTokenRepository
。你也可能喜欢探索
您可能还需要添加选项方法作为允许的方法,如下所示
response.addHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE, PATCH");
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/public-api-such-as-login").permitAll()
.anyRequest().authenticated();
}
对于CORS,浏览器将发送选项请求,以验证其是否正在发出更正服务器的请求(称为预授权请求),如果它将成功响应,则实际请求已启动
CSRF令牌在安全性方面也发挥着重要作用。您可能需要根据自己的要求对其进行配置 要禁用,可以使用
.csrf().disable()
,如下所示
response.addHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE, PATCH");
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/public-api-such-as-login").permitAll()
.anyRequest().authenticated();
}
如果您想包括CSRF,您可以使用以下CookieSRFTokenRepository
。你也可能喜欢探索
可能由于CORS预飞行而被阻止,您可以尝试首先在访问控制允许方法中添加选项:response.addHeader(“访问控制允许方法”、“获取、发布、放置、删除、修补、选项”);-如果您使用响应标题编辑问题,这将非常有用。这看起来像是来自Spring security的XSRF保护。可能由于CORS预飞行而被阻止,您可以尝试首先在访问控制允许方法中添加选项:response.addHeader(“访问控制允许方法”、“获取、发布、放置、删除、修补、选项”);-如果您使用响应标题编辑问题,这将非常有用。该标题看起来像是来自Spring security的XSRF保护。