undefinedfix
Sign in

When authorizationinfo is authorized in Shiro in springboot, it will be executed repeatedly. Why

link1xbetonline6 edited in Mon, 27 Jun 2022

The authorization code is as follows

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    log.info("正在对请求用户进行授权");
    String token = (String) principals.getPrimaryPrincipal();
    String openID = JwtUtil.getopenID(token);
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    //获取相应的角色和权限,详情见getRoleAndPermission()
    RolePermission rolePermission = userService.getRoleAndPermission(openID);
    //添加角色
    log.info("【" + openID + "】" + "_添加角色_" + rolePermission.getRole());
    info.addRole(rolePermission.getRole());
    //添加权限
    try {
        JSONArray permissionArray = new JSONArray(rolePermission.getPermission());
        for (int i = 0; i < permissionArray.length(); i++) {
            log.info("正在添加权限:【"+permissionArray.getString(i)+"】");
            info.addStringPermission(permissionArray.getString(i));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return info;
}

The console output is as follows

clipboard.png

I really don't understand why it's like this. I don't see any problems in debugging (I don't know how to use 2333). I don't know if anyone I've ever met knows what's going on, or maybe what's going on. Thank you

4 Replies
AfajarS
commented on Mon, 27 Jun 2022

I've been looking for it online for a long time. Under abnormal circumstances, this is the problem. After commenting the following code, it will only be output twice. As for why it will still be output twice, please continue to see

    /**
     * 开启后会重复执行doGetAuthorizationInfo
     */
//    @Bean
//    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
//        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
//        // 强制使用cglib,防止重复代理和可能引起代理出错的问题
//        // https://zhuanlan.zhihu.com/p/29161098
//        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
//        return defaultAdvisorAutoProxyCreator;
//    }

The reason why the above comment will be output twice is that (there is something wrong with my writing), I changed it to this one later

 /** 授权,登录成功之后,进行角色的权限验证
     *  角色授权和对应权限添加
     *  只有当需要检测用户权限的时候采用调用此方法,例如checkRole,checkPermission之类的
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String token = (String) principals.getPrimaryPrincipal();
        String openID = JwtUtil.getopenID(token);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        RolePermission rolePermission = userService.getRoleAndPermission(openID);
        //添加角色
        log.info("【" + openID + "】" + "_添加角色_" + rolePermission.getRole());
        info.addRole(rolePermission.getRole());
        //添加权限
        try {
            JSONArray permissionArray = new JSONArray(rolePermission.getPermission());
            for (int i = 0; i < permissionArray.length(); i++) {
                log.info("正在添加权限:【"+permissionArray.getString(i)+"】");
                info.addStringPermission(permissionArray.getString(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

But it seems that it will be triggered twice. This time, it's not a code problem. Let's look at the annotation first

@RequiresRoles(value = {"root","admin"},logical = Logical.OR)

For example, I allow a role with the identity of root or admin to be operated, so the program will be executed twice, root and admin respectively. The second one will not be executed because the first one matches, so the information is output twice. If there are three roles to judge, the information will be output three times. The above is my conclusion

sincerelyTyler
commented on Tue, 28 Jun 2022

I've just used Shiro recently, and I'm not very familiar with it. I guess it's because the URL you Shiro intercepted has been requested many times, so there will be repeated execution

Maj
commented on Tue, 28 Jun 2022

So is mine ..... Do not know why?

kcomf
commented on Tue, 28 Jun 2022

After commenting out the defaultadvisor autoproxycreator, Shiro's annotation will not be used, and a 404 error will be reported

lock This question has been locked and the reply function has been disabled.