package com.stylefeng.guns.modular.system.service.impl;
|
|
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
import com.stylefeng.guns.core.common.constant.JwtConstants;
|
import com.stylefeng.guns.core.shiro.ShiroKit;
|
import com.stylefeng.guns.core.shiro.ShiroUser;
|
import com.stylefeng.guns.core.util.JwtTokenUtil;
|
import com.stylefeng.guns.core.util.ToolUtil;
|
import com.stylefeng.guns.modular.system.dao.DispatchMapper;
|
import com.stylefeng.guns.modular.system.dao.VersionManagementMapper;
|
import com.stylefeng.guns.modular.system.model.Dispatch;
|
import com.stylefeng.guns.modular.system.service.IDispatchService;
|
import com.stylefeng.guns.modular.system.util.RedisUtil;
|
import com.stylefeng.guns.modular.system.util.ResultUtil;
|
import com.stylefeng.guns.modular.system.util.UUIDUtil;
|
import com.stylefeng.guns.modular.system.warpper.LoginWarpper;
|
import org.apache.shiro.authc.SimpleAuthenticationInfo;
|
import org.apache.shiro.authc.UsernamePasswordToken;
|
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
|
import org.apache.shiro.crypto.hash.Md5Hash;
|
import org.apache.shiro.util.ByteSource;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import java.util.Map;
|
|
|
@Service
|
public class DispatchServiceImpl extends ServiceImpl<DispatchMapper, Dispatch> implements IDispatchService {
|
|
@Resource
|
private DispatchMapper dispatchMapper;
|
|
@Autowired
|
private RedisUtil redisUtil;
|
|
@Resource
|
private VersionManagementMapper versionManagementMapper;
|
|
private final String SALT = "WL:x9#";
|
|
|
/**
|
* 账号密码登录
|
* @param account
|
* @param password
|
* @return
|
* @throws Exception
|
*/
|
@Override
|
public ResultUtil dispatchLogin(String account, String password) throws Exception {
|
Dispatch dispatch = this.queryByAccount(account);
|
if(null == dispatch){
|
return ResultUtil.error("账号无效");
|
}
|
if(dispatch.getState() == 2){
|
return ResultUtil.error("账号被冻结");
|
}
|
if(!ShiroKit.md5(password, SALT).equals(dispatch.getPassword())){
|
return ResultUtil.error("密码错误");
|
}
|
|
//调用单点登录的逻辑
|
this.singlePointLogin(dispatch.getId());
|
|
String token = this.getToken(dispatch, password);
|
LoginWarpper loginWarpper = new LoginWarpper();
|
loginWarpper.setId(dispatch.getId());
|
loginWarpper.setToken(token);
|
loginWarpper.setAppid(UUIDUtil.getRandomCode());
|
loginWarpper.setPushOrder(dispatch.getPushOrder());
|
return ResultUtil.success(loginWarpper);
|
}
|
|
|
/**
|
* 从redis中获取用户id
|
* @param request
|
* @return
|
* @throws Exception
|
*/
|
@Override
|
public Integer getUserIdFormRedis(HttpServletRequest request) throws Exception {
|
String requestHeader = request.getHeader(JwtConstants.AUTH_HEADER);
|
if (requestHeader != null && requestHeader.startsWith("Bearer ")) {
|
requestHeader = requestHeader.substring(requestHeader.indexOf(" ") + 1);
|
String key = null;
|
int length = requestHeader.length();
|
if(length > 32){
|
key = requestHeader.substring(length - 32);
|
}else{
|
key = requestHeader;
|
}
|
String value = redisUtil.getValue(key);
|
return null != value ? Integer.valueOf(value) : null;
|
}else{
|
return null;
|
}
|
}
|
|
|
/**
|
* 编辑推单配置
|
* @param pushOrder
|
* @param uid
|
* @return
|
* @throws Exception
|
*/
|
@Override
|
public ResultUtil editPushOrder(Integer pushOrder, Integer uid) throws Exception {
|
Dispatch dispatch = this.selectById(uid);
|
if(pushOrder == dispatch.getPushOrder()){
|
return ResultUtil.error("不允许重复配置");
|
}
|
dispatch.setPushOrder(pushOrder);
|
this.updateById(dispatch);
|
return ResultUtil.success();
|
}
|
|
public Dispatch queryByAccount(String account){
|
return dispatchMapper.queryByAccount(account);
|
}
|
|
|
|
/**
|
* 获取token
|
* @param dispatch
|
* @param password
|
* @return
|
*/
|
private String getToken(Dispatch dispatch, String password) throws Exception{
|
//封装请求账号密码为shiro可验证的token
|
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(dispatch.getAccount(), password.toCharArray());
|
String credentials = dispatch.getPassword();
|
ByteSource credentialsSalt = new Md5Hash(SALT);
|
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
|
new ShiroUser(), credentials, credentialsSalt, "");
|
|
//校验用户账号密码
|
HashedCredentialsMatcher md5CredentialsMatcher = new HashedCredentialsMatcher();
|
md5CredentialsMatcher.setHashAlgorithmName(ShiroKit.hashAlgorithmName);
|
md5CredentialsMatcher.setHashIterations(ShiroKit.hashIterations);
|
boolean passwordTrueFlag = md5CredentialsMatcher.doCredentialsMatch(
|
usernamePasswordToken, simpleAuthenticationInfo);
|
String s = JwtTokenUtil.generateToken(String.valueOf(dispatch.getId()));
|
redisUtil.setStrValue(s.substring(s.length() - 32), String.valueOf(dispatch.getId()), 7 * 24 * 60 * 60);
|
redisUtil.setStrValue(dispatch.getPhone(), s.substring(s.length() - 32));
|
redisUtil.setStrValue("DISPATCH_" + dispatch.getId(), s);
|
return s;
|
}
|
|
|
|
/**
|
* 单点登录
|
* @param id
|
*/
|
private void singlePointLogin(Integer id) throws Exception{
|
//开始验证当前账号是否在别处登录
|
String value = redisUtil.getValue("DISPATCH_" + id);
|
if(ToolUtil.isNotEmpty(value)){//将另外设备上的强迫下线
|
//开始清除redis中无效的数据
|
Dispatch dispatch = dispatchMapper.selectById(id);
|
String key = redisUtil.getValue(dispatch.getPhone());
|
redisUtil.remove(key);//删除个人信息数据
|
redisUtil.remove(dispatch.getPhone());//删除后台冻结相关缓存
|
redisUtil.remove("DISPATCH_" + id);//清除存储的token
|
}
|
}
|
}
|