Spring-security 阻止匿名用户从 MongoDB 中提取数据

Posted

技术标签:

【中文标题】Spring-security 阻止匿名用户从 MongoDB 中提取数据【英文标题】:Spring-security blocking anonymous users from pulling data from MongoDB 【发布时间】:2018-04-04 18:41:36 【问题描述】:

我有一个使用 Spring Security 的 Spring Boot Web 应用程序。 index.html 页面包含一个对控制器的方法调用 (POST),该控制器将对象从 MongoDB 加载到一个 ArrayList 中并返回它,以便它可以显示在首页上。

Spring Security 似乎正在阻止匿名用户的 POST 请求。如果我第一次登录所以调用“/loadContent”方法,然后注销,一切正常。我确实在调用方法之前传递了 CSRF 令牌。

我的“WebSecurityConfig”:

protected void configure(HttpSecurity http) throws Exception 
    http
            .authorizeRequests()
            .antMatchers("/", "/loadContent")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .permitAll()
            .and()
            .exceptionHandling()
            .accessDeniedPage("/403");

【问题讨论】:

【参考方案1】:

在 Spring Security 中默认启用 CSRF。

一种可能的解决方案是手动禁用它(参见下面代码中的最后一行)。

protected void configure(HttpSecurity http) throws Exception 
    http
            .authorizeRequests()
            .antMatchers("/", "/loadContent")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .permitAll()
            .and()
            .exceptionHandling()
            .accessDeniedPage("/403")
            .and().csrf().disable();


更新:

如果你想使用我鼓励的 csrf,也许可以考虑保护一个额外的 REST 端点,例如从 /api/ 开始。

在下面的示例中,这些端点使用名为api 的用户使用基本授权进行保护,但您可以轻松更改它以允许匿名用户请求资源:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
     @Autowired
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception 
        auth
            .inMemoryAuthentication()
                .withUser("api").password("api").roles("API").and()
                .withUser("user").password("user").roles("USER").and()
                .withUser("admin").password("admin").roles("USER", "API", "ADMIN");
     

     @Configuration
     @Order(1) // higher order = lower priority
     public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter

         @Override
         protected void configure(HttpSecurity http) throws Exception 
            // no csrf when communicating directly with the backend api
             http
                 .antMatcher("/api/**")
                     .authorizeRequests()
                         .anyRequest().hasAnyRole("API")
                         .and()
                 .httpBasic()
                 .and()
                 .csrf().disable();
             http.sessionManagement().disable();

         
     

     @Configuration
     @Order(2) // higher order = lower priority
     public static class UIWebSecurityConfig extends WebSecurityConfigurerAdapter 
       @Override
        protected void configure(HttpSecurity http) throws Exception 
           http.authorizeRequests() 
                .antMatchers("/**").hasAnyRole("USER", "ADMIN").anyRequest().authenticated();

           http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
           http.httpBasic().disable();
        
     

【讨论】:

【参考方案2】:

变成了控制器@RequestMapping 区域的错字。非常感谢您的帮助。

【讨论】:

以上是关于Spring-security 阻止匿名用户从 MongoDB 中提取数据的主要内容,如果未能解决你的问题,请参考以下文章

重写 spring-security 重定向 URL

尝试匿名登录 Firebase 时,引荐来源请求被阻止

从Spring-Security过滤器中抛出用户定义的异常

Spring-security-3 浏览器后退按钮问题

Spring-Security认证和授权

Spring-Security:升级到 Spring-Security 4.1 后,用户名发送为空以进行登录