【问题标题】:How can I allow public access to parts of Spring Security app using permitAll?如何使用 permitAll 允许公开访问 Spring Security 应用程序的部分内容?
【发布时间】:2019-10-22 12:03:43
【问题描述】:

我正在尝试让 Spring 应用程序允许一些公共请求(无需登录)和一些私有请求(需要登录)。

此时我想让公共部分正常工作。

我已经尝试了 Spring Security 文档中列出的大多数示例配置,包括各种匿名() 和 permitAll() 组合。所有最终都重定向到登录页面。

  protected void configure(HttpSecurity http) throws Exception{
        http.authorizeRequests()
        .antMatchers("/resources/**", "/signup", "/about","/api/home").permitAll()                
            .antMatchers("/admin/**").hasRole("ADMIN")                                    
            .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")      
            .anyRequest().authenticated()                                             
            .and()
        // ...
        .formLogin();
    }

预期结果: permitAll() 下的项目无需登录即可访问

实际结果:

重定向到登录页面。这显示在日志中:2019-06-06 17:29:43.593 信息 56330 --- [主要] o.s.s.web.DefaultSecurityFilterChain :创建过滤器链:任何 请求,[org.srin...

这让我相信它甚至没有读取此配置。有没有办法解决这个问题?

谢谢!

更新:我尝试添加网络安全忽略,但它似乎仍然无法正常工作。它似乎仍然打印“defaultsecuritychain”错误,所以我觉得这可能与它有关。

更新 2:在 src/main/resources 下添加了 application.properties 文件,其中包含此行 logging.level.org.springframework.security=DEBUG 以使其记录调试消息。

pastebin.com/2u9k7eHD

【问题讨论】:

    标签: spring spring-boot spring-security


    【解决方案1】:

    看看http://blog.florian-hopf.de/2017/08/spring-security.html,它可能会更详细地解释您的用例。

    我的建议是尝试将 WebSecurity 用于静态和公共资源

    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**", "/signup", "/about","/api/home");
    }
    

    【讨论】:

      【解决方案2】:

      您可以通过以下配置实现您的要求。这是使用不需要将身份验证/授权放置在WebSecurity using ignoring instead of HttpSecurity as WebScurity will bypass the Spring Security Filter Chain and reduce the execution time 中的 URL 的好方法

      @Override
      public void configure(WebSecurity web) throws Exception {
          web
              .ignoring()
              .antMatchers("/resources/**", "/signup", "/about","/api/home");
      }
      
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
              .authorizeRequests()
              .antMatchers("/admin/**").hasRole("ADMIN")                                    
              .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") 
              .anyRequest().authenticated()
              .yourConfigurations
      }
      

      当你使用HttpSecurity 并尝试permitAll() 请求时。您的请求将被允许从 Spring Security Filter Chain 访问。这是昂贵的,因为会有其他请求也进入此过滤器链,需要根据身份验证/授权来允许或禁止

      但是当你使用WebSecurity 时,任何对"/resources/**", "/signup", "/about","/api/home" 的请求都将完全绕过Spring Security Filter Chain。这是安全的,因为您不需要任何身份验证/授权即可查看图像或读取 javascript 文件。

      【讨论】:

      • 我已尝试实施此解决方案,但它似乎不起作用,应用程序仍重定向到登录。谢谢
      • 你好@larryqiann 你能把你的项目上传到git上并分享链接吗
      【解决方案3】:

      原来我在我的一个源文件中一直缺少@SpringBootApplication 注释。确保它在那里,也许它会起作用。

      感谢所有回复的人!

      【讨论】:

        猜你喜欢
        • 2014-09-02
        • 2019-08-19
        • 1970-01-01
        • 1970-01-01
        • 2015-10-09
        • 1970-01-01
        • 2020-05-10
        • 1970-01-01
        • 2014-03-14
        相关资源
        最近更新 更多