You need to enable JavaScript to run this app. Spring Authorization Server 1.0.1集成JPA认证报错求助
优惠活动
大模型
产品
解决方案
定价
更多

Spring Authorization Server 1.0.1集成JPA认证报错求助

Spring Authorization Server 1.0.1认证错误问题

我正在基于Spring Boot 3和JPA开发Spring Authorization Server 1.0.1,认证时出现以下错误:

No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken

我的AuthorizationServerConfig配置代码如下:

@Configuration(proxyBeanMethods = false)
public class AuthorizationServerConfig {

    @Autowired
    JpaRegisteredClientRepository jpaRegisteredClientRepository;

    private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizationServerConfig.class);

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    /**
     * A Spring Security filter chain for the Protocol Endpoints.
     * aus Doku übernommen (1)
     */
    public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {

        OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
        http.getConfigurer(OAuth2AuthorizationServerConfigurer.class)
                .oidc(Customizer.withDefaults());   // Enable OpenID Connect 1.0
        http
                // Redirect to the login page when not authenticated from the
                // authorization endpoint
                .exceptionHandling((exceptions) -> exceptions
                        .authenticationEntryPoint(
                                new LoginUrlAuthenticationEntryPoint("\/login"))
                )
                // Accept access tokens for User Info and/or Client Registration
                .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);

        return http.build();

    }

    @Bean
    @Order(2)
    /**
     * A Spring Security filter chain for authentication.
     */
    public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
            throws Exception {
        http
                .authorizeHttpRequests((authorize) -> authorize
                        .anyRequest().authenticated()
                )
                // Form login handles the redirect to the login page from the
                // authorization server filter chain
                .formLogin(Customizer.withDefaults());

        return http.build();
    }

    @Bean
    /**
     * An instance of UserDetailsService for retrieving users to authenticate.
     */
    public UserDetailsService userDetailsService() {
        UserDetails userDetails = User.withDefaultPasswordEncoder()
                .username("utilo")
                .password("utilo")
                .roles("USER")
                .build();

        return new InMemoryUserDetailsManager(userDetails);
    }

    /**
     *  An instance of RegisteredClientRepository for managing clients.
     * in Doku vorhanden (4)
     */
    @Bean
    @Primary
    public RegisteredClientRepository registeredClientRepository() {

        RegisteredClient client = jpaRegisteredClientRepository.findByClientId("utilo-client");
        List<RegisteredClient> registrations = new Vector<RegisteredClient>();
        registrations.add(client);

        InMemoryRegisteredClientRepository inMemoryRegisteredClientRepository
                = new InMemoryRegisteredClientRepository(registrations);
        return inMemoryRegisteredClientRepository;

    }

    /*
     * Generate the private/public key pair for signature of JWT.
     */
    @Bean
    /**
     * An instance of com.nimbusds.jose.jwk.source.JWKSource for signing access tokens.
     */
    public JWKSource<SecurityContext> jwkSource() {
        KeyPair keyPair = generateRsaKey();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        RSAKey rsaKey = new RSAKey.Builder(publicKey)
                .privateKey(privateKey)
                .keyID(UUID.randomUUID().toString())
                .build();
        JWKSet jwkSet = new JWKSet(rsaKey);
        return new ImmutableJWKSet<>(jwkSet);
    }

    /**
     * An instance of java.security.KeyPair with keys generated on startup used to create the JWKSource above.
     */
    private static KeyPair generateRsaKey() {
        KeyPair keyPair;
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(2048);
            keyPair = keyPairGenerator.generateKeyPair();
        }
        catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
        return keyPair;
    }

    @Bean
    /**
     *  An instance of JwtDecoder for decoding signed access tokens.
     */
    public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
        return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
    }

    @Bean
    /**
     * An instance of AuthorizationServerSettings to configure Spring Authorization Server.
     */
    public AuthorizationServerSettings authorizationServerSettings() {
        return AuthorizationServerSettings.builder().build();
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

}

若需要更多用于排查问题的类文件,请告知。


内容的提问来源于stack exchange,提问作者Christian Osterrieder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 22:16:42