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

com.cjoop.security.service.UserDetailsServiceImpl Maven / Gradle / Ivy

The newest version!
package com.cjoop.security.service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cjoop.commons.MessageUtils;
import com.cjoop.commons.ValidatorUtils;
import com.cjoop.security.constants.UserDetailsConstants;
import com.cjoop.system.domain.Account;
import com.cjoop.system.domain.AccountRole;
import com.cjoop.system.domain.QAccount;
import com.cjoop.system.domain.QAccountRole;
import com.cjoop.system.domain.QMenu;
import com.cjoop.system.domain.QRolePerm;
import com.cjoop.system.domain.QUser;
import com.cjoop.system.domain.User;
import com.cjoop.system.repository.AccountRepository;
import com.cjoop.system.repository.AccountRoleRepository;
import com.cjoop.system.repository.MenuRepository;
import com.cjoop.system.repository.PermRepository;
import com.cjoop.system.repository.RolePermRepository;
import com.cjoop.system.repository.UserRepository;

/**
 * 认证用户查询接口实现
 * @author 陈均
 *
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
	private String rolePrefix = "ROLE_";
	private static QUser quser = QUser.user;
	private static QAccount qaccount = QAccount.account;
	QAccountRole qAccountRole = QAccountRole.accountRole;
	QRolePerm qRolePerm = QRolePerm.rolePerm;
	QMenu qMenu = QMenu.menu;
	
	private static String USER_INPUT_ERROR = MessageUtils.getMessage(UserDetailsConstants.USER_INPUT_ERROR_CODE);

	@Autowired
	private AccountRepository accountRepository;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private AccountRoleRepository accountRoleRepository;
	@Autowired
	private RolePermRepository rolePermRepository;
	@Autowired
	private PermRepository permRepository;
	@Autowired
	private MenuRepository menuRepository;
	
	
	@Transactional(readOnly=true)
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = null;
		Account account = null;
		if(ValidatorUtils.isTelephone(username)){
			user = userRepository.findOne(quser.telephone.eq(username));
		}else if(ValidatorUtils.isEmail(username)){
			user = userRepository.findOne(quser.email.eq(username));
		}else{
			account = accountRepository.findOne(qaccount.username.eq(username));
			if(account==null){
				throw new UsernameNotFoundException(USER_INPUT_ERROR);
			}
		}
		if(account==null && user==null){
			throw new UsernameNotFoundException(USER_INPUT_ERROR);
		}
		if(user!=null){
			account = accountRepository.findOne(user.getId());
			if(account==null){
				throw new UsernameNotFoundException(USER_INPUT_ERROR);
			}
		}
		
		Date expiry = account.getExpiryDate();
		if(expiry!=null && account.getAccountNonExpired()){
			if(expiry.compareTo(new Date())==-1){
				account.setAccountNonExpired(false);//账号过期
			}
		}
		if(account.getDelStatus()) {
			account.setAccountNonExpired(false);//账号过期
		}
		Set authorities = new HashSet();
		if(account.getSuperAdmin() == true){
			permRepository.findAll().forEach(perm->{
				authorities.add(new SimpleGrantedAuthority(rolePrefix + perm.getId()));
			});
			menuRepository.findAll(qMenu.permCode.isNotNull()).forEach(menu->{
				authorities.add(new SimpleGrantedAuthority(rolePrefix + menu.getPermCode()));
			});
		}else{
			Iterable iterable = accountRoleRepository.findAll(qAccountRole.account.id.eq(account.getId()));
			List roleList = StreamSupport.stream(iterable.spliterator(), false).map(accountRole->{
				return accountRole.getRole().getId();
			}).collect(Collectors.toList());
			rolePermRepository.findAll(qRolePerm.role.id.in(roleList)).forEach(rolePerm->{
				authorities.add(new SimpleGrantedAuthority(rolePrefix + rolePerm.getPermCode()));
			});
		}
		return new org.springframework.security.core.userdetails.User(
			account.getUsername(), account.getPassword(), account.getEnabled(),
			account.getAccountNonExpired(), account.getCredentialsNonExpired(),
			account.getAccountNonLocked(), authorities);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy