ganzi-api/inventory-ent/src/main/java/com/rzyc/service/UserDetailsServiceImpl.java

73 lines
2.5 KiB
Java
Raw Normal View History

package com.rzyc.service;
2022-10-10 16:22:53 +08:00
import com.rzyc.config.UserDetailsAndId;
import com.rzyc.mapper.AuthorityKeyMapper;
import com.rzyc.mapper.ent.EntUserMapper;
import com.rzyc.model.AuthorityKey;
import com.rzyc.model.ent.EntUser;
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;
private AuthorityKeyMapper authorityKeyMapper;
@Autowired
public void UserDetailsServiceImplFinder(PasswordEncoder passwordEncoder,EntUserMapper entUserMapper,AuthorityKeyMapper authorityKeyMapper) {
this.passwordEncoder = passwordEncoder;
this.entUserMapper = entUserMapper;
this.authorityKeyMapper = authorityKeyMapper;
}
@Override
public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
//判断数据库用户
EntUser entUser = entUserMapper.selectByName(name);
if (Objects.isNull(entUser)){
throw new UsernameNotFoundException("用户名或密码错误");
}
List<GrantedAuthority> authority= new ArrayList<GrantedAuthority>();
List<AuthorityKey>authorizations = authorityKeyMapper.allAuthorizations();
StringBuilder stringBuilder = new StringBuilder();
for (AuthorityKey s:authorizations) {
stringBuilder.append(s.getCategory() +":"+s.getAuthKey());
authority.add(new SimpleGrantedAuthority(stringBuilder.toString()));
}
2022-10-10 16:22:53 +08:00
return new UserDetailsAndId(entUser.getName(), passwordEncoder.encode(entUser.getPasswd()), authority,entUser.getEntUserId());
}
}