Maven打包与JDK版本不对应解决方法
我这里使用jdk8。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
依赖解析
- 每个SpringBoot项目都需要该父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
</parent>
- web开发需要该依赖,像RestController,GetMapping,WebMvcConfigurer,org.springframework.web都是该依赖的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- mysql驱动,没有这个的话,配置数据库时driver-class-name会爆红
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xxxxx
username: root
password: xxxxxx
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
- 添加该依赖后可以使用@Data等注解
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
- Spring测试依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
- Mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
- 可以生成图形验证码
<!-- 添加图形验证码依赖 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-captcha</artifactId>
<version>5.8.6</version>
</dependency>
- redis依赖:配置后可以操作redis(StringRedisTemplate)
配置信息:
redis:
database: 0
timeout: 5000
host: redis服务器地址
port: 6379
lettuce:
pool:
max-active: 20 #连接池最大连接数(使用负值表示没有限制)
max-wait: -1 #连接池最大阻塞等待时间(使用负值表示没有限制)
min-idle: 0 #连接池中的最大空闲连接
max-idle: 10 #连接池中的最小空闲连接
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- JWT依赖,可以用来生成token
<!---jwt-->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.4.0</version>
</dependency>
- Common IO 是一个工具库,用来帮助开发IO功能
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
拦截器配置
- 先写一个拦截器
@Slf4j
public class JWTInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws MyException {
String token = request.getHeader("Authorization");
if(StringUtils.isEmpty(token)){
response.setStatus(400);
throw new MyException("token不能为空");
}
try {
JWTUtil.verify(token);
} catch (Exception e) {
log.error("token无效! 错误 ", e);
response.setStatus(400);
return false;
}
return true;
}
}
- 配置拦截器
@Configuration
public class IntercaptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new JWTInterceptor())
//拦截的路径
.addPathPatterns("/**")
//排除登录接口和一些静态资源
.excludePathPatterns("/user/login")
.excludePathPatterns("/static/images/**");
}
}