2022-10-10 15:57:14 +08:00
|
|
|
package com.rzyc.service;
|
2022-09-30 09:41:48 +08:00
|
|
|
|
2022-10-10 16:22:53 +08:00
|
|
|
|
|
|
|
|
import com.rzyc.config.UserDetailsAndId;
|
2022-10-08 17:33:31 +08:00
|
|
|
import com.rzyc.mapper.AuthorityKeyMapper;
|
2022-09-30 09:41:48 +08:00
|
|
|
import com.rzyc.mapper.ent.EntUserMapper;
|
2022-10-08 17:33:31 +08:00
|
|
|
import com.rzyc.model.AuthorityKey;
|
2022-09-30 09:41:48 +08:00
|
|
|
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;
|
2022-09-30 16:23:29 +08:00
|
|
|
import java.util.Collection;
|
2022-09-30 09:41:48 +08:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* spring security 用户认证过程
|
|
|
|
|
* @author Xuwanxin
|
|
|
|
|
* @author 2022/09/27
|
|
|
|
|
* */
|
|
|
|
|
|
|
|
|
|
@Service("userService")
|
|
|
|
|
public class UserDetailsServiceImpl implements UserDetailsService {
|
|
|
|
|
|
2022-10-08 17:33:31 +08:00
|
|
|
|
|
|
|
|
|
2022-09-30 09:41:48 +08:00
|
|
|
/**
|
|
|
|
|
* 内存过程密码加密
|
|
|
|
|
* */
|
|
|
|
|
private PasswordEncoder passwordEncoder;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 企业端用户
|
|
|
|
|
* */
|
|
|
|
|
private EntUserMapper entUserMapper;
|
|
|
|
|
|
2022-10-08 17:33:31 +08:00
|
|
|
private AuthorityKeyMapper authorityKeyMapper;
|
2022-09-30 16:23:29 +08:00
|
|
|
|
2022-09-30 09:41:48 +08:00
|
|
|
@Autowired
|
2022-10-08 17:33:31 +08:00
|
|
|
public void UserDetailsServiceImplFinder(PasswordEncoder passwordEncoder,EntUserMapper entUserMapper,AuthorityKeyMapper authorityKeyMapper) {
|
2022-09-30 09:41:48 +08:00
|
|
|
this.passwordEncoder = passwordEncoder;
|
|
|
|
|
this.entUserMapper = entUserMapper;
|
2022-10-08 17:33:31 +08:00
|
|
|
this.authorityKeyMapper = authorityKeyMapper;
|
2022-09-30 09:41:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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>();
|
2022-10-08 17:33:31 +08:00
|
|
|
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());
|
2022-09-30 09:41:48 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|