実現したいこと
クライアントからリクエストを投げてAPIに処理させる
前提
VueとSpringbootでログインをするシステムを作っています。
spring securityを使っています。
CORSエラーが出ます。
発生している問題・エラーメッセージ
エラーメッセージ Access to XMLHttpRequest at 'http://localhost:8080/login' from origin 'http://localhost:8081' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Login.vue
1login() { 2 axios 3 .get("http://localhost:8080/login", { 4 username: this.username, 5 password: this.password, 6 }) 7 .then((response) => { 8 console.log(response); 9 }) 10 .catch((error) => { 11 console.error(error); 12 this.showError = true; 13 }); 14 },
SecurityConfig.java
1@Configuration 2@EnableWebSecurity 3public class SecurityConfig { 4 5 @Bean 6 SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { 7 8 http 9 .cors(cors -> cors.disable()) 10 .authorizeHttpRequests() 11 .requestMatchers("/login").permitAll() 12 .anyRequest().authenticated() 13 .and() 14 .formLogin(login -> login 15 .loginPage("/login")); 16 return http.build(); 17 } 18}
LoginController.java
1@RestController 2@RequestMapping("/") 3@CrossOrigin(origins = "http://localhost:8081") 4public class LoginController { 5 @GetMapping("/login") 6 public String showLoginForm() { 7 return "login"; 8 } 9 10 @GetMapping("/logout") 11 public String showLogoutForm() { 12 return "logout"; 13 } 14}

回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2023/05/12 04:37