于紫祥_1901
2020-12-24 f28149d8183a62f87fa9c8df9ae589070d83f612
src/main/java/com/moral/service/impl/AuthUserServiceImpl.java
@@ -1,38 +1,58 @@
package com.moral.service.impl;
import com.moral.entity.auth.AuthRole;
import com.moral.entity.auth.AuthUser;
import com.moral.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
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 java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import com.moral.common.util.RedisUtil;
import com.moral.entity.Account;
import com.moral.entity.OperateUser;
import com.moral.mapper.AccountMapper;
import com.moral.service.AccountService;
import com.moral.service.OperateUserService;
@Service
public class AuthUserServiceImpl implements UserDetailsService {
    @Autowired
    private UserService userService;
   @Resource
   private AccountMapper accountMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AuthUser user = userService.findByUsername(username);
   @Resource
   private AccountService accountService;
        if(user == null){
            throw new UsernameNotFoundException("用户名:"+ username + "不存在!");
        }
        Collection<SimpleGrantedAuthority> collection = new HashSet<SimpleGrantedAuthority>();
        Iterator<AuthRole> iterator =  user.getList().iterator();
        while (iterator.hasNext()){
            collection.add(new SimpleGrantedAuthority(iterator.next().getRole_name()));
        }
   @Resource
   private OperateUserService operateUserService;
        return new org.springframework.security.core.userdetails.User(username, user.getPassword(), collection);
    }
   @Resource
   private RedisTemplate<String, String> redisTemplate;
   @Override
   public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
      String type = RedisUtil.get(redisTemplate, "token_" + username), password;
      Collection<SimpleGrantedAuthority> collection = new HashSet<SimpleGrantedAuthority>();
      if ("screen".equals(type)) {
         Account account = accountService.getAccountByAccountName(username);
         password = account.getPassword();
         List<Map<String, Object>> roleNames = accountMapper.getRoleNameByAccountId(account.getId());
         for (Map<String, Object> roleName : roleNames) {
            collection.add(new SimpleGrantedAuthority((String) roleName.get("role_name")));
         }
      } else {
         OperateUser operateUser = operateUserService.getOperateUserByMobile(username);
         password = operateUser.getPassword();
         collection.add((new SimpleGrantedAuthority("ROLE_MOBILE")));
      }
      return new User(username, password, collection);
   }
}