Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用Satoken + SpringCloudGateWay + knife4j 异常 #680

Open
PolarishT opened this issue Sep 13, 2024 · 0 comments
Open

使用Satoken + SpringCloudGateWay + knife4j 异常 #680

PolarishT opened this issue Sep 13, 2024 · 0 comments

Comments

@PolarishT
Copy link

PolarishT commented Sep 13, 2024

使用版本:

SpringBoot 2.7.6
SpringCloudAlibaba 2021.0.5.0
SpringCloud 2021.0.5

com.github.xiaoymin knife4j-gateway-spring-boot-starter 4.4.0 cn.dev33 sa-token-reactor-spring-boot-starter 1.38.0
    <dependency>
        <groupId>cn.dev33</groupId>
        <artifactId>sa-token-redis-jackson</artifactId>
        <version>1.38.0</version>
    </dependency>

复现代码:

package com.raso.cloud.nav.auth.config.route;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.reactor.context.SaReactorSyncHolder;
import cn.dev33.satoken.reactor.filter.SaReactorFilter;
import cn.dev33.satoken.router.SaHttpMethod;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.raso.cloud.nav.auth.util.ResponseUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;

import javax.annotation.Resource;
import java.util.Map;
import java.util.stream.Collectors;

/**

  • [Sa-Token 权限认证] 配置类

  • @author 北极星
    */
    @configuration
    public class SaTokenConfig {

    @resource
    WhiteRouteList whiteRouteList;

    @resource
    PermissionRouteList permissionRouteList;

    @bean
    public SaReactorFilter getSaReactorFilter() {

     return new SaReactorFilter()
    
             // 拦截地址
             .addInclude("/**")
             // 开放地址
             .addExclude(
                     "/doc.html",
                     "/swagger-ui/**",
                     "/webjars/**",
                     "/v2/api-docs/**",
                     "/error"
             )
    

// .setExcludeList(whiteRouteList.getRoutes())

            .setAuth(obj -> {

                SaRouter.notMatch(whiteRouteList.getRoutes());
                // 登录校验
                SaRouter.match("/**","/ua/register", r -> StpUtil.checkLogin());

                // 角色校验

// SaRouter.match("/**", r -> StpUtil.checkRole("normal_user"));

                // 权限校验
                Map<String, String> rules = permissionRouteList
                        .getRoutes()
                        .entrySet()
                        .stream()
                        .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
                for (String path : rules.keySet()) {
                    SaRouter.match(path, () ->
                            StpUtil.hasPermissionOr("user.add", "user.delete", "user.get", "user.query")
                    );
                }
            })
            .setError(e -> {
                System.out.println(e.getMessage());
                ServerWebExchange exchange = SaReactorSyncHolder.getContext();
                exchange.getResponse().getHeaders().set("Content-Type", "application/json; charset=utf-8");
                ResponseUtil resp = ResponseUtil.res(HttpStatus.UNAUTHORIZED, "用户未授权");
                return new Gson().toJson(resp);
            })
            .setBeforeAuth(obj -> {

                SaHolder.getResponse()
                        .setHeader("Access-Control-Allow-Origin", "*")
                        .setHeader("Access-Control-Allow-Methods", "POST, GET")
                        .setHeader("Access-Control-Max-Age", "3600")
                        .setHeader("Access-Control-Allow-Headers", "*");

                SaRouter.match(SaHttpMethod.OPTIONS).back();
            });
}

}

application.yml
knife4j:
gateway:
enabled: true
strategy: discover
discover:
enabled: true
version: swagger2

报错信息:

未能读取到有效 token
2024-09-13 21:13:20.002 TRACE 3771 --- [ctor-http-nio-4] o.s.c.g.f.WeightCalculatorWebFilter : Weights attr: {}
2024-09-13 21:13:20.002 TRACE 3771 --- [ctor-http-nio-4] o.s.c.g.h.p.PathRoutePredicateFactory : Pattern "[/oss/]" does not match against value "/doc.html"
2024-09-13 21:13:20.003 TRACE 3771 --- [ctor-http-nio-4] o.s.c.g.h.p.PathRoutePredicateFactory : Pattern "[/test/
]" does not match against value "/doc.html"
2024-09-13 21:13:20.003 TRACE 3771 --- [ctor-http-nio-4] o.s.c.g.h.p.PathRoutePredicateFactory : Pattern "[/auth/**]" does not match against value "/doc.html"
2024-09-13 21:13:20.003 TRACE 3771 --- [ctor-http-nio-4] o.s.c.g.h.RoutePredicateHandlerMapping : No RouteDefinition found for [Exchange: GET http://localhost:6001/doc.html]
2024-09-13 21:13:20.003 DEBUG 3771 --- [ctor-http-nio-4] o.s.w.r.handler.SimpleUrlHandlerMapping : [1d44b4bb-23] Mapped to ResourceWebHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/]]

希望结果:

怎么配置了这个排除的路径,还是显示没有token,被拦截了呢

< 备注:如果复现步骤比较复杂,请将 demo 上传到 gitee 并留下地址 >

@PolarishT PolarishT changed the title 使用Satoken 使用Satoken + SpringCloudGateWay + swagger 异常 Sep 13, 2024
@PolarishT PolarishT changed the title 使用Satoken + SpringCloudGateWay + swagger 异常 使用Satoken + SpringCloudGateWay + knife4j 异常 Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant