簡體   English   中英

無法使用oauth2添加用於春季安全性的自定義配置

[英]Unable to add custom config for spring security with oauth2

我有一個可以與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

那么我應該如何配置我的安全性以添加用於匿名訪問的端點?

錯誤來自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/**端點/unprotected/sample/find/**保護,其他所有內容均受保護。

當然,不受保護的端點不應定義任何@PreAuthorize()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM