0
0

More than 1 year has passed since last update.

SpringBoot Nextjs 認可が必要なapiにアクセスできない 原因はSpringSecurityの設定

Posted at

症状

ローカル環境においてNextjsからSpringBootのapiを使おうとしたところCORエラーになりました。
エラーになったapiは認可が必要であり、認可が不必要なapiはCORエラーになりませんでした。

認可はSpringSecurityを使っています。

開発環境

OS:windows10

バックエンド側:
IDE:IntelliJ Community
spring-boot-starter-parent 2.75
java : 11

Postman:Windows版 検証用

フロントエンド:
IDE:VScode

├── @types/node@18.11.15
├── @types/react-dom@18.0.9
├── @types/react@18.0.26
├── eslint-config-next@13.0.6
├── eslint@8.29.0
├── next@13.0.6
├── react-bootstrap@2.7.0
├── react-dom@18.2.0
├── react@18.2.0
├── styled-components@5.3.6
├── styled-jsx@5.1.1
└── typescript@4.9.4

原因

SpringSecurityのCORの設定が未実施だったのが原因でした。
認可の不必要なapiが普通に使えたのはSpringSecurityのフィルターを経由しないで、SpringMvcにか使えたという(憶測)

解決方法

SecurityConfigを以下のように設定。
SpringSecurityのCOR設定をSpringmvc(@CrossOrigin)と同じようにすることができるようです。

SecurityConfig.java

        http.cors(withDefaults())

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0