opensolon / solon

🔥 Java "ecological" application development Framework: Restrained, concise, EFFICIENT, OPEN, ECOLOGICAL!! 300% higher concurrency 50% memory savings Startup is 10 times faster. Packing 90% smaller; Compatible with java8 ~ java23. (Replaceable spring)
https://solon.noear.org
Apache License 2.0
2.29k stars 224 forks source link

Auth 中 exclude的参数疑问 #231

Closed z2z2qp closed 10 months ago

z2z2qp commented 10 months ago

controller代码

@Get
@Mapping("/captchaImage")
public AjaxResult<Captcha> getCaptcha() {
    //具体实现
}

AuthAdapter 部分代码

        return new AuthAdapter()
                .addRule(b -> b.exclude("/login**")
                        .exclude("/captchaImage")
                        .exclude("/register**")
                        .exclude("/refresh**")
                        .exclude("/version")
                        .verifyLogined())
                .processor(processor) //设定认证处理器
                .failure(((ctx, rst) -> {
                    ctx.render(AjaxResult.fail(rst.getCode(), "认证失败"));
                }));

结果不符合预期提示认证失败

curl localhost:8888/captchaImage
{"code":401,"msg":"认证失败"}

当把 .exclude("/captchaImage") 中的captchaImage替换成captchaimage 即 .exclude("/captchaimage") 时候结果符合预期,提示操作成功

curl localhost:8888/captchaImage
{"code":200,"msg":"操作成功"}

这个exclude的参数是有什么只认小写么?直观感受不应该是和@Mapping中的大小写一致么?

noear commented 10 months ago

有点意外啊。看了下用的匹配代码是同一份的。你用的 solon 是什么版本?

noear commented 10 months ago

这是我写的一个单测,没问题

public class RouteTest {
    @Test
    public void routingDefault() {
        //Mvc 里用的 Mapping 路由记录
        RoutingDefault routingDefault = new RoutingDefault("/captchaImage", MethodType.GET, null);
        assert routingDefault.matches(MethodType.GET, "/captchaImage");
        assert routingDefault.matches(MethodType.GET, "/captchaimage") == false;

        routingDefault = new RoutingDefault("/captchaimage", MethodType.GET, null);
        assert routingDefault.matches(MethodType.GET, "/captchaImage") == false;
        assert routingDefault.matches(MethodType.GET, "/captchaimage");
    }

    @Test
    public void authRuleImpl() {
        //AuthRuleImpl 内用的路由规则
        PathRule pathRule = new PathRule();
        pathRule.exclude("/captchaImage");
        assert pathRule.test("/captchaImage") == false;
        assert pathRule.test("/captchaimage");

        pathRule = new PathRule();
        pathRule.exclude("/captchaimage");
        assert pathRule.test("/captchaImage");
        assert pathRule.test("/captchaimage") == false;

        pathRule = new PathRule();
        pathRule.include("/captchaImage");
        assert pathRule.test("/captchaImage");
        assert pathRule.test("/captchaimage") == false;

        pathRule = new PathRule();
        pathRule.include("/captchaimage");
        assert pathRule.test("/captchaImage") == false;
        assert pathRule.test("/captchaimage");
    }
}
z2z2qp commented 10 months ago

有点意外啊。看了下用的匹配代码是同一份的。你用的 solon 是什么版本?

<parent>
        <groupId>org.noear</groupId>
        <artifactId>solon-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/>
    </parent>
z2z2qp commented 10 months ago

这是我写的一个单测,没问题

public class RouteTest {
    @Test
    public void routingDefault() {
        //Mvc 里用的 Mapping 路由记录
        RoutingDefault routingDefault = new RoutingDefault("/captchaImage", MethodType.GET, null);
        assert routingDefault.matches(MethodType.GET, "/captchaImage");
        assert routingDefault.matches(MethodType.GET, "/captchaimage") == false;

        routingDefault = new RoutingDefault("/captchaimage", MethodType.GET, null);
        assert routingDefault.matches(MethodType.GET, "/captchaImage") == false;
        assert routingDefault.matches(MethodType.GET, "/captchaimage");
    }

    @Test
    public void authRuleImpl() {
        //AuthRuleImpl 内用的路由规则
        PathRule pathRule = new PathRule();
        pathRule.exclude("/captchaImage");
        assert pathRule.test("/captchaImage") == false;
        assert pathRule.test("/captchaimage");

        pathRule = new PathRule();
        pathRule.exclude("/captchaimage");
        assert pathRule.test("/captchaImage");
        assert pathRule.test("/captchaimage") == false;

        pathRule = new PathRule();
        pathRule.include("/captchaImage");
        assert pathRule.test("/captchaImage");
        assert pathRule.test("/captchaimage") == false;

        pathRule = new PathRule();
        pathRule.include("/captchaimage");
        assert pathRule.test("/captchaImage") == false;
        assert pathRule.test("/captchaimage");
    }
}

你的测试我也跑了,没问题,但是请看图 200 401

方便的话你可以直接看的代码 https://gitee.com/z2z2qp/solon-flex 配置代码路径 src/main/java/com/zjb/demo/common/config/AuthConfig.java 测试代码路径 src/test/java/features/RouteTest.java

noear commented 10 months ago

帮忙调试调试,找找真正的原因。

z2z2qp commented 10 months ago

帮忙调试调试,找找真正的原因。

找到了 org.noear.solon.auth.impl.AuthRuleImpl#handle() 方法第一行 String path = ctx.pathNew().toLowerCase(); 对此你怎么看😂

noear commented 10 months ago

泪目。。。要不要帮忙提个 pr ?。。。基于 dev 修改,完了 pr 到 dev

noear commented 10 months ago

新的快照片已发: 2.6.5-SNAPSHOT

noear commented 10 months ago

发了测试版,试下:2.6.5-M1

noear commented 10 months ago

有试过了吗?

z2z2qp commented 10 months ago

2.6.5-SNAPSHOT 正常可用了