【问题标题】:Spring Security disable security for requests made FROM a certain urlSpring Security 禁用从某个 url 发出的请求的安全性
【发布时间】:2018-03-15 19:56:06
【问题描述】:

我的应用程序使用弹簧安全性。我知道您可以使用 antMatchers 来允许对映射到您的应用程序的某些 URI 的请求。但我也需要允许从外部 api 向我的应用程序中的 URI 发出的请求,并且只针对该 api。我需要这样的东西:

     @Override
     protected void configure(WebSecurity web) throws Exception 
     {            
        web.ignoring()
          .antMatchers("http://externalTestApi.com");
     }

有人知道这是否可能吗?我没有在这方面找到任何东西。

【问题讨论】:

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


    【解决方案1】:

    你可以这样做(如果你想组合多个条件)

    .authorizeRequests()
    .antMatchers("/user/list")
    .access("authenticated or hasIpAddress('666.666.666.666') or hasIpAddress('127.0.0.1') or hasIpAddress('0:0:0:0:0:0:0:1')")
    

    .authorizeRequests()
    .antMatchers("/user/list")
    .hasIpAddress("0.0.0.0/0");
    

    【讨论】:

    • 适用于接受 HttpSecurity 作为参数的配置方法,我需要使用 WebSecurity 的配置方法。
    • 你不是在扩展WebSecurityConfigurerAdapter吗?只需@override configure(HttpSecurity http) 并在那里做你的安全吗?
    • 在 WebSecurityConfigurerAdapter 中有 2 种配置方法,一种采用 HttpSecurity,另一种采用 WebSecurity,对于带有 WebSecurity 的方法,我需要这个,因为这是过滤请求并且我得到 403 null。我正在使用 RestTemplate 从 api 发布帖子,因为它是发布的,它进行了一些过滤并且我的会话有问题。
    • 如果您将其移至具有 HttpSecurity 的方法,则安全性将在该层完成。我看不出有任何理由阻止您将代码移至 HttpSecurity.. 请参阅此处的差异@ 987654321@
    • 这两种配置方法有不同的用途,正如您发布的链接中所说,使用WebSecurity您可以拒绝请求,安全配置是这样的,即请求被某些过滤器拒绝,如果我按照您的建议进行操作,我的请求将在到达我在 HttpSecurity 上所做的任何配置之前被拒绝。
    猜你喜欢
    • 2018-11-22
    • 2014-07-16
    • 2015-09-23
    • 1970-01-01
    • 2019-11-11
    • 2018-09-13
    • 2016-01-15
    • 1970-01-01
    相关资源
    最近更新 更多