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

具有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>
启动不了的服务调用提示:

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

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

@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();
    }
}

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

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

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

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访问如下:

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