Spring-Cloud-Oauth2调用服务启动不了

本文解决了一个SpringCloud微服务环境下,升级SpringBoot和SpringCloud版本后,服务调用无法启动的问题。通过对比不同版本的依赖,发现缺少spring-boot-starter-web导致找不到javax.servlet.Filter。最终通过添加依赖解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

具有OAuth2的Spring Cloud分布微服务架构。

原来做过:Spring Boot--2.0.7,SpringCloud--Finchley.SR2。

现在进行:Spring Boot--2.2.4,SpringCloud--Hoxton.SR1。

遇到问题:调用服务启动不了,服务架构、相关依赖及其代码跟原来一样。

使用现在的注册、授权、网关,用原来的调用服务,可以正常启动。

调用服务引用的依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
启动不了的服务调用提示:

问题应该在服务的资源配置:

@Configuration
@EnableResourceServer
public class RscSvrCfg extends            // 资源服务配置
    ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf()
            .disable()
            .authorizeRequests()
            .anyRequest()
            .authenticated();
    }
}
修改常见形式:

public class RscSvrCfg  extends ResourceServerConfigurerAdapter{

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .exceptionHandling()
                .authenticationEntryPoint((request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED))
            .and()
                .authorizeRequests()
                .anyRequest().authenticated()
            .and()
                .httpBasic();
    }
}

原来版本无语法错误提示,现在版本则存在,说明问题可能是由版本差异引起。

再看控制台的错误原因提示:

SpringBoot项目找不到javax.servlet.Filter

javax.servlet.Filter这个类位于tomcat-embed这个jar下面。这是由于没有添加spring-boot-starter-web依赖造成的。

springboot项目默认会添加spring-boot-starter和spring-boot-starter-test两个依赖,而web项目需要spring-boot-starter-web依赖。

说明原有版本含有spring-boot-starter-web依赖,现有版本不含。打开相应JAR包观察,果然如此。

在pom.XML文件中添加spring-boot-starter-web依赖,具有OAuth2的服务调用正常启动。POSTMAN访问如下:

 

 

 

 

 

 

 

### 接入 OAuth2 的概述 Ruoyi-Cloud 是基于 Spring Cloud 和 Vue 开发的一套微服务架构模板,支持分布式事务、网关路由等功能。通过集成 OAuth2 实现统一的身份认证和授权管理是一个常见的需求[^3]。 以下是关于如何将 Ruoyi-Cloud 项目接入 OAuth2 进行身份验证的具体说明: --- ### 配置认证服务器 (Authorization Server) #### 添加依赖 在 `pom.xml` 文件中引入 Spring Security OAuth2 相关依赖: ```xml <dependency> <groupId>org.springframework.security.oauth.boot</groupId> <artifactId>spring-security-oauth2-autoconfigure</artifactId> </dependency> ``` 此依赖用于快速启动 OAuth2 认证功能[^1]。 #### 编写配置类 创建一个 Java 配置类来定义认证服务器的行为: ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig { } ``` 上述代码启用了 OAuth2 的认证服务器功能,并允许其处理授权码模式下的请求[^2]。 --- ### 资源服务器 (Resource Server) 配置 资源服务器负责保护 API 并验证访问令牌的有效性。同样需要添加相关依赖并编写配置文件。 #### 修改 `application.yml` 在资源配置文件中指定 JWT 密钥和其他参数: ```yaml security: oauth2: resource: jwt: key-value: | -----BEGIN RSA PRIVATE KEY----- MIICXQIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPq... -----END RSA PRIVATE KEY----- ``` 该密钥用于签名和校验 JSON Web Token (JWT)。 #### 创建资源服务器配置类 ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; @Configuration @EnableResourceServer public class ResourceServerConfig { } ``` 启用资源服务器后,受保护的接口会自动拦截未携带有效 token 的请求。 --- ### 客户端注册与测试 为了使第三方应用能够向认证服务器发起请求,需预先注册客户端信息到数据库表 `oauth_client_details` 中。例如: | client_id | client_secret | scope | authorized_grant_types | web_server_redirect_uri | |-----------|---------------|-------------|--------------------------------|-----------------------------| | client_1 | secret | read,write | authorization_code,password... | https://www.baidu.com/ | 当用户尝试登录时,浏览器会被重定向至 `/oauth/authorize` 地址以完成授权过程。 --- ### 测试流程 假设已成功部署认证服务器,则可通过以下 URL 获取授权码: ``` http://localhost:8080/oauth/authorize?response_type=code&client_id=client_1&redirect_uri=https://www.baidu.com/ ``` 随后利用返回的 code 请求 access_token: ``` POST /oauth/token HTTP/1.1 Host: localhost:8080 Content-Type: application/x-www-form-urlencoded grant_type=authorization_code& code={AUTHORIZATION_CODE}& redirect_uri=https://www.baidu.com/& client_id=client_1& client_secret=secret ``` 最终获得的 token 就可用于调用受保护的服务接口。 --- ### 前端对接注意事项 对于前端部分(如 RuoYi-Vue),建议采用 Axios 或其他 HTTP 工具封装 token 自动刷新逻辑。每次发送请求前附加 Bearer 类型的 header 字段即可[^4]: ```javascript axios.interceptors.request.use(config => { const token = localStorage.getItem('access_token'); if (token) { config.headers.Authorization = 'Bearer ' + token; } return config; }); ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值