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;
|
|
|
|
|
|
import com.rzyc.mapper.ent.SysEnterpriseMapper;
|
|
|
|
|
|
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-09-30 09:41:48 +08:00
|
|
|
|
@Autowired
|
2022-10-11 15:21:09 +08:00
|
|
|
|
public void UserLoginServiceFinder(UserDetailsService userDetailsService,PasswordEncoder passwordEncoder,SysEnterpriseMapper sysEnterpriseMapper){
|
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-09-30 09:41:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-11 15:21:09 +08:00
|
|
|
|
public String login(String username, String password)throws Exception {
|
2022-09-30 09:41:48 +08:00
|
|
|
|
String token = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
|
|
|
|
|
|
if (Objects.isNull(userDetails)) {
|
|
|
|
|
|
throw new UsernameNotFoundException("账号不存在");
|
|
|
|
|
|
}
|
2022-10-11 15:21:09 +08:00
|
|
|
|
SysEnterprise sysEnterprise = sysEnterpriseMapper.findEnterpriseByName(username);
|
|
|
|
|
|
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-09-30 09:41:48 +08:00
|
|
|
|
throw new BadCredentialsException("密码不正确");
|
|
|
|
|
|
}
|
|
|
|
|
|
//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);
|
|
|
|
|
|
} catch (AuthenticationException e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
return token;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|