浏览代码

first commit

pull/8/head
chenjiandong 2 年前
当前提交
6e31c72e67
共有 2 个文件被更改,包括 100 次插入0 次删除
  1. +37
    -0
      src/main/java/com/tuoheng/gateway/config/GatewayCorsConfig.java
  2. +63
    -0
      src/main/java/com/tuoheng/gateway/config/WebSecurityConfig.java

+ 37
- 0
src/main/java/com/tuoheng/gateway/config/GatewayCorsConfig.java 查看文件

@@ -0,0 +1,37 @@
package com.tuoheng.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
* 跨域处理
*/
@Configuration
public class GatewayCorsConfig {

/**
* 跨域处理
*
* @return
*/
@Bean
public CorsWebFilter corsFilter() {

// 跨域请求配置
CorsConfiguration configuration = new CorsConfiguration();
// 在生产环境上最好指定域名,以免产生跨域安全问题
configuration.addAllowedOrigin("*");
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", configuration);

return new CorsWebFilter(source);
}

}

+ 63
- 0
src/main/java/com/tuoheng/gateway/config/WebSecurityConfig.java 查看文件

@@ -0,0 +1,63 @@
package com.tuoheng.gateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;

import java.util.List;

/**
* @author chenjiandong
* @description: TODO
* @date 2022/9/28 13:53
*/
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebFluxSecurity
public class WebSecurityConfig {

/**
* 需要登录后访问的接口
*/
public static String oauthUrlStr;

/**
* 不需要登录就可以访问的接口
*/
public static String permitUrlStr;

@Value("${security.ignore.oauthUrls}")
public void setOauthUrlStr(String oauthUrls) {
oauthUrlStr = oauthUrls;
}

@Value("${security.ignore.permitUrls}")
public void setPermitUrlStr(String permitUrls) {
permitUrlStr = permitUrls;
}

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity httpSecurity){
String[] OAUTH_PATH = oauthUrlStr.split(",");
String[] PERMIT_PATH = permitUrlStr.split(",");
httpSecurity
.authorizeExchange()
.pathMatchers(OAUTH_PATH).authenticated()
.pathMatchers(PERMIT_PATH).permitAll()
.pathMatchers("/api/system/demo/test").hasAuthority("SCOPE_email")
.anyExchange().permitAll()
.and()
.csrf()
.disable()
.cors();
httpSecurity.oauth2ResourceServer().jwt();
return httpSecurity.build();
}

}

正在加载...
取消
保存