spring-security-oauth2-client启动报错,提示java版本太低

项目正常启动,突然出现了问题,提示:

org/springframework/security/oauth2/client/registration/ClientRegistrationRepository has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0

说是java编译版本比当前运行的版本高。

奇怪,明明昨天还好好的。

试着找一下这个原因,发现 ClientRegistrationRepository这个类属于spring-security-oauth2-client包,而这个包,版本项目中使用时<version>是自动获取最新的版本:

 然后拉下来的包是6.0.0-M1版本的。

去mvnrepository,发现居然没有这个版本,最高只到5.6.1:

 突然想起,要不去官网看看,才发现:

它确实更新了6.0.0-M1这个版本,只不过 6.0.0-M1以后要用JDK17+版本才能使用,可支持JDK8的版本是5.7.0-M1。

好巧不巧,它17号发布的,我18号中的枪。怪不得之前都好好的。

估计spring其他模块或多或少也会这样,在此记录一下。

项目上把security的版本改为 5.7.0-M1,重新拉包,重启项目,没问题。

完美!!

 

### 配置和使用 `/oauth2/token` 端点 在 Spring Security OAuth2 授权服务器中,正确配置和使用 `/oauth2/token` 端点涉及多个方面。以下是详细的说明: #### 1. 添加依赖项 为了使应用程序能够处理 OAuth2 认证请求并提供令牌服务,需确保项目中的 `pom.xml` 或构建文件已包含必要的依赖项。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-authorization-server</artifactId> </dependency> ``` #### 2. 启用授权服务器功能 通过创建一个配置类来启用授权服务器的功能,并指定支持的客户端认证方式以及授予类型。 ```java @Configuration(proxyBeanMethods = false) public class AuthorizationServerConfig { @Bean public RegisteredClientRepository registeredClientRepository() { RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString()) .clientId("client-id") .clientSecret("{noop}secret") .scope(OidcScopes.OPENID) .redirectUri("http://localhost:8080/login/oauth2/code/messaging-client-oidc") .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC) .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) .build(); return new InMemoryRegisteredClientRepository(registeredClient); } @Bean public OAuth2AuthorizationService authorizationService(JdbcTemplate jdbcTemplate, RegisteredClientRepository registeredClientRepository) { return new JdbcOAuth2AuthorizationService(jdbcTemplate, registeredClientRepository); } } ``` 此部分代码定义了一个内存中的注册客户端仓库,并设置了相应的授权服务实例[^1]。 #### 3. 定义安全配置 设置 HTTP 请求的安全策略,允许访问 `/oauth2/token` 路径下的资源仅限于经过身份验证后的客户端应用。 ```java @EnableWebSecurity(debug = true) public class SecurityConfiguration extends WebSecurityConfigurerAdapter { private final RegisteredClientRepository registeredClientRepository; // 构造函数注入 protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/actuator/**").permitAll() .anyRequest().authenticated(); http.oauth2ResourceServer(oauth2 -> oauth2.jwt()); http.oauth2Login(Customizer.withDefaults()); http.formLogin(form -> form.loginPage("/login") .permitAll() ); http.apply(new OAuth2AuthorizationServerConfigurer<>()) .and() .authorizeRequests(authorizations -> authorizations.mvcMatchers("/oauth2/token").permitAll()); http.csrf(csrf -> csrf.disable()); } } ``` 上述配置启用了基于 JWT 的资源服务器保护机制,并开放了对 `/oauth2/token` API 的无条件访问权限以便测试目的;实际部署时应根据需求调整安全性措施[^2]。 #### 4. 测试接口调用 完成以上步骤之后,可以利用 Postman 或其他工具向该 URL 发送 POST 请求来进行 token 获取操作。注意要携带正确的参数如 client_id 和 client_secret 进行基本认证。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值