javaspring-bootmavenswaggerspringfox

No mapping for GET /v3/api-docs | not able to use swagger in spring boot app


I have created a spring-boot application, for blogging application. while I try to use swagger in spring-boot app to create a document for all the APIs I have created.

The GET http://localhost:8080/v3/api-docs is giving me a unauthorized error. even though I am using the correct token.

unauthorized error

springfox dependencies in maven dependencies

here is the classes I have created

package com.codewithdurgesh.blog.configs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;

import com.codewithdurgesh.blog.sequrity.CustomUserDetailService;
import com.codewithdurgesh.blog.sequrity.JWTAuthenticationEntryPoint;
import com.codewithdurgesh.blog.sequrity.JwtAuthenticationFilter;

import jakarta.servlet.Filter;

@Configuration
@EnableMethodSecurity
@EnableWebMvc
@EnableWebSecurity
public class SecurityConfigs {

    public static final String[] PUBLIC_URLS = {
            "/v3/api-docs",
            "/api/v1/auth/**",
            "/v2/api-docs",
            "/v3/api-docs",
            "/swagger-resources/**",
            "/swagger-ui/**",
            "/webjars/**"
    };
    
    @Autowired
    private CustomUserDetailService customUserDetailService;

    @Autowired
    private JWTAuthenticationEntryPoint jwtAuthenticationEntryPoint;

    @Autowired
    private JwtAuthenticationFilter jwtAutheticationFilter;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @SuppressWarnings("removal")
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {

        httpSecurity.csrf().disable().authorizeHttpRequests().requestMatchers("/api/v1/auth/**").permitAll()
                .requestMatchers("/v3/api-docs").permitAll().anyRequest().authenticated().and().exceptionHandling()
                .authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
//      .formLogin();

        httpSecurity.addFilterBefore(this.jwtAutheticationFilter, UsernamePasswordAuthenticationFilter.class);

        httpSecurity.authenticationProvider(daoAuthenticationProvider());

        DefaultSecurityFilterChain defaultSecurityFilterChain = httpSecurity.build();

        return defaultSecurityFilterChain;
    }


    @Bean
    public DaoAuthenticationProvider daoAuthenticationProvider() {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setUserDetailsService(customUserDetailService);
        provider.setPasswordEncoder(passwordEncoder());

        return provider;
    }

    @Bean
    public AuthenticationManager authenticationManagerBean(AuthenticationConfiguration config) throws Exception {
        return config.getAuthenticationManager();

    }

}

below is the Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.5</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.codewithdurgesh.blog</groupId>
    <artifactId>blog-app-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>blog-app-api</name>
    <description>this is a backend APIs project for blogging</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>jakarta.validation</groupId>
            <artifactId>jakarta.validation-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <!-- https://docs.groupdocs.com/viewer/java/noclassdeffounderror-javaxxmlbind/ 
            as we were gettingjava.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter] 
            with root cause java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter 
            exception -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

        **<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>**

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>3.2.0</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

belowis the stack trace

2023-11-07T00:29:18.915+05:30 DEBUG 10440 --- [nio-8080-exec-6] o.s.security.web.FilterChainProxy        : Securing GET /v3/api-docs
request token is Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhbmtpdFhZWkB4eXouY29tIiwiZXhwIjoxNjk5MzE1MDQ0LCJpYXQiOjE2OTkyOTcwNDR9.-BvOmOxiFSdhLIoyppvs9xGkwQsDtleDFPhhRB9_SXdcyDThXRw9sKlQcFWyQ6vRfZpirpv7lQ4rwAei6CeyGA
Hibernate: 
    select
        u1_0.id,
        u1_0.about,
        u1_0.email,
        u1_0.user_name,
        u1_0.password 
    from
        users u1_0 
    where
        u1_0.email=?
Hibernate: 
    select
        r1_0.user,
        r1_1.id,
        r1_1.role_name 
    from
        user_roles r1_0 
    join
        role r1_1 
            on r1_1.id=r1_0.role 
    where
        r1_0.user=?
2023-11-07T00:29:18.938+05:30 DEBUG 10440 --- [nio-8080-exec-6] o.s.security.web.FilterChainProxy        : Secured GET /v3/api-docs
2023-11-07T00:29:18.939+05:30  WARN 10440 --- [nio-8080-exec-6] o.s.web.servlet.PageNotFound             : No mapping for GET /v3/api-docs
2023-11-07T00:29:18.940+05:30 DEBUG 10440 --- [nio-8080-exec-6] o.s.security.web.FilterChainProxy        : Securing GET /error
2023-11-07T00:29:18.942+05:30 DEBUG 10440 --- [nio-8080-exec-6] o.s.s.w.a.AnonymousAuthenticationFilter  : Set SecurityContextHolder to anonymous SecurityContext

I am not seeing any error logs for these, so it is a little confusing for me.

I tried to look for answers in stackOverFlows. I did get some questions, However I was not able to solve the issue I am facing

any help would be appreciated

thank you in advance


Solution

  • Ankit Rege Please check the below

    1. check your application.properties | yaml file for any context-Path .

    2. springfox-swagger-ui can be replace with springdoc-openapi-starter-webmvc-ui refer this Migrating from SpringFox