在Java中解決OAuth跨域問題通常需要在服務(wù)端配置跨域資源共享(CORS)策略。以下是一些解決方案:
@CrossOrigin(origins = "http://localhost:8080")
@GetMapping("/api/data")
public ResponseEntity<String> getData() {
// 返回數(shù)據(jù)
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable();
}
在application.properties中添加以下配置:
spring.mvc.cors.allowed-origins=http://localhost:8080
spring.mvc.cors.allowed-methods=GET,POST
在application.yml中添加以下配置:
spring:
mvc:
cors:
allowed-origins: http://localhost:8080
allowed-methods: GET,POST
通過以上方法配置跨域資源共享策略,可以解決Java OAuth跨域訪問的問題。