diff --git a/pom.xml b/pom.xml index c5cfa62..cadcb4c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.3.RELEASE + 2.5.14 com.jwl.driver.server driver-server @@ -32,9 +32,14 @@ org.springframework.boot - spring-boot-starter-security + spring-boot-starter-validation + + + + + mysql mysql-connector-java @@ -103,6 +108,13 @@ + + junit + junit + 4.12 + test + + org.projectlombok lombok diff --git a/src/main/java/com/jwl/driver/server/DriverServerApplication.java b/src/main/java/com/jwl/driver/server/DriverServerApplication.java index b603dba..4b0d9d2 100644 --- a/src/main/java/com/jwl/driver/server/DriverServerApplication.java +++ b/src/main/java/com/jwl/driver/server/DriverServerApplication.java @@ -3,13 +3,21 @@ package com.jwl.driver.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @ComponentScan(basePackages = {"com.jwl.driver.server.*"}) @SpringBootApplication -public class DriverServerApplication { +public class DriverServerApplication implements WebMvcConfigurer { public static void main(String[] args) { SpringApplication.run(DriverServerApplication.class, args); } + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); + registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); + } + } diff --git a/src/main/java/com/jwl/driver/server/config/CorsConfig.java b/src/main/java/com/jwl/driver/server/config/CorsConfig.java new file mode 100644 index 0000000..3bed5b9 --- /dev/null +++ b/src/main/java/com/jwl/driver/server/config/CorsConfig.java @@ -0,0 +1,65 @@ +package com.jwl.driver.server.config; + +import com.jwl.driver.server.interceptor.AuthInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.reactive.CorsWebFilter; +import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/** + * @author 曹林 + * @description 跨域配置类 + * @create 2023/8/11 22:39 + */ +@Configuration +public class CorsConfig implements WebMvcConfigurer { + + @Bean + public AuthInterceptor initAuthInterceptor(){ + return new AuthInterceptor(); + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(initAuthInterceptor()).addPathPatterns("/driver-api/**") + .excludePathPatterns("/login/**"); + } + + @Override + public void addCorsMappings(CorsRegistry registry) { + // 设置允许跨域的路径 + registry.addMapping("/**") + // 设置允许跨域请求的域名 + .allowedOriginPatterns("*") + // 是否允许cookie + .allowCredentials(true) + // 设置允许的请求方式 + .allowedMethods("GET", "POST", "DELETE", "PUT") + // 设置允许的header属性 + .allowedHeaders("*") + // 跨域允许时间 + .maxAge(3600); + } + + @Bean + public CorsWebFilter corsWebFilter(){ + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + + CorsConfiguration corsConfiguration = new CorsConfiguration(); + //1、配置跨域 + corsConfiguration.addAllowedHeader("*");//允许哪些头进行跨域 + corsConfiguration.addAllowedMethod("*");//允许哪些请求方式进行跨域 + corsConfiguration.addAllowedOriginPattern("*");//允许哪些请求来源进行跨域 + corsConfiguration.setAllowCredentials(true);//是否允许携带cookie进行跨域,否则跨域请求会丢失cookie信息 + + source.registerCorsConfiguration("/**",corsConfiguration); + + return new CorsWebFilter(source); + } + +} diff --git a/src/main/java/com/jwl/driver/server/config/SwaggerConfig.java b/src/main/java/com/jwl/driver/server/config/SwaggerConfig.java index ccad151..2acb01f 100644 --- a/src/main/java/com/jwl/driver/server/config/SwaggerConfig.java +++ b/src/main/java/com/jwl/driver/server/config/SwaggerConfig.java @@ -47,9 +47,9 @@ public class SwaggerConfig { .title("管理中心") .description("管理中心接口文档") .termsOfServiceUrl("http://127.0.0.1") - .contact(new Contact("jslx", + .contact(new Contact("jwl", "http://www.jslx.com", - "jslx@equ-tech.com")) + "1149034574@qq.com")) .version("1.0") .build(); } diff --git a/src/main/java/com/jwl/driver/server/controller/TdSysUserController.java b/src/main/java/com/jwl/driver/server/controller/TdSysUserController.java index 87b43aa..89c32b9 100644 --- a/src/main/java/com/jwl/driver/server/controller/TdSysUserController.java +++ b/src/main/java/com/jwl/driver/server/controller/TdSysUserController.java @@ -1,9 +1,19 @@ package com.jwl.driver.server.controller; +import com.jwl.driver.server.dto.LoginUserDto; +import com.jwl.driver.server.response.BaseResponse; +import com.jwl.driver.server.service.ITdSysUserService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RestController; /** *

@@ -13,8 +23,21 @@ import org.springframework.stereotype.Controller; * @author Automated procedures * @since 2023-08-10 */ -@Controller -@RequestMapping("//tdSysUser") +@RestController +@Api(tags = "用户管理") +@RequestMapping("/tdSysUser") +@Slf4j public class TdSysUserController { + @Autowired + private ITdSysUserService userService; + + + @ApiOperation("用户登陆") + @PostMapping("/login") + public BaseResponse login(@RequestBody LoginUserDto loginUserDto) throws Exception { + log.info("用户登录======>loginUserDto:{}", loginUserDto); + return BaseResponse.success(userService.login(loginUserDto)); + } + } diff --git a/src/main/java/com/jwl/driver/server/dto/LoginUserDto.java b/src/main/java/com/jwl/driver/server/dto/LoginUserDto.java new file mode 100644 index 0000000..aa80ef6 --- /dev/null +++ b/src/main/java/com/jwl/driver/server/dto/LoginUserDto.java @@ -0,0 +1,25 @@ +package com.jwl.driver.server.dto; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotBlank; + +/** + * @author 曹林 + * @description 登陆入参 + * @create 2023/8/11 22:21 + */ +@Data +@Accessors(chain = true) +public class LoginUserDto { + + @ApiModelProperty("登陆手机号") + @NotBlank(message = "登陆手机号不能为空") + private String phone; + + @ApiModelProperty("登陆验证码") + @NotBlank(message = "登陆验证码不能为空") + private String code; +} diff --git a/src/main/java/com/jwl/driver/server/interceptor/AuthInterceptor.java b/src/main/java/com/jwl/driver/server/interceptor/AuthInterceptor.java new file mode 100644 index 0000000..87316cd --- /dev/null +++ b/src/main/java/com/jwl/driver/server/interceptor/AuthInterceptor.java @@ -0,0 +1,52 @@ +package com.jwl.driver.server.interceptor; + +import com.jwl.driver.server.redis.RedisCache; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.StringUtils; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Objects; + +/** + * @author 曹林 + * @description + * @create 2023/8/11 18:05 + */ + +public class AuthInterceptor implements HandlerInterceptor { + + @Autowired + private RedisCache redisCache; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + response.setCharacterEncoding("UTF-8"); + response.setContentType("text/html;charset=utf-8"); + String token = request.getHeader("token"); + if (StringUtils.isEmpty(token)) { + response.getWriter().print("用户未登录,请登录后操作!"); + return false; + } + Object loginStatus = redisCache.getCacheObject(token); + if( Objects.isNull(loginStatus)){ + response.getWriter().print("登陆异常,请查看!"); + return false; + } + return true; + } + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { + + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + + } +} + + diff --git a/src/main/java/com/jwl/driver/server/redis/RedisCache.java b/src/main/java/com/jwl/driver/server/redis/RedisCache.java new file mode 100644 index 0000000..0008e97 --- /dev/null +++ b/src/main/java/com/jwl/driver/server/redis/RedisCache.java @@ -0,0 +1,296 @@ +package com.jwl.driver.server.redis; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.*; +import org.springframework.stereotype.Component; + +/** + * spring redis 工具类 + * + * @author ruoyi + **/ +@SuppressWarnings(value = {"unchecked", "rawtypes"}) +@Component +@Slf4j +public class RedisCache { + @Autowired + public RedisTemplate redisTemplate; + + @Autowired + private RedisTemplate stringRedisTemplate; + + public boolean hasKey(final String key) { + return Boolean.TRUE.equals(redisTemplate.hasKey(key)); + } + + /** + * 缓存基本的对象,Integer、String、实体类等 + * + * @param key 缓存的键值 + * @param value 缓存的值 + */ + public void setCacheObject(final String key, final T value) { + redisTemplate.opsForValue().set(key, value); + } + + /** + * 缓存基本的对象,Integer、String、实体类等 + * + * @param key 缓存的键值 + * @param value 缓存的值 + * @param timeout 时间 + * @param timeUnit 时间颗粒度 + */ + public void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) { + redisTemplate.opsForValue().set(key, value, timeout, timeUnit); + } + + /** + * 缓存基本的对象,Integer、String、实体类等 + * + * @param key 缓存的键值 + * @param value 缓存的值 + * @param timeout 时间 + */ + public void setCacheObject(final String key, final T value, final long timeout) { + redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS); + } + + /** + * 设置有效时间 + * + * @param key Redis键 + * @param timeout 超时时间 + * @return true=设置成功;false=设置失败 + */ + public boolean expire(final String key, final long timeout) { + return expire(key, timeout, TimeUnit.SECONDS); + } + + /** + * 设置有效时间 + * + * @param key Redis键 + * @param timeout 超时时间 + * @param unit 时间单位 + * @return true=设置成功;false=设置失败 + */ + public boolean expire(final String key, final long timeout, final TimeUnit unit) { + return redisTemplate.expire(key, timeout, unit); + } + + /** + * 获得缓存的基本对象。 + * + * @param key 缓存键值 + * @return 缓存键值对应的数据 + */ + public T getCacheObject(final String key) { + ValueOperations operation = redisTemplate.opsForValue(); + return operation.get(key); + } + + /** + * 删除单个对象 + * + * @param key + */ + public boolean deleteObject(final String key) { + return redisTemplate.delete(key); + } + + /** + * 删除集合对象 + * + * @param collection 多个对象 + * @return + */ + public long deleteObject(final Collection collection) { + return redisTemplate.delete(collection); + } + + /** + * 删除单个对象 + * + * @param key + */ + public void deleteObjectLike(final String key) { + Set keys = redisTemplate.keys(key + "*"); + redisTemplate.delete(keys); + } + + /** + * 缓存List数据 + * + * @param key 缓存的键值 + * @param dataList 待缓存的List数据 + * @return 缓存的对象 + */ + public long setCacheList(final String key, final List dataList) { + Long count = redisTemplate.opsForList().rightPushAll(key, dataList); + return count == null ? 0 : count; + } + + /** + * 获得缓存的list对象 + * + * @param key 缓存的键值 + * @return 缓存键值对应的数据 + */ + public List getCacheList(final String key) { + return redisTemplate.opsForList().range(key, 0, -1); + } + + /** + * 缓存Set + * + * @param key 缓存键值 + * @param dataSet 缓存的数据 + * @return 缓存数据的对象 + */ + public BoundSetOperations setCacheSet(final String key, final Set dataSet) { + BoundSetOperations setOperation = stringRedisTemplate.boundSetOps(key); + Iterator it = dataSet.iterator(); + while (it.hasNext()) { + setOperation.add(it.next()); + } + return setOperation; + } + + /** + * 获得缓存的set + * + * @param key + * @return + */ + public Set getCacheSet(final String key) { + return stringRedisTemplate.opsForSet().members(key); + } + + public Long addCacheSet(final String key, String... value) { + SetOperations setOperations = stringRedisTemplate.opsForSet(); + return setOperations.add(key, value); + } + + public Long removeCacheSet(final String key, Object... value) { + return stringRedisTemplate.opsForSet().remove(key, value); + } + + /** + * 缓存Map + * + * @param key + * @param dataMap + */ + public void setCacheMap(final String key, final Map dataMap) { + if (dataMap != null) { + redisTemplate.opsForHash().putAll(key, dataMap); + } + } + + /** + * 获得缓存的Map + * + * @param key + * @return + */ + public Map getCacheMap(final String key) { + return redisTemplate.opsForHash().entries(key); + } + + /** + * 往Hash中存入数据 + * + * @param key Redis键 + * @param hKey Hash键 + * @param value 值 + */ + public void setCacheMapValue(final String key, final String hKey, final T value) { + redisTemplate.opsForHash().put(key, hKey, value); + } + + /** + * 获取Hash中的数据 + * + * @param key Redis键 + * @param hKey Hash键 + * @return Hash中的对象 + */ + public T getCacheMapValue(final String key, final String hKey) { + HashOperations opsForHash = redisTemplate.opsForHash(); + return opsForHash.get(key, hKey); + } + + /** + * 删除Hash中的数据 + * + * @param key + * @param hkey + */ + public void delCacheMapValue(final String key, final String hkey) { + HashOperations hashOperations = redisTemplate.opsForHash(); + hashOperations.delete(key, hkey); + } + + /** + * 获取多个Hash中的数据 + * + * @param key Redis键 + * @param hKeys Hash键集合 + * @return Hash对象集合 + */ + public List getMultiCacheMapValue(final String key, final Collection hKeys) { + return redisTemplate.opsForHash().multiGet(key, hKeys); + } + + /** + * 获得缓存的基本对象列表 + * + * @param pattern 字符串前缀 + * @return 对象列表 + */ + public Collection keys(final String pattern) { + return redisTemplate.keys(pattern); + } + + /** + * 根据key获取过期时间 + * + * @param key + * @return + */ + public Long getExpire(final String key) { + return redisTemplate.getExpire(key); + } + + + /** + * value 增加 + */ + public Long addKeyValue(String key, Long addValue) { + if (addValue == null || addValue == 0) { + addValue = 1L; + } + return redisTemplate.getConnectionFactory().getConnection().incrBy( + redisTemplate.getKeySerializer().serialize(key), addValue + ); + } + + /** + * value 增加 + */ + public Long increment(final String key, final Integer value, final Long timeout, final TimeUnit timeUnit) { + Long count = redisTemplate.opsForValue().increment(key, value); + redisTemplate.expire(key, timeout, timeUnit); + return count; + } + +} diff --git a/src/main/java/com/jwl/driver/server/service/ITdSysUserService.java b/src/main/java/com/jwl/driver/server/service/ITdSysUserService.java index 759c247..d4b3f44 100644 --- a/src/main/java/com/jwl/driver/server/service/ITdSysUserService.java +++ b/src/main/java/com/jwl/driver/server/service/ITdSysUserService.java @@ -1,7 +1,9 @@ package com.jwl.driver.server.service; +import com.jwl.driver.server.dto.LoginUserDto; import com.jwl.driver.server.entity.TdSysUser; import com.baomidou.mybatisplus.extension.service.IService; +import com.jwl.driver.server.vo.LoginUserVo; /** *

@@ -13,4 +15,5 @@ import com.baomidou.mybatisplus.extension.service.IService; */ public interface ITdSysUserService extends IService { + LoginUserVo login(LoginUserDto loginUserDto); } diff --git a/src/main/java/com/jwl/driver/server/service/impl/TdSysUserServiceImpl.java b/src/main/java/com/jwl/driver/server/service/impl/TdSysUserServiceImpl.java index f45f2d4..a07e09c 100644 --- a/src/main/java/com/jwl/driver/server/service/impl/TdSysUserServiceImpl.java +++ b/src/main/java/com/jwl/driver/server/service/impl/TdSysUserServiceImpl.java @@ -1,11 +1,19 @@ package com.jwl.driver.server.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.jwl.driver.server.dto.LoginUserDto; import com.jwl.driver.server.entity.TdSysUser; import com.jwl.driver.server.mapper.TdSysUserMapper; +import com.jwl.driver.server.redis.RedisCache; import com.jwl.driver.server.service.ITdSysUserService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.jwl.driver.server.vo.LoginUserVo; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.UUID; + /** *

* 用户表; 服务实现类 @@ -17,4 +25,28 @@ import org.springframework.stereotype.Service; @Service public class TdSysUserServiceImpl extends ServiceImpl implements ITdSysUserService { + @Autowired + private RedisCache redisCache; + + @Override + public LoginUserVo login(LoginUserDto loginUserDto) { + //XI + LambdaQueryWrapper cond = new LambdaQueryWrapper() + .eq(TdSysUser::getPhone,"18255439337"); + TdSysUser tdSysUser = this.baseMapper.selectOne(cond); + if (tdSysUser == null){ + //创建用户 + } + + String token = UUID.randomUUID().toString(); + redisCache.setCacheObject(token,"123456",365*24*60*60); + LoginUserVo loginUserVo = new LoginUserVo(); + BeanUtils.copyProperties(tdSysUser,loginUserVo); + loginUserVo.setToken(token); + + return loginUserVo; + + + + } } diff --git a/src/main/java/com/jwl/driver/server/vo/LoginUserVo.java b/src/main/java/com/jwl/driver/server/vo/LoginUserVo.java new file mode 100644 index 0000000..9910038 --- /dev/null +++ b/src/main/java/com/jwl/driver/server/vo/LoginUserVo.java @@ -0,0 +1,68 @@ +package com.jwl.driver.server.vo; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * @author 曹林 + * @description 登陆用户出参 + * @create 2023/8/11 22:49 + */ +@ApiModel("登陆用户出参") +@Data +@Accessors(chain = true) +public class LoginUserVo { + + @ApiModelProperty("用户id") + private Long userId; + + /** + * 用户名 + */ + @ApiModelProperty("用户名") + private String userName; + + /** + * 手机号码 + */ + @ApiModelProperty("手机号码") + private String phone; + + /** + * 备注 + */ + @ApiModelProperty("备注") + private String remark; + + /** + * 头像 + */ + @ApiModelProperty("头像") + private String avatar; + + /** + * 驾校标识 + */ + @ApiModelProperty("驾校标识") + private Long schoolId; + + + /** + * 驾校名称 + */ + @ApiModelProperty("驾校名称") + private String schoolName; + + /** + * 驾校名称 + */ + @ApiModelProperty("用户token") + private String token; + + +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 013f0ab..47d32a5 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -5,12 +5,12 @@ spring: port: 6379 database: 8 timeout: 5000 - password: caolin123 + auth: caolin123 # 数据库 配置 datasource: - url: jdbc:mysql://127.0.0.1:3306/driver_test?characterEncoding=utf-8&autoReconnect=true&maxReconnects=2&useSSL=false&failOverReadOnly=false&serverTimezone=Asia/Shanghai + url: jdbc:mysql://127.0.0.1:3306/driver_server?characterEncoding=utf-8&autoReconnect=true&maxReconnects=2&useSSL=false&failOverReadOnly=false&serverTimezone=Asia/Shanghai username: root password: 123456 diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..4661739 --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + ${CONSOLE_LOG_PATTERN} + + + + + + ${LOG_HOME}/${application_name}/${application_name}.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + 50MB + ${LOG_HOME}/${application_name}/${application_name}.%d{yyyy-MM-dd}.%i.log.zip + 30 + 2GB + + + + + + ${LOG_HOME}/${application_name}/${application_name}-root.log + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + 50MB + ${LOG_HOME}/${application_name}/${application_name}-root.%d{yyyy-MM-dd}.%i.log.zip + 30 + 2GB + + + + + + + + + + + + + + + + + diff --git a/src/test/java/com/jwl/driver/server/DriverServerApplicationTests.java b/src/test/java/com/jwl/driver/server/DriverServerApplicationTest.java similarity index 82% rename from src/test/java/com/jwl/driver/server/DriverServerApplicationTests.java rename to src/test/java/com/jwl/driver/server/DriverServerApplicationTest.java index 9a332f0..cc922d9 100644 --- a/src/test/java/com/jwl/driver/server/DriverServerApplicationTests.java +++ b/src/test/java/com/jwl/driver/server/DriverServerApplicationTest.java @@ -4,7 +4,7 @@ import org.junit.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest -class DriverServerApplicationTests { +class DriverServerApplicationTest { @Test void contextLoads() {