92 lines
2.9 KiB
Java
92 lines
2.9 KiB
Java
package com.rzyc.service;
|
|
|
|
|
|
import com.rzyc.advice.CustomException;
|
|
import com.rzyc.config.RedisUtil;
|
|
import com.rzyc.config.UserDetailsAndId;
|
|
import com.rzyc.enums.SysEnterpriseState;
|
|
import com.rzyc.mapper.AuthorityKeyMapper;
|
|
import com.rzyc.mapper.ent.EntUserMapper;
|
|
import com.rzyc.mapper.ent.SysEnterpriseMapper;
|
|
import com.rzyc.model.AuthorityKey;
|
|
import com.rzyc.model.ent.EntUser;
|
|
import com.rzyc.model.ent.SysEnterprise;
|
|
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.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.stereotype.Service;
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* spring security 用户认证过程
|
|
* @author Xuwanxin
|
|
* @author 2022/09/27
|
|
* */
|
|
|
|
@Service("userService")
|
|
public class UserDetailsServiceImpl implements UserDetailsService {
|
|
|
|
|
|
|
|
/**
|
|
* 内存过程密码加密
|
|
* */
|
|
private PasswordEncoder passwordEncoder;
|
|
|
|
/**
|
|
* 企业端用户
|
|
* */
|
|
private EntUserMapper entUserMapper;
|
|
|
|
/**
|
|
* 权限表mapper
|
|
* */
|
|
private AuthorityKeyMapper authorityKeyMapper;
|
|
|
|
|
|
/**
|
|
* redis操作工具
|
|
* */
|
|
private RedisUtil redisUtil;
|
|
|
|
|
|
|
|
@Autowired
|
|
public void UserDetailsServiceImplFinder(PasswordEncoder passwordEncoder,EntUserMapper entUserMapper,AuthorityKeyMapper authorityKeyMapper,RedisUtil redisUtil) {
|
|
this.passwordEncoder = passwordEncoder;
|
|
this.entUserMapper = entUserMapper;
|
|
this.authorityKeyMapper = authorityKeyMapper;
|
|
this.redisUtil = redisUtil;
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
public UserDetails loadUserByUsername(String mobileNumber){
|
|
//判断用户是否存在
|
|
EntUser user = entUserMapper.validAccount(mobileNumber,null);
|
|
if (Objects.isNull(user)){
|
|
throw new CustomException("用户名不存在");
|
|
}
|
|
List<GrantedAuthority> authority= new ArrayList<GrantedAuthority>();
|
|
List<AuthorityKey>authorizations = authorityKeyMapper.allAuthorizations();
|
|
redisUtil.set("allKeys",authorizations);
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
for (AuthorityKey s:authorizations) {
|
|
stringBuilder.append(s.getAuthKey());
|
|
authority.add(new SimpleGrantedAuthority(stringBuilder.toString()));
|
|
stringBuilder.setLength(0);
|
|
}
|
|
return new UserDetailsAndId(user.getName(), passwordEncoder.encode(user.getPasswd()), authority,user.getEntUserId(),user.getPostId());
|
|
|
|
}
|
|
}
|