Spring Boot REST API无法识别OAuth2库问题求助
Hey there, I've run into this exact issue before when working with Spring OAuth2—let's break down what's wrong and how to fix it:
The Root Cause
The AuthorizationServerConfigurerAdapter and old org.springframework.security.oauth2.client classes you're trying to use belong to the legacy Spring Security OAuth2 project, which has been officially deprecated and is no longer maintained. Starting with Spring Boot 2.4+, the framework moved OAuth2 functionality into new, supported modules, which is why your IDE can't resolve those symbols.
Solution 1: Use the New, Supported OAuth2 Stack (Recommended)
If you're starting a new project or can migrate, use Spring's official current OAuth2 modules. Here's what to add to your pom.xml:
For an Authorization Server (Protecting your REST API)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-authorization-server</artifactId> </dependency>
Note: This requires Spring Boot 2.6 or newer. The old AuthorizationServerConfigurerAdapter is replaced with a more modular configuration approach. Here's a basic example:
@Configuration public class AuthorizationServerConfig { @Bean public RegisteredClientRepository registeredClientRepository() { RegisteredClient client = RegisteredClient.withId(UUID.randomUUID().toString()) .clientId("your-client-id") .clientSecret("{noop}your-client-secret") // Use a password encoder in production! .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS) .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN) .scope("api.read") .scope("api.write") .build(); return new InMemoryRegisteredClientRepository(client); } @Bean public AuthorizationServerSettings authorizationServerSettings() { return AuthorizationServerSettings.builder().build(); } }
For an OAuth2 Client (If your app needs to authenticate against an external OAuth2 provider)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency>
The old OAuth2ClientContext is replaced with classes like OAuth2AuthorizedClientService or the @RegisteredOAuth2AuthorizedClient annotation for accessing authorized client details.
Solution 2: Fall Back to the Legacy OAuth2 Module (Not Recommended)
If you must use the old classes for compatibility with existing code, add these dependencies to your pom.xml (ensure versions match your Spring Boot version—this example works with Spring Boot 2.3.x):
<dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.5.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-jwt</artifactId> <version>1.1.1.RELEASE</version> </dependency>
Keep in mind this module is no longer updated, so you'll miss out on security patches and new features.
Quick Checks
- Double-check your Spring Boot version in
pom.xmlto ensure dependency compatibility. - If you're using Maven, run
mvn clean installto refresh dependencies after making changes. - In your IDE, try invalidating caches and restarting (File → Invalidate Caches... in IntelliJ, for example) to fix any lingering symbol resolution issues.
内容的提问来源于stack exchange,提问作者Ilya Kremniou

