Spring boot 如何在没有身份验证的情况下允许spring security中的某些端点?

Spring boot 如何在没有身份验证的情况下允许spring security中的某些端点?,spring-boot,spring-security,Spring Boot,Spring Security,我有一个Spring Boot Rest API web应用程序,我在其中使用Spring security使大多数端点需要身份验证。 这是代码的一部分: public class SecurityConfig extends WebSecurityConfigurerAdapter { .... @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().dis

我有一个Spring Boot Rest API web应用程序,我在其中使用Spring security使大多数端点需要身份验证。 这是代码的一部分:

public class SecurityConfig extends WebSecurityConfigurerAdapter {
....
@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .requestMatchers()
                .antMatchers("/oauth/token")
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                .anyRequest().authenticated();
    }
有人能解释一下以http.csrf开头的每一行是什么意思吗


如何修改上述代码,使enpoint/bar/pk可以在不需要身份验证的情况下访问,就像没有Spring Security一样?

默认情况下,Spring Boot会激活防止(跨站点请求伪造攻击)的保护。该攻击包括恶意站点利用已通过站点(如银行)身份验证的用户欺骗用户在该站点上执行操作(如资金转账)

针对攻击的保护包括Spring引导应用程序在每个响应中发送令牌,并期望客户端在后续请求中发送令牌。如果没有收到令牌,Spring Boot将返回一个错误

有时,您希望禁用此行为(风险由您自己承担),因此使用
csrf.disable
。如果您开发了无状态API,并且无法将POST请求链接到任何以前的请求或会话,您可能会发现禁用csrf保护很方便。但是,你需要仔细考虑这个问题和原因。 请注意,CSRF保护对GET请求没有影响。它只影响状态更改请求(如POST、DELETE)

为了允许您的endoint给任何人,而不需要任何身份验证,您需要使用

http.authorizeRequests().antMatchers(“/**”).permitAll()

编辑

要特别允许未经授权的请求
/bar/pk
,并保持其他元素不变,请按如下方式修改代码:

http.csrf().disable()
                .requestMatchers()
                .antMatchers("/oauth/token")
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                .antMatchers("/bars/pk").permitAll()
                .anyRequest().authenticated();

默认情况下,Spring Boot会激活针对(跨站点请求伪造攻击)的保护。该攻击包括恶意站点利用已通过站点(如银行)身份验证的用户欺骗用户在该站点上执行操作(如资金转账)

针对攻击的保护包括Spring引导应用程序在每个响应中发送令牌,并期望客户端在后续请求中发送令牌。如果没有收到令牌,Spring Boot将返回一个错误

有时,您希望禁用此行为(风险由您自己承担),因此使用
csrf.disable
。如果您开发了无状态API,并且无法将POST请求链接到任何以前的请求或会话,您可能会发现禁用csrf保护很方便。但是,你需要仔细考虑这个问题和原因。 请注意,CSRF保护对GET请求没有影响。它只影响状态更改请求(如POST、DELETE)

为了允许您的endoint给任何人,而不需要任何身份验证,您需要使用

http.authorizeRequests().antMatchers(“/**”).permitAll()

编辑

要特别允许未经授权的请求
/bar/pk
,并保持其他元素不变,请按如下方式修改代码:

http.csrf().disable()
                .requestMatchers()
                .antMatchers("/oauth/token")
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                .antMatchers("/bars/pk").permitAll()
                .anyRequest().authenticated();

下面是一个完整的示例:

 httpSecurity.authorizeRequests()
                    .antMatchers(HttpMethod.GET)                  
                    .permitAll() // Allow all GET requests to go unauthenticated
                    .antMatchers(allowedResources)                  
                    .permitAll() // Allow all requests to go unauthenticated for the specified paths
                    .antMatchers(protectedResources).hasRole(USER)
                    .antMatchers(adminResources).hasRole(ADMIN)
                    .anyRequest().authenticated(); // Authenticate all other request paths

下面是一个完整的示例:

 httpSecurity.authorizeRequests()
                    .antMatchers(HttpMethod.GET)                  
                    .permitAll() // Allow all GET requests to go unauthenticated
                    .antMatchers(allowedResources)                  
                    .permitAll() // Allow all requests to go unauthenticated for the specified paths
                    .antMatchers(protectedResources).hasRole(USER)
                    .antMatchers(adminResources).hasRole(ADMIN)
                    .anyRequest().authenticated(); // Authenticate all other request paths

您可以使用permitAll
.antMatchers(“/bar/pk”).permitAll()
csrf.disable()是在Spring Security中禁用csrf保护的一个示例,以允许您可以执行以下操作的所有GET请求:`httpSecurity.authorizeRequests().antMatchers(HttpMethod.GET.permitAll())//允许所有GET请求在未经身份验证的情况下运行`Tuhin在哪里添加行,在末尾还是开头?您可以使用permitAll
.antMatchers(“/bar/pk”)。permitAll()
csrf.disable()是在Spring Security中禁用csrf保护的一个示例,以允许所有GET请求执行类似于:`httpSecurity.authorizeRequests().antMatchers(HttpMethod.GET).permitAll()//允许所有GET请求在未经身份验证的情况下运行`Tuhin在哪里添加行,在末尾还是在开头?