在Ubuntu系統上使用Postman進行跨域請求測試,可以通過以下步驟實現:
Origin
,值為你要跨域的目標地址(例如:http://example.com:8080
)。Access-Control-Allow-Origin
字段。如果你有權限修改服務器配置,可以在后端設置CORS(Cross-Origin Resource Sharing)來允許跨域請求。例如,在Spring Boot應用中,可以通過添加@CrossOrigin
注解在控制器上來配置跨域訪問:
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin(origins = "http://example.com:8081")
public class MyController {
// ...
}
或者在配置類中添加CORS配置:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://example.com:8081");
}
}
Access-Control-Allow-Origin: *
)可能會帶來安全風險,應謹慎使用。通過上述方法,你可以在Ubuntu系統上使用Postman方便地進行跨域請求測試。