macrozheng / mall-swarm

mall-swarm是一套微服务商城系统,采用了 Spring Cloud 2021 & Alibaba、Spring Boot 2.7、Oauth2、MyBatis、Docker、Elasticsearch、Kubernetes等核心技术,同时提供了基于Vue的管理后台方便快速搭建系统。mall-swarm在电商业务的基础集成了注册中心、配置中心、监控中心、网关等系统功能。文档齐全,附带全套Spring Cloud教程。

Home Page:https://www.macrozheng.com/admin/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

return mono.filter(Authentication::isAuthenticated) .flatMapIterable(Authentication::getAuthorities).map(GrantedAuthority::getAuthority) 这段怎么理解

YitianJiang opened this issue · comments

    return mono
            .filter(Authentication::isAuthenticated)
            .flatMapIterable(Authentication::getAuthorities)
            .map(GrantedAuthority::getAuthority)

这段怎么理解

说说我的理解:
return mono
//jwt一定是需要鉴权的,".filter(Authentication::isAuthenticated)"一句可以不加
//猜测:jwt传进来后,jwt中的authorities字段对应的内容(也就是用户拥有roles),一个个都被封装成GrantedAuthority的一
//个实现,比如SimpleGrantedAuthority
//(猜测来源: 从SimpleGrantedAuthority看到,getAuthority返回的是“role”,也就是jwt中用户所拥有的角色)
//----------------------------------------------------------------------------------------
//Authentication::getAuthorities返回:Collection<? extends GrantedAuthority>
//猜测"flatMapIterable(Authentication::getAuthorities)"这一句 得到当前用户拥有的"role"列表
//只不过这时,这些"role"被包装成了类似SimpleGrantedAuthority这样的东西
.flatMapIterable(Authentication::getAuthorities)
//".map(GrantedAuthority::getAuthority)"这句对每个SimpleGrantedAuthority进行拆包,获取到里面的"role"
.map(GrantedAuthority::getAuthority)
//从redis中查询访问当前路径"需要"用户是哪些"role"(这些role记为roles),只要当前用户具备的"role"中存在一项在这个roles里面
.any(authorities::contains)
//就返回鉴权成功
.map(AuthorizationDecision::new)
//否则鉴权失败
.defaultIfEmpty(new AuthorizationDecision(false));