SpringBoot2.x separating the front and rear end cross-domain issues and can not access Swagger

First, simply add some configuration, you can solve

package com.liangjian.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * Solve cross-domain access
 */

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public  void addCorsMappings (CorsRegistry Registry) {
         // settings allow cross-domain path 
        registry.addMapping ( " / ** " )
                 // set the cross-domain allows the domain name request 
                .allowedOrigins ( " * " )
                 // if the certificate is no longer allowed to default open 
                .allowCredentials ( to true )
                 // set allowedMethod 
                .allowedMethods ( " * " )
                 // cross-domain allowable time 
                .maxAge ( 3600 );
    }

    /**
     * Cross-domain configuration swagger2 may not have access, you need to add the following configuration
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        
    }

    
}

 

Guess you like

Origin www.cnblogs.com/castlechen/p/11090284.html
Recommended