Swagger not working with Spring-Boot after 2.7.0 upgrade

3.4k views Asked by At

I upgraded Spring-Boot from version 2.5 to 2.7. After trying multiple solutions, to make it run, I got to the point to be able to run and access the endpoints directly, like https://localhost:8493/test/myendpoint, it returns the json needed, but I cannot access https://localhost:8493/test/swagger-ui. It displays:

You don't have authorisation to view this page. HTTP ERROR 403

What else do I need to add/remove?


@Configuration 
@Order(Ordered.HIGHEST_PRECEDENCE)
@Data
public class SecurityConfig {


    @Value("${portal.required.role.name}")
    private String portalRoleName;

    @Value("${server.servlet.context-path}")
    private String servletContextPath;

    @Bean
    protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

        http
            .authorizeRequests()
                .antMatchers("/myendpoint/**").permitAll().and()
            .authorizeRequests().antMatchers("/myendpoint2/**").permitAll().and()
            .authorizeRequests().antMatchers(servletContextPath).hasAuthority(portalRoleName)
                .anyRequest().fullyAuthenticated();

        return http.build();
    }
}

@Configuration
@EnableOpenApi
@Import(SpringDataRestConfiguration.class)
public class SpringFoxConfig {

    @Bean
    public WebMvcRequestHandlerProvider webMvcRequestHandlerProvider(Optional<ServletContext> servletContext, HandlerMethodResolver methodResolver, List<RequestMappingInfoHandlerMapping> handlerMappings) {
        handlerMappings = handlerMappings.stream().filter(rh -> rh.getClass().getName().contains("RequestMapping")).toList();
        return new WebMvcRequestHandlerProvider(servletContext, methodResolver, handlerMappings);
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("mypackage"))
                .paths(PathSelectors.any())
                .build();
    }

}
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.6</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-envers -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>5.4.4.Final</version>
        </dependency>

        <!--Swagger START-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-bean-validators</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!--Swagger END-->

        <!-- JSONObject -->
        <dependency>
            <groupId>org.codehaus.jettison</groupId>
            <artifactId>jettison</artifactId>
            <version>1.5.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

    </dependencies>

and I have this in my properties file:

spring.mvc.pathmatch.matching-strategy= ANT_PATH_MATCHER
0

There are 0 answers