You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
healthManageSystem/api/src/main/java/com/intelligentHealthCare/service/impl/AccountHandle.java

78 lines
3.3 KiB

package com.intelligentHealthCare.service.impl;
import com.intelligentHealthCare.entity.Account;
import com.intelligentHealthCare.exception.BizException;
import com.intelligentHealthCare.exception.LoginFailException;
import com.intelligentHealthCare.remote.AccountServiceRemote;
import com.intelligentHealthCare.security.SecurityUser;
import com.intelligentHealthCare.utils.JwtUtil;
import com.intelligentHealthCare.utils.RedisCache;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__({@Autowired}))
public class AccountHandle {
@Autowired
private final AuthenticationManager authenticationManager;
@Autowired
private final RedisCache redisCache;
@Autowired
private final AccountServiceRemote accountServiceRemote;
public String login(Account account, String code, String userIp) {
Object cacheCode = redisCache.getCacheObject(userIp);
if(cacheCode == null){
throw new BizException("验证码已过期");
} else if (!cacheCode.toString().equalsIgnoreCase(code)) {
throw new BizException("验证码有误");
}
redisCache.deleteObject(userIp);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(account.getUsername(), account.getPassword());
Authentication authenticate;
try {
authenticate = authenticationManager.authenticate(authenticationToken);
}catch (DisabledException disabledException){
throw new LoginFailException("账号已被停用");
}catch (BadCredentialsException e){
throw new LoginFailException("密码错误");
}catch (AuthenticationException e){
log.error("认证错误", e);
throw new LoginFailException("账号不存在");
}
SecurityUser loginAccount = (SecurityUser) authenticate.getPrincipal();
String jwt = JwtUtil.createJWT(loginAccount.getAccount().getId());
redisCache.setCacheObject(loginAccount.getAccount().getId(), loginAccount, 7, TimeUnit.DAYS);
return jwt;
}
public Boolean logout() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
SecurityUser loginAccount = (SecurityUser) authentication.getPrincipal();
return redisCache.deleteObject(loginAccount.getAccount().getId());
}
public Account register(Account account) {
account.setPassword(new BCryptPasswordEncoder().encode(account.getPassword()));
return accountServiceRemote.register(account);
}
}