简体   繁体   English

Spring MVC:单个控制器的域限制

[英]Spring MVC: Domain restriction for a single controller

I know that we can use Spring security to restrict access to a controller by IP address or even I can create my custom annotation to add some custom logic for this purpose. 我知道我们可以使用Spring安全性来限制IP地址对控制器的访问,甚至可以创建自定义注释来为此添加一些自定义逻辑。 I need to restrict access to a controller in my application to only specific domain name ( a third party ), I checked with this third party provider for the IP ranges which I can use to configure at my end, however they want to have more freedom around IP address and would like me to create access based on the domain name. 我需要将对应用程序中控制器的访问权限限制为仅特定域名(第三方),我向该第三方提供商检查了我可以用来配置的IP范围,但是他们希望拥有更大的自由度IP地址周围,希望我根据域名创建访问权限。

I checked through the doc but unable to find any such use cases, Can any one help me to understand if this is possible or I need to go back to IP based access mechanism ? 我检查了文档,但找不到任何这样的用例,有人可以帮助我了解这是否可行,或者我需要返回基于IP的访问机制吗?

i think you can use Spring's CORS support for this. 我认为您可以为此使用Spring的CORS支持。

ie if the domain that you expect the request is example.com you can have in your controller method the following annotation 即,如果您希望请求的域是example.com,则可以在控制器方法中包含以下注释

@CrossOrigin(origins = "http://example.com")

This @CrossOrigin annotation enables cross-origin requests only for this specific method. 此@CrossOrigin批注仅针对此特定方法启用跨域请求。 By default, its allows all origins, all headers, the HTTP methods specified in the @RequestMapping annotation and a maxAge of 30 minutes is used. 默认情况下,它允许使用所有来源,所有标头,@ RequestMapping批注中指定的HTTP方法以及30分钟的maxAge。 You can customize this behavior by specifying the value of one of the annotation attributes: origins, methods, allowedHeaders, exposedHeaders, allowCredentials or maxAge. 您可以通过指定以下注释属性之一的值来自定义此行为:起点,方法,allowedHeaders,暴露的Headers,allowCredentials或maxAge。 In this example, we only allow http://localhost:8080 to send cross-origin requests. 在此示例中,我们仅允许http:// localhost:8080发送跨域请求。

Have a look in the following url https://spring.io/guides/gs/rest-service-cors/ 看看以下网址https://spring.io/guides/gs/rest-service-cors/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM