springboot security oauth2解决,前端VUE项目打包部署时,跨域访问问题

 /oauth/token跨域问题,无法获取

参考文章:

https://blog.csdn.net/GeorgeShaw1/article/details/75089734

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 允许跨域访问
 * 解决/oauth/token无法跨域问题
 * @author NPF
 * @date 2020/12/30
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 1允许任何域名使用
        config.addAllowedOrigin("*");
        // 2允许任何头
        config.addAllowedHeader("*");
        // 3允许任何方法(post、get等)
        config.addAllowedMethod(HttpMethod.GET);
        config.addAllowedMethod(HttpMethod.POST);
        config.addAllowedMethod(HttpMethod.PUT);
        config.addAllowedMethod(HttpMethod.DELETE);
        config.addAllowedMethod(HttpMethod.OPTIONS);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

}

 

允许所有资源跨域访问

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 设置系统资源允许跨域访问
 * @author HLN
 * @date 2020/02/04 08:30
 */
@Configuration
public class WebMvcConfigurer implements WebMvcConfigurer {
    
    /**
     * 设置系统资源允许跨域访问
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOrigins("*")
                //这里:是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                //跨域允许时间
                .maxAge(3600);
    }
}

 

OPTIONS类型,访问后台资源报401,提示需要token

/**
 * 设置匿名访问地址清单
 * @param http
 * @throws Exception
 */
@Override
public void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        //swagger-ui.html 相关路径可以匿名访问
        .antMatchers("/v2/api-docs", "/favicon.ico",
                "/swagger-resources/configuration/ui",
                "/swagger-resources",
                "/swagger-resources/configuration/security",
                "/swagger-ui.html",
                )
        .permitAll()
        // 设置OPTIONS类型,不需要认证,解决CORS跨域问题
        .antMatchers(HttpMethod.OPTIONS).permitAll()
        .anyRequest().authenticated()
        .and()
        .logout()
        .logoutUrl("/oauth/logout")
        //退出成功的业务处理
        .logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler());
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值