问题:当从 Angular hhtp RestClient 触发 POST 请求时,nginx 网关返回 403
在 /etc/nginx/sites-available/default 上:
server {
listen 443 ssl;
server_name peertube-seeker.com;
root /var/www/peertube-seeker;
index index.html;
ssl_certificate /etc/nginx/ssl/certificate.cer;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
location / {
try_files $uri $uri/ =404;
}
location /api/auth/ {
proxy_pass http://localhost:8085/;
proxy_redirect off;
}
}
休息控制器:
@RestController
@RequestMapping("/account")
@RequiredArgsConstructor
public class AccountRest {
@PostMapping("/login")
public ResponseEntity<Logation> login(@RequestBody LoginDto loginDto)
{
return userRestControllerService.login(loginDto);
}
}
Srping 安全过滤器链配置
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception
{ http
.csrf((csrf) -> csrf.disable())
.sessionManagement((session) -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)
.authorizeHttpRequests((requests) -> requests
.requestMatchers("/account/login").permitAll()
.anyRequest().authenticated()
);
http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
在 ubuntu 服务器上:systemctl status nginx.service
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running)
在 ubuntu 服务器上:java -jar authent.module-0.1.0-SNAPSHOT.jar
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8085 (http) with context path
在 ubuntu 服务器上:部署 Angular 应用程序。
在角度应用程序上:当触发登录请求时,nginx 返回 403
this.http.post<LoginInfo>(`${environment.apiUrl}/api/auth/login`, { email, password })
备注 1:我有一些 GET 请求工作正常,请参阅下面的示例。
@GetMapping("/user/maxaccountallowed")
public ResponseEntity<Integer> getNbAuthorizedAccount(){
Integer nbAuthaurizedAccount = globalCaracteristicsService.maxAcccountAllowed();
return new ResponseEntity<Integer>(nbAuthorizedAccount, HttpStatus.OK);
}
问题:使用 POST 时是否有一些特殊问题
我假设您的 REST API 的系统路径如下:
/var/www/peertube-seeker/api/auth/帐户/登录
不是这个:
/var/www/peertube-seeker/帐户/登录
如果我的假设是正确的,请删除 proxy_pass https://localhost:8085/ 中的尾部斜杠;
location /api/auth/ {
proxy_pass https://localhost:8085;
...
}