【问题标题】:Spring Boot web service designed for a specific domain为特定域设计的 Spring Boot Web 服务
【发布时间】:2015-04-29 03:16:34
【问题描述】:

我想限制我的 Spring Boot 安全性,只允许特定域(或主机名)进行 Web 服务调用。

这个想法是只允许一个应用程序(基于 Spring MVC)在 Spring Boot 应用程序中访问这些 Web 服务。

更新

我尝试合并hasIpAddress,如下所示,但没有效果

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll().and().authorizeRequests().anyRequest().access("hasIpAddress('localhost')");
    }

spring security 有这样的设置吗?或者我需要使用拦截器?

【问题讨论】:

  • 删除 permitAll 部分并将主机名更改为 IP 地址或子网,然后它应该可以工作,例如:http.authorizeRequests().anyRequest().access("hasIpAddress('192.168. 0.0/24')");

标签: spring-mvc spring-security spring-boot


【解决方案1】:

尝试:

http.authorizeRequests()
       .anyRequest().hasIpAddress('localhost');

希望对你有帮助

【讨论】:

    【解决方案2】:

    在 Spring Web Security 中有 hasIpAddress 表达式,您可以在其中传入 ip 或范围。

    示例配置:

    <http use-expressions="true">
            <intercept-url pattern="/myUrlPattern/*" access="hasIpAddress('100.10.11.12')" />
    
        </http>
    

    更多解释here.

    【讨论】:

    • 我可以为此使用注释吗?
    • 虽然我没有专门使用注解,但人们在此处的注解中报告了一些问题:stackoverflow.com/questions/16351506/…
    • 我将使用它作为最后的手段。尽量不使用xml。如果没有更好的解决方案,我会将其标记为已接受。谢谢
    【解决方案3】:

    也许您可以利用响应标头来控制可以访问您的服务的有效 IP。我认为应该使用 Spring Security 来处理用户身份验证和授权。

    response.setHeader("Access-Control-Allow-Origin", "*");

    https://spring.io/guides/gs/rest-service-cors/

    【讨论】:

      猜你喜欢
      • 2018-10-25
      • 2019-03-17
      • 2021-04-26
      • 1970-01-01
      • 2021-08-31
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多