feat: 登录功能开发
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.schisandra.auth.application.controller;
|
package com.schisandra.auth.application.controller;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import com.schisandra.auth.application.convert.SchisandraAuthUserDTOConverter;
|
import com.schisandra.auth.application.convert.SchisandraAuthUserDTOConverter;
|
||||||
@@ -14,6 +15,7 @@ import org.apache.commons.lang3.ObjectUtils;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -78,14 +80,26 @@ public class SchisandraAuthUserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("login")
|
@PostMapping("login")
|
||||||
public Result login(@RequestBody SchisandraAuthUserDTO schisandraAuthUserDTO) {
|
public Result login(@RequestBody SchisandraAuthUserDTO schisandraAuthUserDTO) {
|
||||||
String EmailType = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$";
|
HashMap<String, Object> map = new HashMap<>();
|
||||||
Boolean s = EmailType.matches(schisandraAuthUserDTO.getEmail());
|
SchisandraAuthUserBO schisandraAuthUserBO=SchisandraAuthUserDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthUserDTO);
|
||||||
// return Result.ok(schisandraAuthUserDomainService.login(SchisandraAuthUserDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthUserDTO)));
|
SchisandraAuthUserDTO schisandraAuthUserDTO1=SchisandraAuthUserDTOConverter.INSTANCE.convertBOToDTO(schisandraAuthUserDomainService.login(schisandraAuthUserBO));
|
||||||
// return null;
|
map.put("user",schisandraAuthUserDTO1);
|
||||||
return Result.ok(s);
|
if(schisandraAuthUserDTO1!=null){
|
||||||
|
if (StpUtil.isLogin(schisandraAuthUserDTO1.getId())){
|
||||||
|
StpUtil.logout(schisandraAuthUserDTO1.getId());
|
||||||
|
StpUtil.login(schisandraAuthUserDTO1.getId());
|
||||||
|
String token=StpUtil.getTokenValueByLoginId(schisandraAuthUserDTO1.getId());
|
||||||
|
map.put("token",token);
|
||||||
|
return Result.ok(map);
|
||||||
|
}else {
|
||||||
|
StpUtil.login(schisandraAuthUserDTO1.getId());
|
||||||
|
return Result.ok(schisandraAuthUserDTO1);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
return Result.fail("用户名或密码错误");}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -19,4 +19,6 @@ public interface SchisandraAuthUserDTOConverter {
|
|||||||
|
|
||||||
SchisandraAuthUserBO convertDTOToBO(SchisandraAuthUserDTO schisandraAuthUserDTO);
|
SchisandraAuthUserBO convertDTOToBO(SchisandraAuthUserDTO schisandraAuthUserDTO);
|
||||||
|
|
||||||
|
SchisandraAuthUserDTO convertBOToDTO(SchisandraAuthUserBO schisandraAuthUserBO);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,6 @@ package com.schisandra.auth.domain.service.impl;
|
|||||||
|
|
||||||
|
|
||||||
import cn.dev33.satoken.stp.SaLoginConfig;
|
import cn.dev33.satoken.stp.SaLoginConfig;
|
||||||
import cn.dev33.satoken.stp.SaTokenInfo;
|
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.schisandra.auth.common.entity.Result;
|
import com.schisandra.auth.common.entity.Result;
|
||||||
@@ -30,16 +29,12 @@ import com.schisandra.auth.infra.basic.service.SchisandraAuthUserService;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import me.zhyd.oauth.model.AuthToken;
|
import me.zhyd.oauth.model.AuthToken;
|
||||||
import me.zhyd.oauth.model.AuthUser;
|
import me.zhyd.oauth.model.AuthUser;
|
||||||
import org.apache.commons.codec.digest.Md5Crypt;
|
|
||||||
import org.apache.commons.lang3.ObjectUtils;
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 领域service实现了
|
* 领域service实现了
|
||||||
@@ -89,7 +84,24 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SchisandraAuthUserBO login(SchisandraAuthUserBO schisandraAuthUserBO) {
|
public SchisandraAuthUserBO login(SchisandraAuthUserBO schisandraAuthUserBO) {
|
||||||
return null;
|
|
||||||
|
Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
|
||||||
|
Boolean emailmatch = pattern.matcher(schisandraAuthUserBO.getUserName()).matches();
|
||||||
|
SchisandraAuthUser schisandraAuthUser;
|
||||||
|
if(emailmatch){
|
||||||
|
schisandraAuthUser=schisandraAuthUserService.queryByEmail(schisandraAuthUserBO.getUserName());
|
||||||
|
}else {
|
||||||
|
schisandraAuthUser=schisandraAuthUserService.queryByPhone(schisandraAuthUserBO.getUserName());
|
||||||
|
}
|
||||||
|
if (schisandraAuthUser != null && schisandraAuthUserBO.getPassword() != null){
|
||||||
|
if (schisandraAuthUser.getPassword().equals(schisandraAuthUserBO.getPassword())){
|
||||||
|
return SchisandraAuthUserBOConverter.INSTANCE.convertEntityToBO(schisandraAuthUser);
|
||||||
|
}else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,10 +1,11 @@
|
|||||||
package com.schisandra.auth.infra.basic.entity;
|
package com.schisandra.auth.infra.basic.entity;
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.schisandra.auth.infra.interceptor.EncryptInterceptor;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -16,24 +17,26 @@ import java.util.Date;
|
|||||||
* @since 2024-05-23 20:00:28
|
* @since 2024-05-23 20:00:28
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@TableName("schisandra_auth_user")
|
@TableName(value = "schisandra_auth_user",autoResultMap = true)
|
||||||
public class SchisandraAuthUser implements Serializable {
|
public class SchisandraAuthUser implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@TableId(value = "`id`", type = IdType.AUTO)
|
@TableId(value = "id", type = IdType.AUTO)
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@TableField("`user_name`")
|
|
||||||
|
@TableField(value = "`user_name`")
|
||||||
private String userName;
|
private String userName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@TableField("`nick_name`")
|
@TableField("`nick_name`")
|
||||||
private String nickName;
|
private String nickName;
|
||||||
|
|
||||||
@@ -52,7 +55,7 @@ public class SchisandraAuthUser implements Serializable {
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@TableField("`password`")
|
@TableField(value = "`password`",typeHandler = EncryptInterceptor.class)
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -10,8 +10,12 @@ import com.schisandra.auth.infra.basic.entity.SchisandraAuthUser;
|
|||||||
*/
|
*/
|
||||||
public interface SchisandraAuthUserService {
|
public interface SchisandraAuthUserService {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
SchisandraAuthUser queryByEmail(String email);
|
||||||
SchisandraAuthUser queryByPhone(String phone);
|
SchisandraAuthUser queryByPhone(String phone);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过ID查询单条数据
|
* 通过ID查询单条数据
|
||||||
*
|
*
|
||||||
|
@@ -23,6 +23,11 @@ public class SchisandraAuthUserServiceImpl implements SchisandraAuthUserService
|
|||||||
@Resource
|
@Resource
|
||||||
private SchisandraAuthUserDao schisandraAuthUserDao;
|
private SchisandraAuthUserDao schisandraAuthUserDao;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SchisandraAuthUser queryByEmail(String email) {
|
||||||
|
return this.schisandraAuthUserDao.selectOne(new QueryWrapper<SchisandraAuthUser>().eq("email",email));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SchisandraAuthUser queryByPhone(String phone) {
|
public SchisandraAuthUser queryByPhone(String phone) {
|
||||||
return this.schisandraAuthUserDao.selectOne(new QueryWrapper<SchisandraAuthUser>().eq("phone", phone).eq("is_deleted", 0));
|
return this.schisandraAuthUserDao.selectOne(new QueryWrapper<SchisandraAuthUser>().eq("phone", phone).eq("is_deleted", 0));
|
||||||
|
@@ -0,0 +1,69 @@
|
|||||||
|
package com.schisandra.auth.infra.interceptor;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.schisandra.auth.common.redis.RedisUtil;
|
||||||
|
import com.schisandra.auth.common.utils.AESUtils;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import org.apache.ibatis.type.BaseTypeHandler;
|
||||||
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.sql.CallableStatement;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class EncryptInterceptor<T> extends BaseTypeHandler<T> {
|
||||||
|
@Resource
|
||||||
|
RedisUtil redisUtil;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
|
||||||
|
String key = redisUtil.get("key1");
|
||||||
|
// String md5BySalt = SaSecureUtil.md5BySalt("123456", "abc");
|
||||||
|
try {
|
||||||
|
ps.setString(i, AESUtils.encrypt((String) parameter, key));
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||||
|
String key = redisUtil.get("key1");
|
||||||
|
try {
|
||||||
|
if ( "password".equals(columnName)) {
|
||||||
|
String columnValue = rs.getString(columnName);
|
||||||
|
return (T) columnValue;
|
||||||
|
}else {
|
||||||
|
String columnValue = rs.getString(columnName);
|
||||||
|
return StrUtil.isBlank(columnValue) ? (T) columnValue : (T) AESUtils.decrypt(columnValue, key);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
@Override
|
||||||
|
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||||
|
String key = redisUtil.get("key1");
|
||||||
|
String columnValue = rs.getString(columnIndex);
|
||||||
|
return StrUtil.isBlank(columnValue) ? (T) columnValue : (T) AESUtils.decrypt(columnValue, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||||
|
String key = redisUtil.get("key1");
|
||||||
|
String columnValue = cs.getString(columnIndex);
|
||||||
|
try {
|
||||||
|
return StrUtil.isBlank(columnValue) ? (T) columnValue : (T) AESUtils.decrypt(columnValue, key);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
<resultMap id="BaseResultMap" type="com.schisandra.auth.infra.basic.entity.SchisandraAuthUser">
|
<resultMap id="BaseResultMap" type="com.schisandra.auth.infra.basic.entity.SchisandraAuthUser">
|
||||||
<id column="id" jdbcType="BIGINT" property="id"/>
|
<id column="id" jdbcType="BIGINT" property="id"/>
|
||||||
<result column="user_name" jdbcType="VARCHAR" property="userName"/>
|
<result column="user_name" jdbcType="VARCHAR" property="userName" typeHandler="com.schisandra.auth.infra.interceptor.EncryptInterceptor"/>
|
||||||
<result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
|
<result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
|
||||||
<result column="email" jdbcType="VARCHAR" property="email"/>
|
<result column="email" jdbcType="VARCHAR" property="email"/>
|
||||||
<result column="phone" jdbcType="VARCHAR" property="phone"/>
|
<result column="phone" jdbcType="VARCHAR" property="phone"/>
|
||||||
|
Reference in New Issue
Block a user