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

📄 acegiuserdeitailsservice.java

📁 Spring+acegi+ext2.0+mysql 开发 acegi做权限控制 ext2.0做前台显示 spring 做控制层 mysql 做数据库
💻 JAVA
字号:
package com.mypro.security;

import java.util.List;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;

import com.mypro.hibernate.bean.Userrole;
import com.mypro.hibernate.dao.UserDAO;
import com.mypro.hibernate.dao.UserroleDAO;

public class AcegiUserDeitailsService implements UserDetailsService {
	private final Log LOG = LogFactory.getLog(AcegiUserDeitailsService.class);
	/* 依赖注入 */
	private UserDAO userDao;
	private UserroleDAO userRoleDao;

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}

	public void setUserRoleDao(UserroleDAO userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

	/* 用户所有的权限 */
	//private final List<GrantedAuthority> grantedAuthList = new ArrayList<GrantedAuthority>(6);
	private GrantedAuthority[] grantedAuthArray;

	public UserDetails loadUserByUsername(String userName)
			throws UsernameNotFoundException, DataAccessException {
		if (LOG.isDebugEnabled()) {
			LOG.debug("Loading UserDetails of userName: " + userName);
		}
		/* 取得用户 */
		com.mypro.hibernate.bean.User user = (com.mypro.hibernate.bean.User)userDao.findByUserName(userName).get(0);
		if (user == null) {
			LOG
					.warn("UserDetails load failed: No such UserRole with userName: "
							+ userName);
			throw new UsernameNotFoundException("User name is not found.");
		}
		/* 取得所有用户权限 */
		List<Userrole> userRoleList = userRoleDao
				.findByProperty("user_name", userName);
		if (userRoleList == null || userRoleList.size() == 0) {
			LOG.warn("UserRole load failed: No such UserRole with userName: "
					+ userName);
			throw new UsernameNotFoundException("UserRole is not found.");
		}
		/* 取得用户的所有角色 */
		int size = userRoleList.size();
		grantedAuthArray = new GrantedAuthority[size];
		int j = 0;
		for (int i = 0; i < size; i++) {
			Userrole userRole = userRoleList.get(i);
			if (userRole != null) {
				this.grantedAuthArray[j++] = new GrantedAuthorityImpl(userRole
						.getRoleName().toUpperCase());
			}
		}
		LOG.info("UserName: " + userName + " loaded successfully.");
		return new org.acegisecurity.userdetails.User(userName, user
				.getUserPass(), true, true, true, true, this.grantedAuthArray);
	}
}

⌨️ 快捷键说明

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