【问题标题】:Unable to add custom config for spring security with oauth2无法使用 oauth2 为 Spring Security 添加自定义配置
【发布时间】:2017-12-19 23:39:44
【问题描述】:

我有一个可以与 oauth2(作为资源服务器)一起正常工作的 Spring Boot 应用程序。没有自定义 configure(HttpSecurity http) 方法。只有

spring-boot-starter-security
spring-security-oauth2
spring-security-jwt

被添加到 pom 中。

现在我想添加应该不受保护的端点。所以(在许多 SO 响应之后)我开始添加:

@Configuration
public class Security extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
    }
}

然后我得到了:

无法将 org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer@6513fd22 应用于已构建的对象

完全错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer@6513fd22 to already built object

那么我应该如何配置我的安全性以添加匿名访问端点?

【问题讨论】:

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


    【解决方案1】:

    错误来自configure() 方法中的空体。

    您必须明确指定它。例如(来自我们的工作应用程序):

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/unprotected/sample/find/**").permitAll()
                .antMatchers("/unprotected/another/register").permitAll()
                .anyRequest().authenticated().and()
                .csrf().disable();
    }
    

    与/unprotected/sample/find/** 和/unprotected/sample/find/** 匹配的端点不受保护,其他一切都受到保护。

    当然,不受保护的端点不应定义任何@PreAuthorize()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 2016-08-12
      • 2011-12-05
      • 2021-01-24
      • 2015-03-24
      • 2015-11-18
      • 1970-01-01
      相关资源
      最近更新 更多