我已经有了一个应用程序,它通过Spring MVC提供一些web内容,还有一些在同一个URI下的JSON。如何使用Spring Security来保护REST API(而不是视图)?
@Controller
public class SomeController {
@RequestMapping(value = {"/someUri"}, method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
public String getView() {
return "index.html";
}
@RequestMapping(path = "/someUri", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String getJson() {
return "{ \"some\": \"json\" }";
}
,现在我想,以确保只有REST API产生的MediaType.APPLICATION_JSON_VALUE
与春季安全。
当我在每种方法上添加@PreAuthorize
或@Secured
注释时,它都能正常工作。但我想全局配置它,例如在WebSecurityConfiguration#configure(HttpSecurity http)
方法中。是否有可能以全球任何方式确保产生特定媒体类型的端点?