All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.infop.security.MyCustomRealm Maven / Gradle / Ivy

package cn.infop.security;

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

import org.apache.log4j.Logger;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import cn.infop.dao.UserDao;
import cn.infop.entity.User;

public class MyCustomRealm extends JdbcRealm {
	
	private static final Logger log = Logger.getLogger(MyCustomRealm.class);

	public MyCustomRealm() {
	}

	/**
	 * Processing login requests
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		UsernamePasswordToken upToken = (UsernamePasswordToken) token;
		String username = upToken.getUsername();
		
		log.debug(username);

		if (username == null) {
			throw new UnknownAccountException("The user name does not exist.");
		}

		User user = new UserDao().findByUsername(username);

		if (Boolean.TRUE.equals(user.isLocked())) {
			throw new LockedAccountException(); // 帐号锁定
		}

		SimpleAuthenticationInfo info = null;
		info = new SimpleAuthenticationInfo(username, user.getPassword().toCharArray(), getName());
		info.setCredentialsSalt(ByteSource.Util.bytes(user.getPassword_salt()));
		return info;
	}

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		if (principals == null) {
			throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
		}

		String username = (String) getAvailablePrincipal(principals);
		Set roleNames = null;
		Set permissions = new HashSet<>();
		UserDao dao = new UserDao();
		roleNames = dao.findRoleNamesByUsername(username);

		for (String role : roleNames) {
			permissions.addAll(dao.findPermissionByRolename(role));
		}

		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
		info.setStringPermissions(permissions);
		return info;
	}

	@Override
	public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
		super.clearCachedAuthorizationInfo(principals);
	}

	@Override
	public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
		super.clearCachedAuthenticationInfo(principals);
	}

	@Override
	public void clearCache(PrincipalCollection principals) {
		super.clearCache(principals);
	}

	public void clearAllCachedAuthorizationInfo() {
		getAuthorizationCache().clear();
	}

	public void clearAllCachedAuthenticationInfo() {
		getAuthenticationCache().clear();
	}

	public void clearAllCache() {
		clearAllCachedAuthenticationInfo();
		clearAllCachedAuthorizationInfo();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy