⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hibernatedaoimpl.java

📁 Java开发的权限管理的例子
💻 JAVA
字号:
package org.artemis.manager.auth;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.userdetails.User;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.springframework.dao.DataAccessException;
import org.apache.log4j.Logger;
import org.artemis.manager.common.Constants;
import org.artemis.right.RightDelegateIF;
import org.artemis.right.model.Role;
import com.gsta.eshore.framework.auth.cache.AuthorityBasedUserCache;
import com.gsta.eshore.framework.dao.BaseDao;
import com.gsta.eshore.framework.exeception.ClientException;
import com.gsta.eshore.framework.jcf.ErrorCode;
import com.gsta.eshore.framework.jcf.IState;
import com.gsta.eshore.framework.jcf.StateImpl;
import com.gsta.eshore.framework.service.impl.ContextServiceLocator;

public class HibernateDaoImpl implements UserDetailsService {
	private static Logger logger = Logger.getLogger(HibernateDaoImpl.class);
	private String rolePrefix = "";
	private AuthorityBasedUserCache cache;
	private BaseDao baseDao;
	public String getRolePrefix() {
		return rolePrefix;
	}

	public void setRolePrefix(String rolePrefix) {
		this.rolePrefix = rolePrefix;
	}

	/**
	 * 从数据库中查找该用户User以及所拥有的role
	 */
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		IState state = new StateImpl();
		org.artemis.right.model.User misUser = getUserByName(
				username, state);
		if (!state.isOK()) {
			throw new ClientException("read users from database error!",ErrorCode.DB_OPERATOR_ERROR);
		}
		if (misUser == null)
			return null;
		GrantedAuthority[] arrayAuths = getAuthoritiesByUsernameQuery(misUser);
		if (arrayAuths.length == 0) {
			throw new UsernameNotFoundException("User has no GrantedAuthority");
		}
		
		logger.debug("username = " + username + " user.getPassword() = "+ misUser.getPassword());
		for (int i = 0; i < arrayAuths.length; i++) {
			logger.debug(username + " has role = "	+ arrayAuths[i].getAuthority());
		}
		
		return new User(username, misUser.getPassword() , getBoolean(misUser.getEnabled()), true,
				true, true, arrayAuths);
	}
	
	private boolean getBoolean(String b){
		return "1".endsWith(b)?true:false;
	}

	// 得到该用户的权限
	public GrantedAuthority[] getAuthoritiesByUsernameQuery(
			org.artemis.right.model.User misUser)
			throws DataAccessException {
		if (misUser != null) {
			GrantedAuthority[] grantedAuthoritys = cache
					.getAuthorityFromCache(misUser.getName());
			if (grantedAuthoritys == null) {
				List roles = misUser.getRoles();
				Iterator it = roles.iterator();
				List list = new ArrayList();
				while (it.hasNext()) {
					GrantedAuthorityImpl gai = new GrantedAuthorityImpl(
							((Role) it.next()).getName());
					list.add(gai);
				}
				grantedAuthoritys = (GrantedAuthority[]) list
						.toArray(new GrantedAuthority[0]);
				cache.putAuthorityInCache(misUser.getName(), grantedAuthoritys);
				return grantedAuthoritys;
			}
			return grantedAuthoritys;
		}
		return null;
	}

	private org.artemis.right.model.User getUserByName(String name, IState state) {
		return getBiz().findUserByNameFromDataBase(name, state);
	}

	public RightDelegateIF getBiz() {
		return ((RightDelegateIF) ContextServiceLocator.getInstance().getBean(Constants.BizDelegateWithOutMethodControl));
	}
/*
	private com.gsta.bnet.bizservice.right.model.User getUserByName(String name) {
		com.gsta.bnet.bizservice.right.model.User user = new com.gsta.bnet.bizservice.right.model.User();
		if (name.endsWith("root")) {
			user.setName("root");
			user.setPassword("root");
			user.setEnabled("1");
			logger.debug("Boolean.getBoolean(misUser.getEnabled()) = "
					+ Boolean.valueOf(user.getEnabled()).booleanValue());
			List role = new ArrayList();
			Role r = new Role();
			r.setEnabled("1");
			r.setName("ROLE_SUPERVISOR");
			r.setRoleType("ROLE_SUPERVISOR");
			role.add(r);
			user.setRoles(role);
		} else if (name.endsWith("user")) {
			user.setName("user");
			user.setPassword("user");
			user.setEnabled("1");
			logger.debug("Boolean.getBoolean(misUser.getEnable()) = "
					+ Boolean.valueOf(user.getEnabled()).booleanValue());
			List role = new ArrayList();
			Role r = new Role();
			r.setEnabled("1");
			r.setName("ROLE_USER");
			r.setRoleType("ROLE_USER");
			role.add(r);
			user.setRoles(role);
		} else {
			return null;
		}

		return user;
	}
*/
	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public AuthorityBasedUserCache getCache() {
		return cache;
	}

	public void setCache(AuthorityBasedUserCache cache) {
		this.cache = cache;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -