State springboot + springsecurity + vue integration projects, solve problems and cross-domain token is empty or invalid can not get the return code issues

1. In springboot solve problems of cross-domain, two ways, one is in front of the class or method name @CrossOrigin can add comments, but they are very troublesome before annotate each method, because it is before and after end of the separation, we need cross-domain access almost all of the methods, this is the time we should use another method, and you can add a configuration class

/**
 * 允许跨域访问
 * 跨域访问类配置
 */
@Configuration
public class CorsConfig {

    private CorsConfiguration buildConfig(){
        CorsConfiguration corsConfiguration=new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");//允许任何域名
        corsConfiguration.addAllowedHeader("*");//允许任何头
        corsConfiguration.addAllowedMethod("*");//允许任何方法(post,get,delete...)
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",buildConfig());
        return new CorsFilter(source);
    }
}

2. However, when integrated into springsecurity found token failure or problem status code returned is empty not obtain, because springsecurity configurations also allow cross-domain access, springsecurity class configuration plus .cors () can
Here Insert Picture Description

Published 14 original articles · won praise 6 · views 6327

Guess you like

Origin blog.csdn.net/weixin_43817709/article/details/102725202