ServerHttpSecurity设置pathMatchers.permitAll失效,仍然401.

ServerHttpSecurity直接贴代码。
在这里插入图片描述
这是之前一直改用的配置,访问此路径一直是401(403)。但是把anyExchange也设置成permitAll。是可以访问了。(而且另一种情况:.pathMatchers(“/gateway/**”).authenticated()
.pathMatchers(“/ **”).permitAll()。这种也是可以了,就是反过来不行)。后来再想能不能吧后面这个anyExchange改一下,看了一下源码是有理由的。
贴代码2:
在这里插入图片描述
改成了这种。
accessManager代码块:

import cn.hutool.core.collection.ConcurrentHashSet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.authorization.ReactiveAuthorizationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.authorization.AuthorizationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Set;

@Component
public class AccessManager implements ReactiveAuthorizationManager<AuthorizationContext> {
    private static final AntPathMatcher antPathMatcher = new AntPathMatcher();

    public static List<String> DATABASE;

    @Value("#{'${gateway.whitelist}'.split(',')}")
    public void setDatabase(List<String> db) {
        DATABASE = db;
    }


    /**
     * 实现权限验证判断
     */
    @Override
    public Mono<AuthorizationDecision> check(Mono<Authentication> authenticationMono, AuthorizationContext authorizationContext) {
        ServerWebExchange exchange = authorizationContext.getExchange();
        //请求资源
        String requestPath = exchange.getRequest().getURI().getPath();
        // 是否直接放行
        if (permitAll(requestPath)) {
            return Mono.just(new AuthorizationDecision(true));
        }

        return authenticationMono.map(auth -> {
            return new AuthorizationDecision(checkAuthorities(exchange, auth, requestPath));
        }).defaultIfEmpty(new AuthorizationDecision(false));

    }

    /**
     * 校验是否属于静态资源
     * @param requestPath 请求路径
     * @return
     */
    private boolean permitAll(String requestPath) {
        return DATABASE.stream()
                .filter(r -> antPathMatcher.match(r.replace("/gateway", ""), requestPath)).findFirst().isPresent();
    }

    //权限校验
    private boolean checkAuthorities(ServerWebExchange exchange, Authentication auth, String requestPath) {
        Object principal = auth.getPrincipal();
        return true;
    }
}

现在就没有401,可以访问到了,看了改了anyExchange是好使的。

https://blog.csdn.net/yelvgou9995/article/details/107229699/

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慢慢CG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值