I have seen this before and Spring Boot has support to ensure you can control which resources can be accessed outside of the current origin. It's as simple as an annotation "@CrossOrigin", as shown below. In this example every request from this Rest Controller supports resource calls residing outside the current origin.
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping(value = "/beacon")
public class BeaconRest
{
private static Log logger = LogFactory.getLog(BeaconRest.class);
@Autowired
private BeaconRepository beaconRepository;
@RequestMapping(value = "/all",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public List<Beacon> allBeacons()
{
logger.info("Invoking /beacon/all RESTful method");
return beaconRepository.findAll();
}
Of course it's much more flexible then that adding the ability to add options, and you can read more about it here.
https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/cors.html
No comments:
Post a Comment