package com.rzyc.service; import com.common.utils.encryption.MD5; import com.common.utils.jwt.JwtUtil; import com.rzyc.advice.CustomException; import com.rzyc.config.UserDetailsAndId; import com.rzyc.enums.SysEnterpriseState; import com.rzyc.mapper.ent.EntPostMapper; import com.rzyc.mapper.ent.SysEnterpriseMapper; import com.rzyc.model.ent.EntPost; import com.rzyc.model.ent.EntUser; import com.rzyc.model.ent.SysEnterprise; 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 { /** * spring security 登陆业务接口service * */ private UserDetailsService userDetailsService; /** * spring security 内存中加密 * */ private PasswordEncoder passwordEncoder; /** * 企业端公司 * */ private SysEnterpriseMapper sysEnterpriseMapper; /** * 企业端岗位 * */ private EntPostMapper entPostMapper; @Autowired public void UserLoginServiceFinder(UserDetailsService userDetailsService,PasswordEncoder passwordEncoder,SysEnterpriseMapper sysEnterpriseMapper,EntPostMapper entPostMapper){ this.userDetailsService = userDetailsService; this.passwordEncoder = passwordEncoder; this.sysEnterpriseMapper= sysEnterpriseMapper; this.entPostMapper = entPostMapper; } public EntUser login(String username, String password)throws Exception { EntUser entUser = new EntUser(); String token = null; UserDetails userDetails = userDetailsService.loadUserByUsername(username); if (Objects.isNull(userDetails)) { throw new CustomException("账号不存在,请填入企业名或企业绑定的手机号"); } SysEnterprise sysEnterprise = sysEnterpriseMapper.findEnterpriseByPhoneNumber(username); if (Objects.isNull(sysEnterprise) || sysEnterprise.getState().equals(SysEnterpriseState.DISABLE)){ throw new CustomException("企业不存在或已经禁用"); } //这里可能会不对,因为我们是MD5,这个是spring security 中的 encoder加密 if (!passwordEncoder.matches(MD5.md5(password), userDetails.getPassword())) { throw new CustomException("密码不正确"); } //spring security context insert UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); SecurityContextHolder.getContext().setAuthentication(authentication); //企业用户id String id = ((UserDetailsAndId)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId(); token = JwtUtil.createToken(id); 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.setUserToken(token); entUser.setEntPostName(entPost.getName()); entUser.setPostId(entPost.getPostId()); entUser.setUserTypeName("企业用户"); entUser.setEnterpriseName(sysEnterprise.getEntname()); return entUser; } }