在 https://github.com/spring-projects/spring-authorization-server/tree/main/samples#spa-single-page-application-sample 这个 SPA (Single Page Application) Sample 案例
资源服务器的 post 请求为什么不可以,应该是除了 get 请求外都不行,其他没试,除 get 外,其他请求还需要配置什么?前端服务器 spa-client 有一个退出的 post 方法,我就是按这个方法增加了一个资源服务器的 post 请求资源,不行,需要怎么修改?哪位大佬帮忙看看,应该怎么改其他请求都支持。
就测试了post,修改如下:
资源服务器 messages-resource 的 MessagesController.java 中增加了一个post请求如下:
@PostMapping("/messages/post")
public String[] postMessages() {
return new String[] {"post Message 1", "post Message 2", "post Message 3"};
}前端服务器 spa-client 的 app.component.ts 修改如下
// ...
// 增加
messages2: string[] = [];
// ..
ngOnInit(): void {
// ..
// 增加
this.postMessages();
}
// ...
// 增加
postMessages(): void {
this.http.post<string[]>('/messages/post', null)
.pipe(catchError((error) => {
console.error(error);
return of([]);
}))
.subscribe((messages) => {
this.messages2 = messages;
});
}app.component.html 修改如下(就是一个展示,这个可不修改,主要是请求报错)
// 增加
<div *ngIf="messages2.length > 0" class="row py-5 justify-content-start">
<div class="col">
<table class="table table-striped caption-top">
<caption>Messages2</caption>
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Message2</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let message2 of messages2; let index = index">
<th scope="row">{{ index + 1 }}</th>
<td>{{ message2 }}</td>
</tr>
</tbody>
</table>
</div>
</div>刚测试了一下,在资源服务器 messages-resource 的 MessagesController.java 中多增加了两个get请求,还需要在服务器 backend-for-spa-client 的配置文件 application.yml 节点 routes 也要增加一次,为什么我用通配符不行只能一个一个增加,应该怎么配置?
predicates:
// 不行
- Path=/messages** @GetMapping("/messages/get")
public String[] getGetMessages() {
return new String[] {"get Message 1", "get Message 2", "get Message 3"};
}
@GetMapping("/messages/get1")
public String[] get1GetMessages() {
return new String[] {"get1 Message 1", "get1 Message 2", "get1 Message 3"};
}已搞定,新版本配置问题
spring:
cloud:
gateway:
server:
webmvc:
routes:
- id: userinfo
uri: http://localhost:9000
predicates:
- Path=/userinfo
filters:
- TokenRelay=
- id: messages
uri: http://localhost:8090
predicates:
- Path=/messages/**
filters:
- RelayTokenIfExists=messaging-client-authorization-code
已搞定,新版本配置问题