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());
}

 

Spring Security OAuth2 是 Spring Security 的子项目,用于支持 OAuth2 协议。其源码包括了授权服务器和资源服务器两部分,下面分别介绍。 ## 授权服务器源码 Spring Security OAuth2 的授权服务器源码主要包括以下几个模块: 1. oauth2-core:包括 OAuth2 协议的核心实现,如授权码、令牌等的生成和验证。 2. oauth2-jwt:包括 JWT 令牌的生成和解析。 3. oauth2-authorization-server:包括授权服务器的实现,包括授权码模式、密码模式、客户端模式等。 其中,授权服务器的实现是基于 Spring MVC 的,主要包括以下几个核心类: 1. AuthorizationEndpoint:处理授权端点,包括授权码模式、密码模式、客户端模式等。 2. TokenEndpoint:处理令牌端点,包括颁发访问令牌、刷新令牌等。 3. WhitelabelApprovalEndpoint:处理用户授权页面,提供用户授权功能。 ## 资源服务器源码 Spring Security OAuth2 的资源服务器源码主要包括以下几个模块: 1. oauth2-resource:包括资源服务器的核心实现,如令牌解析和访问控制等。 2. oauth2-jwt:包括 JWT 令牌的生成和解析。 其中,资源服务器的实现是基于 Spring Security 的,主要包括以下几个核心类: 1. ResourceServerConfigurerAdapter:用于配置资源服务器,包括资源的访问控制、令牌的解析等。 2. JwtAccessTokenConverter:用于将 JWT 令牌转换为 OAuth2 令牌。 以上是 Spring Security OAuth2 的授权服务器和资源服务器的主要源码模块和类,具体实现方式还需要根据实际需求进行具体的配置和实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值