最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud Gateway鑒權(quán)和跨域解決方案

 更新時間:2023年11月24日 10:01:40   作者:Doker 多克  
網(wǎng)關(guān)是介于客戶端和服務(wù)器端之間的中間層,所有的外部請求都會先經(jīng)過 網(wǎng)關(guān)這一層,也就是說,API 的實(shí)現(xiàn)方面更多的考慮業(yè)務(wù)邏輯,而安全、性能、監(jiān)控可以交由 網(wǎng)關(guān)來做,這樣既提高業(yè)務(wù)靈活性又不缺安全性,本文給大家介紹SpringCloud Gateway鑒權(quán)和跨域解決方案,一起看看吧

一、Gateway鑒權(quán)實(shí)現(xiàn)方案

網(wǎng)關(guān)是介于客戶端和服務(wù)器端之間的中間層,所有的外部請求都會先經(jīng)過 網(wǎng)關(guān)這一層。也就是說,API 的實(shí)現(xiàn)方面更多的考慮業(yè)務(wù)邏輯,而安全、性能、監(jiān)控可以交由 網(wǎng)關(guān)來做,這樣既提高業(yè)務(wù)靈活性又不缺安全性。

RBAC(Role-Based Access Control)基于角色訪問控制,目前使用最為廣泛的權(quán)限模型。相信大家對這種權(quán)限模型已經(jīng)比較了解了。此模型有三個用戶、角色和權(quán)限,在傳統(tǒng)的權(quán)限模型用戶直接關(guān)聯(lián)加了角色,解耦了用戶和權(quán)限,使得權(quán)限系統(tǒng)有了更清晰的職責(zé)劃分和更高的靈活度

1、添加依賴

 
<dependency>
  <groupId>io.jsonwebtoken</groupId>
   <artifactId>jjwt</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

2、實(shí)現(xiàn)代碼

 
@Configuration
@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {
    @Autowired
    JwtTokenUtil jwtTokenUtil;
    @Autowired(required = false)
    JedisUtil jedisUtil;
    private String cachePrefix = "km-gateway-";
    @Value("${spring.redis.expired}")
    private Integer expiredSecond;//600000,10m
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        HttpHeaders httpHeaders = request.getHeaders();
        exchange.getRequest().getURI();
        String requestUri = request.getPath().pathWithinApplication().value();
        String token = null;
        if (httpHeaders != null && httpHeaders.containsKey("token") && !httpHeaders.get("token").isEmpty()) {
            token = httpHeaders.get("token").get(0);
        }
//        AuthenticateRequest
        if (StringUtil.isBlank(token)) {
//            String message = "You current request uri do not have permission or auth.";
//            return getVoidMono(exchange, message);
            return chain.filter(exchange);
        }
        String userAccountId = jwtTokenUtil.getUserAccountIdFromToken(token);
        boolean hasPermission = checkPermission(userAccountId, requestUri);
        String username = jwtTokenUtil.getUsernameFromToken(token);
        String redisSetUrlKey = cachePrefix.concat("url-").concat(username);
        //  log.info("###### hasPermission.2=" + hasPermission);
        if (hasPermission) {
            jedisUtil.SetAndTime(redisSetUrlKey, expiredSecond, requestUri);
        } else {
            String message = "You current request uri do not have permission or auth.";
            // log.warn(message);
            return getVoidMono(exchange, message);
        }
        jwtTokenUtil.isValid(token);
        return chain.filter(exchange);
    }
    @Override
    public int getOrder() {
        return 0;
    }
   //根據(jù)角色權(quán)限進(jìn)行權(quán)限控制
    private boolean checkPermission(String userId, String requestUrl) {
        return false;
    }
    private Mono<Void> getVoidMono(ServerWebExchange exchange, String body) {
        exchange.getResponse().setStatusCode(HttpStatus.OK);
        byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes);
        return exchange.getResponse().writeWith(Flux.just(buffer));
    }
}

二、Gateway跨域解決方案

在SpringCloud項(xiàng)目中,前后端分離目前很常見,在調(diào)試時會遇到前端頁面通過不同域名或IP訪問微服務(wù)的后臺,此時,如果不加任何配置,前端頁面的請求會被瀏覽器跨域限制攔截,所以,業(yè)務(wù)服務(wù)常常會添加跨域配置

1、配置類實(shí)現(xiàn)

 
@Configuration
public class GulimallCorsConfiguration {
    /**
     * 添加跨域過濾器
     * @return
     */
    @Bean
    public CorsWebFilter corsWebFilter(){
        //基于url跨域,選擇reactive包下的
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 跨域配置信息
        CorsConfiguration configuration = new CorsConfiguration();
        // 允許跨域的頭
        configuration.addAllowedHeader("*");
        // 允許跨域的請求方式
        configuration.addAllowedMethod("*");
        // 允許跨域的請求來源
        configuration.addAllowedOrigin("*");
        // 是否允許攜帶cookie跨域
        configuration.setAllowCredentials(true);
        // 任意url都要進(jìn)行跨域配置
        source.registerCorsConfiguration("/**", configuration);
        return new CorsWebFilter(source);
    }
}
注: SpringCloudGateWay中跨域配置不起作用,原因是SpringCloudGetway是 Springwebflux的而不是SpringWebMvc的,所以我們需要導(dǎo)入的包導(dǎo)入錯了

2、配置文件配置

 
server:
  port: 10010
spring:
  application:
    name: gatewayservice
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "https://www.xx.com" # 允許那些網(wǎng)站跨域訪問
            allowedMethods: "GET" # 允許那些Ajax方式的跨域請求
            allowedHeaders: "*" # 允許請求頭攜帶信息
            allowCredentials: "*" # 允許攜帶cookie
            maxAge: 360000 # 這次跨域有效期于相同的跨域請求不會再預(yù)檢

到此這篇關(guān)于SpringCloud Gateway鑒權(quán)和跨域解決方案的文章就介紹到這了,更多相關(guān)SpringCloud Gateway鑒權(quán)和跨域內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

蓬溪县| 琼结县| 衢州市| 宁河县| 成武县| 岗巴县| 房产| 桃源县| 勃利县| 永年县| 延寿县| 信宜市| 桂阳县| 正镶白旗| 汕尾市| 德令哈市| 博乐市| 民县| 漯河市| 民勤县| 呼玛县| 东乡族自治县| 靖江市| 阿克陶县| 保山市| 本溪| 博客| 贵德县| 高雄市| 维西| 双桥区| 佛教| 台江县| 南和县| 建平县| 阿拉善盟| 张家口市| 淳化县| 久治县| 永泰县| 交口县|