苍穹外卖初始化代码
This commit is contained in:
129
sky-server/pom.xml
Normal file
129
sky-server/pom.xml
Normal file
@@ -0,0 +1,129 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>sky-take-out</artifactId>
|
||||
<groupId>com.sky</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>sky-server</artifactId>
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sky</groupId>
|
||||
<artifactId>sky-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sky</groupId>
|
||||
<artifactId>sky-pojo</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- poi -->
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
16
sky-server/src/main/java/com/sky/SkyApplication.java
Normal file
16
sky-server/src/main/java/com/sky/SkyApplication.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.sky;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement //开启注解方式的事务管理
|
||||
@Slf4j
|
||||
public class SkyApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SkyApplication.class, args);
|
||||
log.info("server started");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.sky.config;
|
||||
|
||||
import com.sky.interceptor.JwtTokenAdminInterceptor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
|
||||
/**
|
||||
* 配置类,注册web层相关组件
|
||||
*/
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
||||
|
||||
@Autowired
|
||||
private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
|
||||
|
||||
/**
|
||||
* 注册自定义拦截器
|
||||
*
|
||||
* @param registry
|
||||
*/
|
||||
protected void addInterceptors(InterceptorRegistry registry) {
|
||||
log.info("开始注册自定义拦截器...");
|
||||
registry.addInterceptor(jwtTokenAdminInterceptor)
|
||||
.addPathPatterns("/admin/**")
|
||||
.excludePathPatterns("/admin/employee/login");
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过knife4j生成接口文档
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public Docket docket() {
|
||||
ApiInfo apiInfo = new ApiInfoBuilder()
|
||||
.title("苍穹外卖项目接口文档")
|
||||
.version("2.0")
|
||||
.description("苍穹外卖项目接口文档")
|
||||
.build();
|
||||
Docket docket = new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(apiInfo)
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage("com.sky.controller"))
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
return docket;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置静态资源映射
|
||||
* @param registry
|
||||
*/
|
||||
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
|
||||
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.sky.controller.admin;
|
||||
|
||||
import com.sky.constant.JwtClaimsConstant;
|
||||
import com.sky.dto.EmployeeLoginDTO;
|
||||
import com.sky.entity.Employee;
|
||||
import com.sky.properties.JwtProperties;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.EmployeeService;
|
||||
import com.sky.utils.JwtUtil;
|
||||
import com.sky.vo.EmployeeLoginVO;
|
||||
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.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 员工管理
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin/employee")
|
||||
@Slf4j
|
||||
public class EmployeeController {
|
||||
|
||||
@Autowired
|
||||
private EmployeeService employeeService;
|
||||
@Autowired
|
||||
private JwtProperties jwtProperties;
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*
|
||||
* @param employeeLoginDTO
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
|
||||
log.info("员工登录:{}", employeeLoginDTO);
|
||||
|
||||
Employee employee = employeeService.login(employeeLoginDTO);
|
||||
|
||||
//登录成功后,生成jwt令牌
|
||||
Map<String, Object> claims = new HashMap<>();
|
||||
claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
|
||||
String token = JwtUtil.createJWT(
|
||||
jwtProperties.getAdminSecretKey(),
|
||||
jwtProperties.getAdminTtl(),
|
||||
claims);
|
||||
|
||||
EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
|
||||
.id(employee.getId())
|
||||
.userName(employee.getUsername())
|
||||
.name(employee.getName())
|
||||
.token(token)
|
||||
.build();
|
||||
|
||||
return Result.success(employeeLoginVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/logout")
|
||||
public Result<String> logout() {
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.sky.handler;
|
||||
|
||||
import com.sky.exception.BaseException;
|
||||
import com.sky.result.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
/**
|
||||
* 全局异常处理器,处理项目中抛出的业务异常
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
@Slf4j
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
/**
|
||||
* 捕获业务异常
|
||||
* @param ex
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler
|
||||
public Result exceptionHandler(BaseException ex){
|
||||
log.error("异常信息:{}", ex.getMessage());
|
||||
return Result.error(ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.sky.interceptor;
|
||||
|
||||
import com.sky.constant.JwtClaimsConstant;
|
||||
import com.sky.properties.JwtProperties;
|
||||
import com.sky.utils.JwtUtil;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* jwt令牌校验的拦截器
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class JwtTokenAdminInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Autowired
|
||||
private JwtProperties jwtProperties;
|
||||
|
||||
/**
|
||||
* 校验jwt
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @param handler
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
//判断当前拦截到的是Controller的方法还是其他资源
|
||||
if (!(handler instanceof HandlerMethod)) {
|
||||
//当前拦截到的不是动态方法,直接放行
|
||||
return true;
|
||||
}
|
||||
|
||||
//1、从请求头中获取令牌
|
||||
String token = request.getHeader(jwtProperties.getAdminTokenName());
|
||||
|
||||
//2、校验令牌
|
||||
try {
|
||||
log.info("jwt校验:{}", token);
|
||||
Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
|
||||
Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());
|
||||
log.info("当前员工id:", empId);
|
||||
//3、通过,放行
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
//4、不通过,响应401状态码
|
||||
response.setStatus(401);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
sky-server/src/main/java/com/sky/mapper/EmployeeMapper.java
Normal file
18
sky-server/src/main/java/com/sky/mapper/EmployeeMapper.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.sky.entity.Employee;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
@Mapper
|
||||
public interface EmployeeMapper {
|
||||
|
||||
/**
|
||||
* 根据用户名查询员工
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
@Select("select * from employee where username = #{username}")
|
||||
Employee getByUsername(String username);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.EmployeeLoginDTO;
|
||||
import com.sky.entity.Employee;
|
||||
|
||||
public interface EmployeeService {
|
||||
|
||||
/**
|
||||
* 员工登录
|
||||
* @param employeeLoginDTO
|
||||
* @return
|
||||
*/
|
||||
Employee login(EmployeeLoginDTO employeeLoginDTO);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.sky.service.impl;
|
||||
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.constant.StatusConstant;
|
||||
import com.sky.dto.EmployeeLoginDTO;
|
||||
import com.sky.entity.Employee;
|
||||
import com.sky.exception.AccountLockedException;
|
||||
import com.sky.exception.AccountNotFoundException;
|
||||
import com.sky.exception.PasswordErrorException;
|
||||
import com.sky.mapper.EmployeeMapper;
|
||||
import com.sky.service.EmployeeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
@Service
|
||||
public class EmployeeServiceImpl implements EmployeeService {
|
||||
|
||||
@Autowired
|
||||
private EmployeeMapper employeeMapper;
|
||||
|
||||
/**
|
||||
* 员工登录
|
||||
*
|
||||
* @param employeeLoginDTO
|
||||
* @return
|
||||
*/
|
||||
public Employee login(EmployeeLoginDTO employeeLoginDTO) {
|
||||
String username = employeeLoginDTO.getUsername();
|
||||
String password = employeeLoginDTO.getPassword();
|
||||
|
||||
//1、根据用户名查询数据库中的数据
|
||||
Employee employee = employeeMapper.getByUsername(username);
|
||||
|
||||
//2、处理各种异常情况(用户名不存在、密码不对、账号被锁定)
|
||||
if (employee == null) {
|
||||
//账号不存在
|
||||
throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
|
||||
}
|
||||
|
||||
//密码比对
|
||||
// TODO 后期需要进行md5加密,然后再进行比对
|
||||
if (!password.equals(employee.getPassword())) {
|
||||
//密码错误
|
||||
throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
|
||||
}
|
||||
|
||||
if (employee.getStatus() == StatusConstant.DISABLE) {
|
||||
//账号被锁定
|
||||
throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
|
||||
}
|
||||
|
||||
//3、返回实体对象
|
||||
return employee;
|
||||
}
|
||||
|
||||
}
|
||||
8
sky-server/src/main/resources/application-dev.yml
Normal file
8
sky-server/src/main/resources/application-dev.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
sky:
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
host: localhost
|
||||
port: 3306
|
||||
database: sky_take_out
|
||||
username: root
|
||||
password: 1234
|
||||
39
sky-server/src/main/resources/application.yml
Normal file
39
sky-server/src/main/resources/application.yml
Normal file
@@ -0,0 +1,39 @@
|
||||
server:
|
||||
port: 8080
|
||||
|
||||
spring:
|
||||
profiles:
|
||||
active: dev
|
||||
main:
|
||||
allow-circular-references: true
|
||||
datasource:
|
||||
druid:
|
||||
driver-class-name: ${sky.datasource.driver-class-name}
|
||||
url: jdbc:mysql://${sky.datasource.host}:${sky.datasource.port}/${sky.datasource.database}?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
|
||||
username: ${sky.datasource.username}
|
||||
password: ${sky.datasource.password}
|
||||
|
||||
mybatis:
|
||||
#mapper配置文件
|
||||
mapper-locations: classpath:mapper/*.xml
|
||||
type-aliases-package: com.sky.entity
|
||||
configuration:
|
||||
#开启驼峰命名
|
||||
map-underscore-to-camel-case: true
|
||||
|
||||
logging:
|
||||
level:
|
||||
com:
|
||||
sky:
|
||||
mapper: debug
|
||||
service: info
|
||||
controller: info
|
||||
|
||||
sky:
|
||||
jwt:
|
||||
# 设置jwt签名加密时使用的秘钥
|
||||
admin-secret-key: inmind
|
||||
# 设置jwt过期时间
|
||||
admin-ttl: 7200000
|
||||
# 设置前端传递过来的令牌名称
|
||||
admin-token-name: token
|
||||
5
sky-server/src/main/resources/mapper/EmployeeMapper.xml
Normal file
5
sky-server/src/main/resources/mapper/EmployeeMapper.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?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.sky.mapper.EmployeeMapper">
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user