我在其中設定 SecurityConfig 設定的課程。
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.cors()
.and()
.csrf().disable()
.exceptionHandling().authenticationEntryPoint(handler).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
.antMatchers("/auth/**")
.permitAll()
.anyRequest().authenticated();
httpSecurity.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}
}
我在哪里實作 AuthController.AuthController.java @RequestMapping("/auth")
@PostMapping("/register")
public ResponseEntity<String> register(@RequestBody UserRequst registerRequst){
if (userService.getOneUserByUserName(registerRequst.getUsername())!=null)
return new ResponseEntity<>("Username already in use.", HttpStatus.BAD_REQUEST);
User user = new User();
user.setUserName(registerRequst.getUsername());
user.setPassword(passwordEncoder.encode(registerRequst.getPassword()));
userService.userSave(user);
return new ResponseEntity<>("user successfully registered",HttpStatus.CREATED);
}
當我從 Postman 向 localhost:8080/auth/register 發送 post 請求時,我收到此錯誤。我找不到原因。我是 Java 后端的新手。我該如何修復錯誤。
java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
at org.springframework.web.cors.CorsConfiguration.validateAllowCredentials(CorsConfiguration.java:473) ~[spring-web-5.3.8.jar:5.3.8]
at org.springframework.web.cors.CorsConfiguration.checkOrigin(CorsConfiguration.java:577) ~[spring-web-5.3.8.jar:5.3.8]
at org.springframework.web.cors.DefaultCorsProcessor.checkOrigin(DefaultCorsProcessor.java:174) ~[spring-web-5.3.8.jar:5.3.8]
at org.springframework.web.cors.DefaultCorsProcessor.handleInternal(DefaultCorsProcessor.java:116) ~[spring-web-5.3.8.jar:5.3.8]
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
我使用的作業系統是 Ubuntu 21.04。
uj5u.com熱心網友回復:
問題出在錯誤訊息中
java.lang.IllegalArgumentException:當allowCredentials 為true 時, allowedOrigins 不能包含特殊值“*”,因為它不能在“Access-Control-Allow-Origin”回應頭中設定。要允許一組來源的憑據,請明確列出它們或考慮改用“allowedOriginPatterns”。
由于config.setAllowCredentials(true);您config.addAllowedOrigin("*");必須包含一個特定 URL,該 URL 是嘗試訪問資源的來源。也許http://localhost:3000如果您正在撰寫基于 javascript 的前端。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380137.html
