【发布时间】:2021-11-18 19:25:24
【问题描述】:
我正在练习 Angular + Spring(使用 Spring Security 基本身份验证)。
我无法发出 PUT 请求来更新我的实体。
服务器响应 405 Method Not Allowed。
HTTP/1.1 405
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Access-Control-Allow-Origin: *
Set-Cookie: JSESSIONID=90B569C7D540CC684DB87C9FCEEED922; Path=/; HttpOnly
Allow: GET, POST
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 26 Sep 2021 08:24:47 GMT
Keep-Alive: timeout=60
Connection: keep-alive
有一个使用 POST 请求的解决方法。我试过了,效果很好。
但我确实想知道如何配置 Spring Security 以使 PUT 请求有效。
我的 Angular 和 Spring 实践代码:
由 Angular 向服务器发送 PUT 请求。
@Injectable({
providedIn: 'root'
})
export class HeroService {
private heroesUrl = 'http://localhost:8080/api/heroes';
private httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Basic dXNlcjpwYXNzd29yZA=='
})
};
/** PUT: update the hero on the server */
updateHero(hero: Hero): Observable<any> {
return this.http.put(this.heroesUrl, hero, this.httpOptions).pipe(
tap(_ => this.log(`updated hero id=${hero.id}`)),
);
}
}
它是put请求的控制器
@RestController
@CrossOrigin
@RequestMapping(path = ["/api/heroes"], produces = ["application/json"])
class HeroController(private val heroService: HeroService) {
@PutMapping(path = ["/{id}"], consumes = ["application/json"])
fun updateOrCreate(@PathVariable id: Long, @RequestBody hero: Hero): ResponseEntity<Hero> {
return if (heroService.existsById(id)) {
hero.id = id;
ResponseEntity<Hero>(heroService.update(hero), HttpStatus.OK);
} else {
ResponseEntity<Hero>(heroService.create(hero), HttpStatus.CREATED);
}
}
}
【问题讨论】:
标签: angular spring authentication spring-security