Merge branch 'master' of https://gitee.com/xinyingpower/xymanagerbackend
commit
618ad79396
@ -0,0 +1,84 @@
|
||||
package com.shxy.xymanager_admin.controller;
|
||||
|
||||
|
||||
import com.shxy.xymanager_common.base.AjaxResult;
|
||||
import com.shxy.xymanager_common.base.ResponseReult;
|
||||
import com.shxy.xymanager_common.bean.ServiceBody;
|
||||
import com.shxy.xymanager_common.bean.ServiceStatus;
|
||||
import com.shxy.xymanager_common.bean.SysUser;
|
||||
import com.shxy.xymanager_common.constant.Constants;
|
||||
import com.shxy.xymanager_common.model.TerminalListModel;
|
||||
import com.shxy.xymanager_common.security.LoginUser;
|
||||
import com.shxy.xymanager_common.util.ServletUtils;
|
||||
import com.shxy.xymanager_common.vo.SysUserVo;
|
||||
import com.shxy.xymanager_service.service.LoginService;
|
||||
import com.shxy.xymanager_service.service.SysUserService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiResponse;
|
||||
import io.swagger.annotations.ApiResponses;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Api(value = "登录验证", tags = "登录验证")
|
||||
@RestController
|
||||
@Slf4j
|
||||
public class SysLoginController {
|
||||
|
||||
|
||||
@Autowired
|
||||
private LoginService loginService;
|
||||
|
||||
@Autowired
|
||||
SysUserService sysUserService;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @Description 登录方法
|
||||
*
|
||||
* @param sysUser
|
||||
* @return ResponseReult
|
||||
*/
|
||||
@ApiOperation(value = "登录", notes = "登录", httpMethod = "POST")
|
||||
@ApiResponses({@ApiResponse(code = 200, message = "请求成功"), @ApiResponse(code = 400, message = "请求参数没填好"), @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")})
|
||||
@RequestMapping("/login")
|
||||
public ResponseReult<String> login(@RequestBody SysUser sysUser) throws Exception {
|
||||
ServiceBody<String> serviceBody = loginService.remoteLogin(sysUser.getUserName(), sysUser.getPassword());
|
||||
if (serviceBody.getCode() == ServiceStatus.SUCCESS) {
|
||||
return ResponseReult.success(serviceBody.getData());
|
||||
} else {
|
||||
return ResponseReult.error(serviceBody.getCode(), serviceBody.getMsg());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 新增用户
|
||||
*
|
||||
* @param vo
|
||||
* @return
|
||||
*/
|
||||
|
||||
@ApiOperation(value = "新增用户", notes = "新增用户", httpMethod = "POST")
|
||||
@ApiResponses({@ApiResponse(code = 200, message = "请求成功"), @ApiResponse(code = 400, message = "请求参数没填好"), @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")})
|
||||
@RequestMapping("/addUser")
|
||||
public ResponseReult<String> add(@RequestBody @Validated SysUserVo vo) {
|
||||
ServiceBody<String> serviceBody = sysUserService.addUser(vo);
|
||||
if (serviceBody.getCode() == ServiceStatus.SUCCESS) {
|
||||
return ResponseReult.success(serviceBody.getData());
|
||||
} else {
|
||||
return ResponseReult.error(serviceBody.getCode(), serviceBody.getMsg());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.shxy.xymanager_common.constant;
|
||||
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Uuid Utils
|
||||
* @author Jordan_Li
|
||||
*
|
||||
*/
|
||||
public class UuidUtils {
|
||||
|
||||
public static String getUUID() {
|
||||
return UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.shxy.xymanager_common.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigInteger;
|
||||
|
||||
@Data
|
||||
public class UserSession implements Serializable {
|
||||
private BigInteger id;
|
||||
|
||||
private String sessionId;
|
||||
|
||||
private String userName;
|
||||
|
||||
private String role;
|
||||
|
||||
private BigInteger expireTime;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.shxy.xymanager_common.exception;
|
||||
|
||||
/**
|
||||
* 自定义异常
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class CustomException extends RuntimeException
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String message;
|
||||
|
||||
public CustomException(String message)
|
||||
{
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public CustomException(String message, Integer code)
|
||||
{
|
||||
this.message = message;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public CustomException(String message, Throwable e)
|
||||
{
|
||||
super(message, e);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage()
|
||||
{
|
||||
return message;
|
||||
}
|
||||
|
||||
public Integer getCode()
|
||||
{
|
||||
return code;
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package com.shxy.xymanager_common.util;
|
||||
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* @author Bobi_huo
|
||||
* @date 2020-10-10 15:26
|
||||
*/
|
||||
public class Md5 {
|
||||
|
||||
private static final String[] STR_DIGITS = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
|
||||
|
||||
public Md5() {
|
||||
}
|
||||
|
||||
private static String byteToArrayString(byte bByte) {
|
||||
int iRet = bByte;
|
||||
if (bByte < 0) {
|
||||
iRet = bByte + 256;
|
||||
}
|
||||
|
||||
int iD1 = iRet / 16;
|
||||
int iD2 = iRet % 16;
|
||||
return STR_DIGITS[iD1] + STR_DIGITS[iD2];
|
||||
}
|
||||
|
||||
private static String byteToString(byte[] bByte) {
|
||||
StringBuffer sBuffer = new StringBuffer();
|
||||
|
||||
for(int i = 0; i < bByte.length; ++i) {
|
||||
sBuffer.append(byteToArrayString(bByte[i]));
|
||||
}
|
||||
|
||||
return sBuffer.toString().toUpperCase();
|
||||
}
|
||||
|
||||
public static String getMD5Code(String strObj) {
|
||||
String resultString = null;
|
||||
|
||||
try {
|
||||
new String(strObj);
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
resultString = byteToString(md.digest(strObj.getBytes()));
|
||||
} catch (NoSuchAlgorithmException var3) {
|
||||
var3.printStackTrace();
|
||||
}
|
||||
|
||||
return resultString;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
// System.out.println(getMD5Code("15500000000123456"));
|
||||
|
||||
System.out.println(DigestUtils.md5DigestAsHex("123456".getBytes()));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.shxy.xymanager_common.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Data
|
||||
@ApiModel(value = "用户对象", description = "用户对象描述")
|
||||
public class SysUserVo {
|
||||
|
||||
@NotNull(message = "用户名")
|
||||
@ApiModelProperty(value = "用户名", example = "123455")
|
||||
private String userName;
|
||||
|
||||
@NotNull(message = "昵称")
|
||||
@ApiModelProperty(value = "昵称", example = "123455")
|
||||
private String nickName;
|
||||
|
||||
@NotNull(message = "密码")
|
||||
@ApiModelProperty(value = "密码", example = "123455")
|
||||
private String password;
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.shxy.xymanager_dao.dao.SysUserMapperDao">
|
||||
<resultMap id="SysUserResult" type="com.shxy.xymanager_common.bean.SysUser">
|
||||
<id column="id" property="userId" />
|
||||
<result column="name" property="userName" />
|
||||
<result column="password" property="password" />
|
||||
<result column="status" property="status" />
|
||||
</resultMap>
|
||||
|
||||
|
||||
<sql id="selectUserVo">
|
||||
SELECT
|
||||
user_id,
|
||||
user_name,
|
||||
`status`
|
||||
FROM
|
||||
sys_user u
|
||||
</sql>
|
||||
|
||||
|
||||
<select id="selectByUserAccount" resultMap="SysUserResult">
|
||||
SELECT * FROM sys_user WHERE user_name = #{userName}
|
||||
</select>
|
||||
|
||||
|
||||
<select id="selectPages" resultMap="SysUserResult">
|
||||
<include refid="selectUserVo"/>
|
||||
WHERE status = '0'
|
||||
<if test="params.userName != null and params.userName != ''">
|
||||
AND user_name like concat('%', #{params.userName}, '%')
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<select id="checkUserNameUnique" parameterType="String" resultType="int">
|
||||
select count(1) from sys_user where user_name = #{userName}
|
||||
</select>
|
||||
|
||||
<select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
|
||||
<include refid="selectUserVo"/>
|
||||
where u.user_name = #{userName}
|
||||
</select>
|
||||
|
||||
<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
|
||||
<include refid="selectUserVo"/>
|
||||
where u.user_id = #{userId}
|
||||
</select>
|
||||
|
||||
|
||||
<update id="resetUserPwd">
|
||||
update sys_user set password = #{password} where user_name = #{userName}
|
||||
</update>
|
||||
|
||||
<insert id="addUser" parameterType="com.shxy.xymanager_common.bean.SysUser">
|
||||
insert into sys_user(user_name,nick_name,password)
|
||||
values(#{userName},#{nickName},#{password})
|
||||
</insert>
|
||||
|
||||
<select id="selectUserBySessionId" resultType="com.shxy.xymanager_common.entity.UserSession">
|
||||
select
|
||||
session_id as sessionId,
|
||||
user_name as userName,
|
||||
role,
|
||||
expire_time as expireTime
|
||||
from sys_user_session
|
||||
where session_id = #{sessionId}
|
||||
</select>
|
||||
|
||||
<update id="updateUserSession" parameterType="com.shxy.xymanager_common.entity.UserSession">
|
||||
update `sys_user_session`
|
||||
<set>
|
||||
<if test="expireTime!= null">
|
||||
expireTime = #{expireTime},
|
||||
</if>
|
||||
</set>
|
||||
where session_id = #{sessionId}
|
||||
</update>
|
||||
|
||||
|
||||
<insert id="insertUserSession" parameterType="com.shxy.xymanager_common.entity.UserSession">
|
||||
insert into sys_user_session (session_id, user_name,role,expire_time)
|
||||
values (#{sessionId,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}, #{ROLE,jdbcType=VARCHAR}, #{expireTime,jdbcType=INTEGER})
|
||||
</insert>
|
||||
|
||||
</mapper>
|
@ -0,0 +1,112 @@
|
||||
package com.shxy.xymanager_service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import com.shxy.xymanager_common.bean.ServiceBody;
|
||||
import com.shxy.xymanager_common.bean.SysUser;
|
||||
import com.shxy.xymanager_common.config.CustomRsaProperties;
|
||||
import com.shxy.xymanager_common.constant.Constants;
|
||||
import com.shxy.xymanager_common.constant.HttpStatusCode;
|
||||
import com.shxy.xymanager_common.entity.UserSession;
|
||||
import com.shxy.xymanager_common.exception.Asserts;
|
||||
import com.shxy.xymanager_common.exception.UserPasswordNotMatchException;
|
||||
import com.shxy.xymanager_common.manager.AsyncManager;
|
||||
import com.shxy.xymanager_common.manager.factory.AsyncFactory;
|
||||
import com.shxy.xymanager_common.security.LoginUser;
|
||||
import com.shxy.xymanager_common.util.MyDateUtils;
|
||||
import com.shxy.xymanager_common.util.RsaUtils;
|
||||
import com.shxy.xymanager_common.util.StringUtils;
|
||||
import com.shxy.xymanager_dao.dao.SysUserMapperDao;
|
||||
import com.shxy.xymanager_service.service.LoginService;
|
||||
import com.shxy.xymanager_service.service.SysUserService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 装置通道实现层
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class LoginServiceImpl implements LoginService {
|
||||
|
||||
|
||||
@Autowired
|
||||
private SysUserService sysUserService;
|
||||
|
||||
@Autowired
|
||||
private SysUserMapperDao sysUserMapperDao;
|
||||
|
||||
/**
|
||||
* @param username 用户名
|
||||
* @param password 密码
|
||||
* @return java.lang.String
|
||||
* @description 登录验证
|
||||
*/
|
||||
public ServiceBody<String> login(String username, String password) throws Exception {
|
||||
// String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
||||
// String captcha = redisCacheService.getCacheObject(verifyKey);
|
||||
//
|
||||
// redisCacheService.deleteObject(verifyKey);
|
||||
// if (captcha == null) {
|
||||
// AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, Constants.LOGIN_FAIL, "验证码错误"));
|
||||
// throw new CustomException("验证码错误", HttpStatusCode.ERROR);
|
||||
// }
|
||||
//
|
||||
// if (!code.equalsIgnoreCase(captcha)) {
|
||||
// AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, Constants.LOGIN_FAIL, "验证码错误"));
|
||||
// throw new CustomException("验证码错误", HttpStatusCode.ERROR);
|
||||
// }
|
||||
// 生成token
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param username 账号
|
||||
* @param password 密码
|
||||
* @return
|
||||
*/
|
||||
public ServiceBody<String> remoteLogin(String username, String password) throws Exception {
|
||||
UserSession user = getLoginUser(username, password);
|
||||
sysUserMapperDao.insertUserSession(user);
|
||||
// 生成token
|
||||
return Asserts.success(user.getSessionId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录校验
|
||||
*
|
||||
* @param username
|
||||
* @param password
|
||||
* @return
|
||||
*/
|
||||
public UserSession getLoginUser(String username, String password) throws Exception {
|
||||
// 用户验证
|
||||
SysUser account = sysUserService.findByUserAccount(username);
|
||||
// 判断用户是否存在
|
||||
if (StringUtils.isNull(account)) {
|
||||
throw new UserPasswordNotMatchException("用户不存在", HttpStatusCode.ERROR);
|
||||
}
|
||||
// String userPassword = Md5.getMD5Code(account.getPasswordSalt() + password);
|
||||
// String userPassword = Md5.getMD5Code(password);
|
||||
// 密码解密
|
||||
String pwd = RsaUtils.decryptByPrivateKey( CustomRsaProperties.privateKey, account.getPassword() );
|
||||
// 密码对比
|
||||
if (!password.equals(pwd)) {
|
||||
throw new UserPasswordNotMatchException("密码错误", HttpStatusCode.ERROR);
|
||||
}
|
||||
UserSession user = new UserSession();
|
||||
// loginUser.setPermissions(permissions);
|
||||
user.setUserName(username);
|
||||
user.setRole(account.getRole());
|
||||
BigInteger date = MyDateUtils.TimeMillSecond2Second(new DateTime());
|
||||
user.setExpireTime(date);
|
||||
return user;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.shxy.xymanager_service.impl;
|
||||
|
||||
import com.shxy.xymanager_common.security.LoginUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 自定义退出处理类 返回成功
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Configuration
|
||||
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler
|
||||
{
|
||||
|
||||
/**
|
||||
* 退出处理
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
|
||||
throws IOException, ServletException
|
||||
{
|
||||
|
||||
/*ServletUtils.renderString(response, JSON.toJSONString( AjaxResult.error( HttpStatusCode.SUCCESS, "退出成功")));*/
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.shxy.xymanager_service.service;
|
||||
|
||||
import com.shxy.xymanager_common.bean.SysUser;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 线路接口
|
||||
*
|
||||
* @author 晶晶
|
||||
*/
|
||||
public interface CustomPermissionService {
|
||||
|
||||
Set<String> getRolePermission(SysUser user);
|
||||
|
||||
Set<String> getMenuPermission(SysUser user);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.shxy.xymanager_service.service;
|
||||
|
||||
import com.shxy.xymanager_common.bean.SysUser;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
/**
|
||||
* 线路接口
|
||||
*
|
||||
* @author 晶晶
|
||||
*/
|
||||
public interface CustomUserDetailsService {
|
||||
|
||||
UserDetails loadUserByUsername(String userAccount);
|
||||
|
||||
UserDetails createLoginUser(SysUser user);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue