feat: 登录功能开发
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.schisandra.auth.application.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.schisandra.auth.application.convert.SchisandraAuthUserDTOConverter;
|
||||
@@ -14,6 +15,7 @@ import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -78,14 +80,26 @@ public class SchisandraAuthUserController {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@PostMapping("login")
|
||||
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}$";
|
||||
Boolean s = EmailType.matches(schisandraAuthUserDTO.getEmail());
|
||||
// return Result.ok(schisandraAuthUserDomainService.login(SchisandraAuthUserDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthUserDTO)));
|
||||
// return null;
|
||||
return Result.ok(s);
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
SchisandraAuthUserBO schisandraAuthUserBO=SchisandraAuthUserDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthUserDTO);
|
||||
SchisandraAuthUserDTO schisandraAuthUserDTO1=SchisandraAuthUserDTOConverter.INSTANCE.convertBOToDTO(schisandraAuthUserDomainService.login(schisandraAuthUserBO));
|
||||
map.put("user",schisandraAuthUserDTO1);
|
||||
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);
|
||||
|
||||
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.SaTokenInfo;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
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 me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import org.apache.commons.codec.digest.Md5Crypt;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 领域service实现了
|
||||
@@ -89,8 +84,25 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
||||
|
||||
@Override
|
||||
public SchisandraAuthUserBO login(SchisandraAuthUserBO schisandraAuthUserBO) {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param schisandraAuthUserBO
|
||||
|
@@ -1,10 +1,11 @@
|
||||
package com.schisandra.auth.infra.basic.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.schisandra.auth.infra.interceptor.EncryptInterceptor;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
@@ -16,24 +17,26 @@ import java.util.Date;
|
||||
* @since 2024-05-23 20:00:28
|
||||
*/
|
||||
@Data
|
||||
@TableName("schisandra_auth_user")
|
||||
@TableName(value = "schisandra_auth_user",autoResultMap = true)
|
||||
public class SchisandraAuthUser implements Serializable {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableId(value = "`id`", type = IdType.AUTO)
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableField("`user_name`")
|
||||
|
||||
@TableField(value = "`user_name`")
|
||||
private String userName;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
@TableField("`nick_name`")
|
||||
private String nickName;
|
||||
|
||||
@@ -52,7 +55,7 @@ public class SchisandraAuthUser implements Serializable {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableField("`password`")
|
||||
@TableField(value = "`password`",typeHandler = EncryptInterceptor.class)
|
||||
private String password;
|
||||
|
||||
/**
|
||||
|
@@ -10,8 +10,12 @@ import com.schisandra.auth.infra.basic.entity.SchisandraAuthUser;
|
||||
*/
|
||||
public interface SchisandraAuthUserService {
|
||||
|
||||
|
||||
|
||||
SchisandraAuthUser queryByEmail(String email);
|
||||
SchisandraAuthUser queryByPhone(String phone);
|
||||
|
||||
|
||||
/**
|
||||
* 通过ID查询单条数据
|
||||
*
|
||||
|
@@ -23,6 +23,11 @@ public class SchisandraAuthUserServiceImpl implements SchisandraAuthUserService
|
||||
@Resource
|
||||
private SchisandraAuthUserDao schisandraAuthUserDao;
|
||||
|
||||
@Override
|
||||
public SchisandraAuthUser queryByEmail(String email) {
|
||||
return this.schisandraAuthUserDao.selectOne(new QueryWrapper<SchisandraAuthUser>().eq("email",email));
|
||||
}
|
||||
|
||||
@Override
|
||||
public SchisandraAuthUser queryByPhone(String phone) {
|
||||
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">
|
||||
<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="email" jdbcType="VARCHAR" property="email"/>
|
||||
<result column="phone" jdbcType="VARCHAR" property="phone"/>
|
||||
|
Reference in New Issue
Block a user