Spring Boot 整合 Shiro 之后可以认证,不能授权的问题

Spring Boot 整合 Shiro 之后可以认证,不能授权的问题

问题不一定适合所有人,但可能会适合你。
直接说解决方案:
原因:网上copy代码,创建多Realm的自定义ShiroConfig类的时候代码中只给Authenticator注入Realms没有给Authorizer注入Realm

代码改成如下就可以了

package cn.com.springbootshiro.common.config.shiro;
import java.util.*;
import org.apache.shiro.authc.Authenticator;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.authz.Authorizer;
import org.apache.shiro.authz.ModularRealmAuthorizer;
import cn.com.springbootshiro.common.config.shiro.ream.UserRealm;
//省略部分无关代码
@Configuration
@Log4j
public class ShiroConfig {
@Bean("authorizer")
public Authorizer authorizer(Realm ...realms) {
ModularRealmAuthorizer authenticator = new ModularRealmAuthorizer();
Collection<Realm> crealms = new ArrayList<>(realms.length);
for(Realm oneRealm:realms){
crealms.add(oneRealm);
}
authenticator.setRealms(crealms );
return authenticator;
}
@Bean("authenticator")
public Authenticator authenticator(Realm ...realms) {
ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
Collection<Realm> crealms = new ArrayList<>(realms.length);
for(Realm oneRealm:realms){
crealms.add(oneRealm);
}
authenticator.setRealms(crealms );
return authenticator;
}
//省略部分无关代码
@Bean
public DefaultWebSecurityManager securityManager(Authenticator authenticator,Authorizer authorizer) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setAuthenticator(authenticator);// 认证 set realms
securityManager.setAuthorizer(authorizer);// 授权 set realms
securityManager.setCacheManager(cacheManager());
return securityManager;
}
}

解释:
之前不走授权流程是因为你的Authenticator有Realm信息但Authorizer中没有Realm信息,授权的时候,需要从Authorizer中获取Realm,得不到,怎么可能走你的授权流程。

注意:AuthorizerAuthenticator很相似,很多和他们有关的方法/类名都很相似,所以一定要注意。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值