cn.itganhuo.app.web.shiro.ShiroDbRealm.java Source code

Java tutorial

Introduction

Here is the source code for cn.itganhuo.app.web.shiro.ShiroDbRealm.java

Source

/*
 * Copyright 2014-2024 the https://github.com/xiaoxing598/itganhuo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * This project consists of JAVA private school online learning community group Friends co-creator [QQ group 329232140].
 * JAVA???[QQ329232140];
 * See the list of IT dry technology sharing network [http://www.itganhuo.cn/teams].
 * ????IT[http://www.itganhuo.cn/teams];
 * The author does not guarantee the quality of the project and its stability, reliability, and security does not bear any responsibility.
 * ????????.
 */
package cn.itganhuo.app.web.shiro;

import cn.itganhuo.app.common.utils.StringUtil;
import cn.itganhuo.app.entity.User;
import cn.itganhuo.app.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Set;

/**
 * <h2>????</h2>
 * <dl>
 * <dt>??</dt>
 * <dd>???</dd>
 * <dt></dt>
 * <dd>???this.clearUserCache?</dd>
 * </dl>
 *
 * @author -?
 * @version 0.0.1-SNAPSHOT
 */
public class ShiroDbRealm extends AuthorizingRealm {

    private static final Logger log = LogManager.getLogger(ShiroDbRealm.class.getName());

    private UserService userService = null;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    /**
     * ???????
     *
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        log.debug("Start reading user permissions.");

        String account = (String) getAvailablePrincipal(principals);
        User user = userService.loadByAccount(account);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Set<String> role = new HashSet<String>();
        Set<String> stringPermissions = new HashSet<String>();
        // TODO ?????
        if (1 == user.getType()) {
            role.add("user");
            stringPermissions.add("user:*");
        } else if (999 == user.getType()) {
            role.add("admin");
            stringPermissions.add("admin:*");
        }
        info.setRoles(role);
        info.setStringPermissions(stringPermissions);
        return info;
    }

    /**
     * ????
     *
     * @version 0.0.1-SNAPSHOT
     * @author -?
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        log.debug("Began to validate user credentials.");

        UsernamePasswordToken uptoken = (UsernamePasswordToken) token;
        User user = userService.loadByAccount(uptoken.getUsername());
        if (user != null && StringUtil.hasText(user.getAccount())) {
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getAccount(), user.getPassword(),
                    getName());
            info.setCredentialsSalt(ByteSource.Util.bytes(uptoken.getUsername() + user.getSalt()));
            return info;
        } else {
            return null;
        }
    }

    /**
     * ?
     * <ol>
     * <li>???shiro?</li>
     * <li>?????????</li>
     * </ol>
     *
     * @param user_id ?
     * @version 0.0.2-SNAPSHOT
     * @author -?
     */
    public void clearUserCache(String user_id) {
        log.debug("Began to clear the user cache.");

        SimplePrincipalCollection spc = new SimplePrincipalCollection();
        spc.add(user_id, getName());
        super.clearCachedAuthorizationInfo(spc);
    }

}