Can';t修复CORS错误(角度和java spring)
我正试图通过前端从后端请求数据,但出现错误:Can';t修复CORS错误(角度和java spring),java,angular,spring,cors,Java,Angular,Spring,Cors,我正试图通过前端从后端请求数据,但出现错误: Access to XMLHttpRequest at 'http://localhost:8081/api/transactions/' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 我可以通过邮递员获
Access to XMLHttpRequest at 'http://localhost:8081/api/transactions/' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我可以通过邮递员获取数据,但我的前端不能。我用的是角形和弹簧靴。My application.java:
@EnableJpaRepositories
@EntityScan
@SpringBootApplication
public class KoalaTreeAccountingApplication {
public static void main(String[] args) {
SpringApplication.run(KoalaTreeAccountingApplication.class, args);
}
}
我的安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest()
.permitAll()
.and().csrf().disable();
}
}
我的服务在angular中进行http调用:
@Injectable({
providedIn: 'root'
})
export class TransactionService {
baseUrl = 'http://localhost:8081/api/';
transactionUrl = this.baseUrl + 'transactions/';
constructor(private http: HttpClient, private logger : Logger){ }
getAllTransactions() : Observable<Transaction[]> {
this.logger.log("Request all transactions");
return this.http.get<Transaction[]>(this.transactionUrl);
}
getTransactionById(id : number) : Observable<Transaction> {
this.logger.log("Request transaction " + id);
return this.http.get<Transaction>(this.transactionUrl + id);
}
}
您需要添加一个bean来配置cors并在安全配置中启用cors
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(final CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(true).maxAge(3600);
}
};
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest()
.permitAll()
.and().cors().and().csrf().disable();
}
您需要在您希望允许的RestController方法上配置CORS。CORS是一种服务器响应
@交叉原点(原点=”http://localhost:4200")
@GetMapping(“/”)
公共列表findAllTransactions(){
返回transactionService.findAllTransactions();}
}
您看过前3个链接了吗?你可以这样问:@ArnaudClaudel这些都没用。我已经尝试过了,但是它给了我访问XMLHttpRequest的错误at'http://localhost:8081/api/transactions/“起源”http://localhost:4200'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'头。
@Imranmadbar我已将我的安全配置替换为您访问的线程中的配置发送给我,但它给出了相同的错误。请提供资源控制器的代码?我说的是在…/api/transactions url上运行GET时调用的后端方法。
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(final CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(true).maxAge(3600);
}
};
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest()
.permitAll()
.and().cors().and().csrf().disable();
}