我有一个角形5应用程序,使我的Spring服务器的POST请求。在后端所做的只是将方向重定向到我的角度应用程序的另一个位置。(我这样做是因为我在模拟外部服务向后端发出POST请求时必须执行的重定向),但是当从我的角度应用程序向后端发出post请求以执行重定向时,会出现一个问题:
从源'resume?state=1&billNumber=234343&description=descr&value=120.000&method=Visa‘(从’http://localhost:8083/v1/redirect‘重定向)的访问“null”已被CORS策略阻止:在飞行前响应中,访问控制允许允许头部授权是不允许的。
我不明白为什么CORS会出现问题,如果我在后端进行了调试,并且请求被我的服务器接受并处理,给出了一个重定向响应.当我的后端在我的角度应用程序中用重定向响应时,会出现错误.
后端代码:
@RequestMapping(value = "/redirect", method = RequestMethod.POST)
@Transactional
public void returnData(UriComponentsBuilder uriComponentsBuilder, final HttpServletRequest request,
final HttpServletResponse response) throws IOException {
String transactionState="1";
String billCode="234343";
String description="descr";
String billValue="120.000";
String paymentMethod="Visa";
response.sendRedirect(hostname+"/payment_resume?state="+ transactionState
+"&"+"billNumber="+billCode
+"&"+"description="+description
+"&"+"value="+billValue
+"&"+"method="+paymentMethod);
return;
}
CORS配置:
@Configuration
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.addAllowedOrigin("*");
configuration.addAllowedHeader("*");
configuration.setAllowedMethods(Arrays.asList("GET","POST","PUT","PATCH","DELETE","OPTIONS"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
前端代码:
redirect(){
this.http.post(environment.apiUrlBase+"/redirect","").subscribe();
}
我的问题是,在我的角度应用程序中,我应该做什么配置,这样才能工作?
非常感谢!
发布于 2018-11-27 01:44:19
我也遇到了同样的问题,然后我使用了CrosFilter。在Bean下面添加并尝试
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowCredentials(true);
configuration.addAllowedOrigin("*");
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("OPTIONS");
configuration.addAllowedMethod("HEAD");
configuration.addAllowedMethod("GET");
configuration.addAllowedMethod("PUT");
configuration.addAllowedMethod("POST");
configuration.addAllowedMethod("DELETE");
configuration.addAllowedMethod("PATCH");
source.registerCorsConfiguration("/**", configuration);
return new CorsFilter(source);
}
https://stackoverflow.com/questions/53489974
复制相似问题