2022-09-30 09:41:48 +08:00
|
|
|
|
package com.rzyc.service;
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-10-12 17:32:06 +08:00
|
|
|
|
import com.common.utils.encryption.MD5;
|
2022-09-30 09:41:48 +08:00
|
|
|
|
import com.common.utils.jwt.JwtUtil;
|
2022-10-10 16:22:53 +08:00
|
|
|
|
|
2022-10-11 15:21:09 +08:00
|
|
|
|
import com.rzyc.advice.CustomException;
|
2022-10-10 16:22:53 +08:00
|
|
|
|
import com.rzyc.config.UserDetailsAndId;
|
2022-10-11 15:21:09 +08:00
|
|
|
|
import com.rzyc.enums.SysEnterpriseState;
|
2022-11-07 17:47:41 +08:00
|
|
|
|
import com.rzyc.mapper.ent.EntPostMapper;
|
2022-10-11 15:21:09 +08:00
|
|
|
|
import com.rzyc.mapper.ent.SysEnterpriseMapper;
|
2022-11-07 17:47:41 +08:00
|
|
|
|
import com.rzyc.model.ent.EntPost;
|
|
|
|
|
|
import com.rzyc.model.ent.EntUser;
|
2022-10-11 15:21:09 +08:00
|
|
|
|
import com.rzyc.model.ent.SysEnterprise;
|
2022-09-30 09:41:48 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
|
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
|
|
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.Objects;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 用户登陆 Service
|
|
|
|
|
|
* @author Xuwanxin
|
|
|
|
|
|
* @date 2022/9/26
|
|
|
|
|
|
* */
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
|
public class UserLoginService {
|
|
|
|
|
|
|
2022-10-11 15:21:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* spring security 登陆业务接口service
|
|
|
|
|
|
* */
|
2022-09-30 09:41:48 +08:00
|
|
|
|
private UserDetailsService userDetailsService;
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-10-11 15:21:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* spring security 内存中加密
|
|
|
|
|
|
* */
|
2022-09-30 09:41:48 +08:00
|
|
|
|
private PasswordEncoder passwordEncoder;
|
|
|
|
|
|
|
2022-10-11 15:21:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 企业端公司
|
|
|
|
|
|
* */
|
|
|
|
|
|
private SysEnterpriseMapper sysEnterpriseMapper;
|
|
|
|
|
|
|
2022-11-07 17:47:41 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 企业端岗位
|
|
|
|
|
|
* */
|
|
|
|
|
|
private EntPostMapper entPostMapper;
|
|
|
|
|
|
|
2022-10-11 15:21:09 +08:00
|
|
|
|
|
2022-09-30 09:41:48 +08:00
|
|
|
|
@Autowired
|
2022-11-07 17:47:41 +08:00
|
|
|
|
public void UserLoginServiceFinder(UserDetailsService userDetailsService,PasswordEncoder passwordEncoder,SysEnterpriseMapper sysEnterpriseMapper,EntPostMapper entPostMapper){
|
2022-09-30 09:41:48 +08:00
|
|
|
|
this.userDetailsService = userDetailsService;
|
|
|
|
|
|
this.passwordEncoder = passwordEncoder;
|
2022-10-11 15:21:09 +08:00
|
|
|
|
this.sysEnterpriseMapper= sysEnterpriseMapper;
|
2022-11-07 17:47:41 +08:00
|
|
|
|
this.entPostMapper = entPostMapper;
|
2022-09-30 09:41:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-07 17:47:41 +08:00
|
|
|
|
public EntUser login(String username, String password)throws Exception {
|
|
|
|
|
|
EntUser entUser = new EntUser();
|
|
|
|
|
|
String token = null;
|
2022-09-30 09:41:48 +08:00
|
|
|
|
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
|
|
|
|
|
|
if (Objects.isNull(userDetails)) {
|
2022-12-21 18:17:12 +08:00
|
|
|
|
throw new CustomException("账号不存在,请填入企业名或企业绑定的手机号");
|
2022-09-30 09:41:48 +08:00
|
|
|
|
}
|
2022-11-08 18:09:46 +08:00
|
|
|
|
SysEnterprise sysEnterprise = sysEnterpriseMapper.findEnterpriseByPhoneNumber(username);
|
2022-10-11 15:21:09 +08:00
|
|
|
|
if (Objects.isNull(sysEnterprise) || sysEnterprise.getState().equals(SysEnterpriseState.DISABLE)){
|
|
|
|
|
|
throw new CustomException("企业不存在或已经禁用");
|
|
|
|
|
|
}
|
2022-09-30 09:41:48 +08:00
|
|
|
|
//这里可能会不对,因为我们是MD5,这个是spring security 中的 encoder加密
|
2022-10-12 17:32:06 +08:00
|
|
|
|
if (!passwordEncoder.matches(MD5.md5(password), userDetails.getPassword())) {
|
2022-11-07 17:47:41 +08:00
|
|
|
|
throw new CustomException("密码不正确");
|
2022-09-30 09:41:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
//spring security context insert
|
|
|
|
|
|
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
|
|
|
|
|
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
|
|
|
//企业用户id
|
2022-10-10 16:22:53 +08:00
|
|
|
|
String id = ((UserDetailsAndId)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
|
2022-09-30 09:41:48 +08:00
|
|
|
|
token = JwtUtil.createToken(id);
|
2022-11-07 17:47:41 +08:00
|
|
|
|
String postId = ((UserDetailsAndId)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPostId();
|
|
|
|
|
|
EntPost entPost = entPostMapper.selectById(postId);
|
|
|
|
|
|
//封装返回数据
|
|
|
|
|
|
entUser.setName(userDetails.getUsername());
|
|
|
|
|
|
entUser.setEntUserId(id);
|
|
|
|
|
|
entUser.setEnterpriseId(sysEnterprise.getSysenterpriseid());
|
|
|
|
|
|
entUser.setToken(token);
|
|
|
|
|
|
entUser.setEntPostName(entPost.getName());
|
2023-03-20 09:25:09 +08:00
|
|
|
|
entUser.setPostId(entPost.getPostId());
|
2022-11-14 17:51:04 +08:00
|
|
|
|
entUser.setUserTypeName("企业用户");
|
|
|
|
|
|
entUser.setEnterpriseName(sysEnterprise.getEntname());
|
2022-11-07 17:47:41 +08:00
|
|
|
|
return entUser;
|
2022-09-30 09:41:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|