初始化仓库

This commit is contained in:
Unique-Jerry 2023-10-13 21:50:21 +08:00
parent ddea9e47ee
commit e743dbc2d0
13 changed files with 422 additions and 28 deletions

View File

@ -2,5 +2,6 @@
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

38
pom.xml
View File

@ -11,10 +11,11 @@
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/>
<relativePath></relativePath>
</parent>
<properties>
@ -56,6 +57,39 @@
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- swagger依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<!-- swagger-ui 依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<!--jwt依赖包-->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.4.0</version>
</dependency>
<!-- redis-clients -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,40 @@
package com.yzdx.AiInterviewer.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* @ClassName redisConfig
* @Description TODO
* @Author 加辣椒了吗?
* @Date 2022/4/28 2:33
* @Version 1.0
**/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
// 将redis注入工厂
RedisTemplate<String,Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 设置key的序列化方式
template.setKeySerializer (RedisSerializer.string());
//设置value的序列化方式
template.setValueSerializer (RedisSerializer.json());
// 设置hash的key的序列化方式
template. setHashKeySerializer (RedisSerializer.string());
// 设置hash的value的序列化方式
template.setHashValueSerializer (RedisSerializer.json());
// 使设置生效
template.afterPropertiesSet();
return template;
}
}

View File

@ -0,0 +1,55 @@
package com.yzdx.AiInterviewer.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
//配置了Swagger 的Docker的bean实例
@Bean
public Docket docket(Environment environment){
//设置要显示的Swagger环境
Profiles profiles =Profiles.of("dev","test");
//判断是否处于开发环境下
boolean flag=environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)
.select()
.apis(RequestHandlerSelectors.basePackage("com.yzdx.AiInterviewer.controller"))
.build()
;
}
//配置Swagger信息=apiInfo
private ApiInfo apiInfo(){
Contact contact=new Contact("扬州大学开发团队", "http://www.yzu.edu.cn/", "2209176490@qq.com");
return new ApiInfo(
"Ai-Interviewer API开发文档",
"再小的帆也能远航",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0"
);
}
}

View File

@ -0,0 +1,51 @@
package com.yzdx.AiInterviewer.config;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.yzdx.AiInterviewer.utiles.JWT;
import net.minidev.json.JSONObject;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
/**
* @Description: 拦截器
* @Author: 新写的旧代码
* @CreateTime: 2022/3/24
*/
public class UserInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Map<String, Object> map = new HashMap<>();
String Token = request.getHeader("Authorization");
// 捕获刚刚JWT中抛出的异常,并封装对应的返回信息
try {
JWT.verifyToken(Token);
return true;
}catch (SignatureVerificationException e){
map.put("msg", "无效签名");
}catch (TokenExpiredException e){
map.put("msg", "已过期");
}catch (AlgorithmMismatchException e){
map.put("msg", "算法不一致");
}catch (Exception e){
map.put("msg", "无效身份信息");
}
// 封装返回值
map.put("code", 401);
JSONObject json = new JSONObject(map);
response.setContentType("application/json;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.print(json);
writer.flush();
writer.close();
return false;
}
}

View File

@ -0,0 +1,27 @@
package com.yzdx.AiInterviewer.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Description: springMVC配置
* @Author: 新写的旧代码
* @CreateTime: 2022/3/24
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 用户拦截器
registry.addInterceptor(new com.yzdx.AiInterviewer.config.UserInterceptor())
// 需要拦截的请求
.addPathPatterns("/**")
// 需要放行的请求
.excludePathPatterns("/user/login","/user/register","/user/sendCode")
// 添加swagger-ui的放行路径
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**","/doc.html/**")
;
}
}

View File

@ -0,0 +1,36 @@
package com.yzdx.AiInterviewer.controller;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
@ApiOperation("用户登录")
@PostMapping("/test")
public Map<String ,Object> testLogin(@ApiParam("用户名") String username ,@ApiParam("密码")String password){
Map<String ,Object> result=new HashMap<>();
result.put("code",0);
result.put("message","登陆成功");
result.put("data","username:"+username+","+"password:"+password);
return result;
}
}

View File

@ -0,0 +1,12 @@
package com.yzdx.AiInterviewer.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}

View File

@ -0,0 +1,68 @@
package com.yzdx.AiInterviewer.utiles;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import java.util.Calendar;
/**
* @Author: 新写的旧代码
* @Description: token生成
* @CreateTime: 2022/3/24
*/
public class JWT {
// 任意字符串
private static final String SING = "xxxx@Jerry";
private static String token;
// get and set
public static String getToken() {
return token;
}
public static void setToken(String token) {
JWT.token = token;
}
// 生成用户token
public static String getJWToken(Integer id){
Calendar instance = Calendar.getInstance();
// 设置过期时间,这里设置的是一天
instance.add(Calendar.DATE,30);
JWTCreator.Builder builder = com.auth0.jwt.JWT.create();
// 指定标识字段
builder.withClaim("lawyerId", id);
// 指定过期时间
token = builder.withExpiresAt(instance.getTime())
// 指定生成算法及签名
.sign(Algorithm.HMAC256(SING));
return token;
}
// 验证token,返回true或false
public static boolean verify(String token){
try {
com.auth0.jwt.JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
return true;
}catch (Exception e){
return false;
}
}
// 验证token,正确通过,否则抛出异常
public static DecodedJWT verifyToken(String lawyerToken){
return com.auth0.jwt.JWT.require(Algorithm.HMAC256(SING)).build().verify(lawyerToken);
}
// 从token中获取用户id
public static int getTokenId(String token){
DecodedJWT verify = com.auth0.jwt.JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
return verify.getClaim("Id").asInt();
}
}

View File

@ -0,0 +1,72 @@
package com.yzdx.AiInterviewer.utiles;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* redis工具类
*/
@Component
public class RedisUtil {
@Resource
private RedisTemplate<String, Object> redisTemplate;
// 指定缓存失效时间
public Boolean expire(final String key, final long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (final Exception e) {
e.printStackTrace();
return false;
}
}
// 根据键获取值
public Object get(final String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
// <key, value>键值对存入redis
public Boolean set(final String key, final Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (final Exception e) {
e.printStackTrace();
return false;
}
}
// 将键值对存入value并设置过期时间
public Boolean set(final String key, final Object value, final long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (final Exception e) {
e.printStackTrace();
return false;
}
}
// 删除键
public Boolean del(final String key) {
try {
redisTemplate.opsForValue().getAndDelete(key);
return true;
} catch (final Exception e) {
e.printStackTrace();
return false;
}
}
}

View File

@ -1,13 +0,0 @@
spring.datasource.url=jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*.xml
user.address.max-count=20
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB>˲<EFBFBD><CBB2><EFBFBD>ӦΪnull<6C><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
spring.jackson.default-property-inclusion=NON_NULL
##server.servlet.context-path=/store
spring.servlet.multipart.maxFileSize=10MB
spring.servlet.multipart.maxRequestSize=10MB
server.port=80

View File

@ -0,0 +1,24 @@
server:
port: 80
spring:
datasource:
url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
jackson:
default-property-inclusion: non_null
mvc:
pathmatch:
matching-strategy: ant_path_matcher
#dev //说明现在是开发环境
profiles:
active: dev
redis:
database: 11
host: localhost
port: 6379

View File

@ -1,13 +0,0 @@
spring.datasource.url=jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*.xml
user.address.max-count=20
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB>˲<EFBFBD><CBB2><EFBFBD>ӦΪnull<6C><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
spring.jackson.default-property-inclusion=NON_NULL
##server.servlet.context-path=/store
spring.servlet.multipart.maxFileSize=10MB
spring.servlet.multipart.maxRequestSize=10MB
server.port=80