//去除gateway path 前缀 | //去除gateway path 前缀 | ||||
String apiUrl = requestUrl.replace(GatewayUrlPathUtil.getPathByClientId(clientId),""); | String apiUrl = requestUrl.replace(GatewayUrlPathUtil.getPathByClientId(clientId),""); | ||||
List<Integer> roleIds = GatewayUrlPathUtil.getRoleIdByApiUrlPermission(clientId, apiUrl, token); | List<Integer> roleIds = GatewayUrlPathUtil.getRoleIdByApiUrlPermission(clientId, apiUrl, token); | ||||
log.info("roleIds is :{}", clientId); | |||||
log.info("roleIds is :{}", roleIds); | |||||
//return invalidClientIdMono(exchange); | //return invalidClientIdMono(exchange); | ||||
if(roleIds.size() > 0){ | if(roleIds.size() > 0){ | ||||
//说明这个url 需要一定的角色才可以访问 | //说明这个url 需要一定的角色才可以访问 |
package com.tuoheng.gateway.config; | package com.tuoheng.gateway.config; | ||||
import com.tuoheng.gateway.constants.AuthorityConstant; | import com.tuoheng.gateway.constants.AuthorityConstant; | ||||
import com.tuoheng.gateway.constants.PermitPathConstant; | |||||
import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
import org.springframework.beans.factory.annotation.Value; | import org.springframework.beans.factory.annotation.Value; | ||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties; | import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties; | ||||
@Bean | @Bean | ||||
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity httpSecurity){ | public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity httpSecurity){ | ||||
String[] OAUTH_PATH = oauthUrlStr.split(","); | String[] OAUTH_PATH = oauthUrlStr.split(","); | ||||
String[] PERMIT_PATH = permitUrlStr.split(","); | |||||
String[] HhzPermitPath = PermitPathConstant.hhzPermitUrlStr; | |||||
httpSecurity | httpSecurity | ||||
.authorizeExchange() | .authorizeExchange() | ||||
.pathMatchers(OAUTH_PATH).hasAnyAuthority(AuthorityConstant.SCOPE_ADMIN, AuthorityConstant.SCOPE_TUOHNEG_DSP_MP, AuthorityConstant.SCOPE_TUOHNEG_DSP_WEB) | .pathMatchers(OAUTH_PATH).hasAnyAuthority(AuthorityConstant.SCOPE_ADMIN, AuthorityConstant.SCOPE_TUOHNEG_DSP_MP, AuthorityConstant.SCOPE_TUOHNEG_DSP_WEB) | ||||
.pathMatchers(HhzPermitPath).permitAll() | |||||
.pathMatchers("/pilot/miniprogram/**").hasAnyAuthority(AuthorityConstant.SCOPE_ADMIN, AuthorityConstant.SCOPE_TUOHNEG_PILOT_MP) | .pathMatchers("/pilot/miniprogram/**").hasAnyAuthority(AuthorityConstant.SCOPE_ADMIN, AuthorityConstant.SCOPE_TUOHNEG_PILOT_MP) | ||||
.pathMatchers("/pilot/admin/**").hasAnyAuthority(AuthorityConstant.SCOPE_ADMIN, AuthorityConstant.SCOPE_TUOHNEG_PILOT_ADMIN) | .pathMatchers("/pilot/admin/**").hasAnyAuthority(AuthorityConstant.SCOPE_ADMIN, AuthorityConstant.SCOPE_TUOHNEG_PILOT_ADMIN) | ||||
.pathMatchers("/hhz/admin/**").hasAnyAuthority(AuthorityConstant.SCOPE_ADMIN, AuthorityConstant.SCOPE_TUOHNEG_HHZ_ADMIN) | .pathMatchers("/hhz/admin/**").hasAnyAuthority(AuthorityConstant.SCOPE_ADMIN, AuthorityConstant.SCOPE_TUOHNEG_HHZ_ADMIN) | ||||
.pathMatchers("/hhz/api/**").hasAnyAuthority(AuthorityConstant.SCOPE_ADMIN, AuthorityConstant.SCOPE_TUOHNEG_HHZ_MP) | |||||
.pathMatchers("/oidc/admin/**").authenticated() | .pathMatchers("/oidc/admin/**").authenticated() | ||||
//.pathMatchers(PERMIT_PATH).permitAll() | //.pathMatchers(PERMIT_PATH).permitAll() | ||||
.anyExchange().permitAll() | .anyExchange().permitAll() |
*/ | */ | ||||
public static final String SCOPE_TUOHNEG_HHZ_ADMIN = "SCOPE_tuoheng-hhz-admin"; | public static final String SCOPE_TUOHNEG_HHZ_ADMIN = "SCOPE_tuoheng-hhz-admin"; | ||||
public static final String SCOPE_TUOHNEG_HHZ_MP = "SCOPE_tuoheng-hhz-mp"; | |||||
} | } |
package com.tuoheng.gateway.constants; | |||||
/** | |||||
* @author chenjiandong | |||||
* @description: TODO | |||||
* @date 2022/12/1 8:49 | |||||
*/ | |||||
public class PermitPathConstant { | |||||
public static String hhzPermitUrlStr[] = { | |||||
"/hhz/admin/analyse/**", | |||||
"/hhz/admin/websocket/**", | |||||
"/hhz/admin/login/**", | |||||
"/hhz/admin/meeting/updatePeopleStatus/**", | |||||
"/hhz/admin/download/workUserExcel/**", | |||||
"/hhz/admin/tencentCloudRtc/genUserSig/**", | |||||
"/hhz/admin/inspection/track/**", | |||||
"/hhz/admin/inspection/uploadFlightUrl/**", | |||||
"/hhz/admin/inspection/updateTaskByCode/**", | |||||
"/hhz/admin/inspection/status/**", | |||||
"/hhz/admin/taskFile/**", | |||||
"/hhz/admin/flightdata/**", | |||||
"/hhz/admin/tenant/**", | |||||
"/hhz/admin/dsp/**", | |||||
"/hhz/admin/common/**", | |||||
"/hhz/api/tenant/**", | |||||
"/hhz/api/dsp/**", | |||||
"/hhz/api/meeting/updatePeopleStatus/**", | |||||
"/hhz/api/common/**", | |||||
"/hhz/api/common/**", | |||||
"/hhz/api/apiConfig/getConfigInfo/**" | |||||
}; | |||||
} |
private static final String HHZ_ADMIN = "tuoheng-hhz-admin"; | private static final String HHZ_ADMIN = "tuoheng-hhz-admin"; | ||||
private static final String HHZ_MP = "tuoheng-hhz-mp"; | |||||
/** | /** | ||||
* 获取 gateway 路由前缀,匹配url | * 获取 gateway 路由前缀,匹配url | ||||
* @param clientId | * @param clientId | ||||
case HHZ_ADMIN: | case HHZ_ADMIN: | ||||
apiPath = "/hhz/admin"; | apiPath = "/hhz/admin"; | ||||
break; | break; | ||||
case HHZ_MP: | |||||
apiPath = "/hhz/api"; | |||||
break; | |||||
} | } | ||||
return apiPath; | return apiPath; | ||||
} | } | ||||
*/ | */ | ||||
public static List<Integer> getRoleIdByApiUrlPermission(String clientId, String apiUrl, String token){ | public static List<Integer> getRoleIdByApiUrlPermission(String clientId, String apiUrl, String token){ | ||||
List<Integer> resList = new ArrayList<>(); | List<Integer> resList = new ArrayList<>(); | ||||
if(clientId.equals(HHZ_ADMIN)){ | |||||
if(clientId.equals(HHZ_ADMIN) || clientId.equals(HHZ_MP)){ | |||||
String url = CommonsConfig.hhzPermissionUrl; | String url = CommonsConfig.hhzPermissionUrl; | ||||
HttpHeaders resultRequestHeader = new HttpHeaders(); | HttpHeaders resultRequestHeader = new HttpHeaders(); | ||||
resultRequestHeader.add("Authorization", "Bearer " + token); | resultRequestHeader.add("Authorization", "Bearer " + token); |
- Path=/pilot/web/** | - Path=/pilot/web/** | ||||
filters: | filters: | ||||
- StripPrefix=2 | - StripPrefix=2 | ||||
# freeway后台管理 | |||||
- id: tuoheng-freeway-admin | |||||
uri: lb://tuoheng-freeway-admin | |||||
predicates: | |||||
- Path=/freeway/admin/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# freeway小程序服务 | |||||
- id: tuoheng-freeway-miniprogram | |||||
uri: lb://tuoheng-freeway-miniprogram | |||||
predicates: | |||||
- Path=/freeway/miniprogram/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# freeway api服务 | |||||
- id: tuoheng-freeway-api | |||||
uri: lb://tuoheng-freeway-api | |||||
predicates: | |||||
- Path=/freeway/web/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# oidc admin服务 | # oidc admin服务 | ||||
- id: tuoheng-oidc-admin | - id: tuoheng-oidc-admin | ||||
uri: lb://tuoheng-oidc-admin | uri: lb://tuoheng-oidc-admin | ||||
- Path=/hhz/admin/** | - Path=/hhz/admin/** | ||||
filters: | filters: | ||||
- StripPrefix=2 | - StripPrefix=2 | ||||
# hhz 小程序服务 | |||||
- id: tuoheng-hhz-api | |||||
uri: lb://tuoheng-hhz-api | |||||
predicates: | |||||
- Path=/hhz/api/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# Redis数据源 | # Redis数据源 | ||||
redis: | redis: | ||||
# 缓存库默认索引0 | # 缓存库默认索引0 | ||||
# 获取 apiUrl 可访问的 roleIdList | # 获取 apiUrl 可访问的 roleIdList | ||||
tuoheng: | tuoheng: | ||||
hhz-admin-perUrl: http://192.168.11.22:9055/api/permission/getRoleIdList | |||||
hhz-admin-perUrl: http://192.168.11.11:9055/permission/getRoleIdList |
- Path=/pilot/web/** | - Path=/pilot/web/** | ||||
filters: | filters: | ||||
- StripPrefix=2 | - StripPrefix=2 | ||||
# freeway后台管理 | |||||
- id: tuoheng-freeway-admin | |||||
uri: lb://tuoheng-freeway-admin | |||||
predicates: | |||||
- Path=/freeway/admin/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# freeway小程序服务 | |||||
- id: tuoheng-freeway-miniprogram | |||||
uri: lb://tuoheng-freeway-miniprogram | |||||
predicates: | |||||
- Path=/freeway/miniprogram/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# freeway api服务 | |||||
- id: tuoheng-freeway-api | |||||
uri: lb://tuoheng-freeway-api | |||||
predicates: | |||||
- Path=/freeway/web/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# oidc admin服务 | # oidc admin服务 | ||||
- id: tuoheng-oidc-admin | - id: tuoheng-oidc-admin | ||||
uri: lb://tuoheng-oidc-admin | uri: lb://tuoheng-oidc-admin | ||||
- Path=/hhz/admin/** | - Path=/hhz/admin/** | ||||
filters: | filters: | ||||
- StripPrefix=2 | - StripPrefix=2 | ||||
# hhz 小程序服务 | |||||
- id: tuoheng-hhz-api | |||||
uri: lb://tuoheng-hhz-api | |||||
predicates: | |||||
- Path=/hhz/api/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# Redis数据源 | # Redis数据源 | ||||
redis: | redis: | ||||
# 缓存库默认索引0 | # 缓存库默认索引0 |
- Path=/pilot/web/** | - Path=/pilot/web/** | ||||
filters: | filters: | ||||
- StripPrefix=2 | - StripPrefix=2 | ||||
# freeway后台管理 | |||||
- id: tuoheng-freeway-admin | |||||
uri: lb://tuoheng-freeway-admin | |||||
predicates: | |||||
- Path=/freeway/admin/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# freeway小程序服务 | |||||
- id: tuoheng-freeway-miniprogram | |||||
uri: lb://tuoheng-freeway-miniprogram | |||||
predicates: | |||||
- Path=/freeway/miniprogram/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# freeway api服务 | |||||
- id: tuoheng-freeway-api | |||||
uri: lb://tuoheng-freeway-api | |||||
predicates: | |||||
- Path=/freeway/web/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# oidc admin服务 | # oidc admin服务 | ||||
- id: tuoheng-oidc-admin | - id: tuoheng-oidc-admin | ||||
uri: lb://tuoheng-oidc-admin | uri: lb://tuoheng-oidc-admin | ||||
- Path=/hhz/admin/** | - Path=/hhz/admin/** | ||||
filters: | filters: | ||||
- StripPrefix=2 | - StripPrefix=2 | ||||
# hhz 小程序服务 | |||||
- id: tuoheng-hhz-api | |||||
uri: lb://tuoheng-hhz-api | |||||
predicates: | |||||
- Path=/hhz/api/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# Redis数据源 | # Redis数据源 | ||||
redis: | redis: | ||||
# 缓存库默认索引0 | # 缓存库默认索引0 |
- Path=/pilot/web/** | - Path=/pilot/web/** | ||||
filters: | filters: | ||||
- StripPrefix=2 | - StripPrefix=2 | ||||
# freeway后台管理 | |||||
- id: tuoheng-freeway-admin | |||||
uri: lb://tuoheng-freeway-admin | |||||
predicates: | |||||
- Path=/freeway/admin/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# freeway小程序服务 | |||||
- id: tuoheng-freeway-miniprogram | |||||
uri: lb://tuoheng-freeway-miniprogram | |||||
predicates: | |||||
- Path=/freeway/miniprogram/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# freeway api服务 | |||||
- id: tuoheng-freeway-api | |||||
uri: lb://tuoheng-freeway-api | |||||
predicates: | |||||
- Path=/freeway/web/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# oidc admin服务 | # oidc admin服务 | ||||
- id: tuoheng-oidc-admin | - id: tuoheng-oidc-admin | ||||
uri: lb://tuoheng-oidc-admin | uri: lb://tuoheng-oidc-admin | ||||
- Path=/hhz/admin/** | - Path=/hhz/admin/** | ||||
filters: | filters: | ||||
- StripPrefix=2 | - StripPrefix=2 | ||||
# hhz 小程序服务 | |||||
- id: tuoheng-hhz-api | |||||
uri: lb://tuoheng-hhz-api | |||||
predicates: | |||||
- Path=/hhz/api/** | |||||
filters: | |||||
- StripPrefix=2 | |||||
# Redis数据源 | # Redis数据源 | ||||
redis: | redis: | ||||
# 缓存库默认索引0 | # 缓存库默认索引0 | ||||
# 获取 apiUrl 可访问的 roleIdList | # 获取 apiUrl 可访问的 roleIdList | ||||
tuoheng: | tuoheng: | ||||
hhz-admin-perUrl: http://192.168.11.22:9055/api/permission/getRoleIdList | |||||
hhz-admin-perUrl: http://172.15.1.21:9055/permission/getRoleIdList |