后端项目开发笔记

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/**");
      }

    }

相关推荐
齐生13 天前
iOS 知识点 - 渲染机制、动画、卡顿小集合
笔记
用户962377954483 天前
VulnHub DC-1 靶机渗透测试笔记
笔记·测试
齐生15 天前
iOS 知识点 - IAP 是怎样的?
笔记
tingshuo29175 天前
D006 【模板】并查集
笔记
tingshuo29176 天前
S001 【模板】从前缀函数到KMP应用 字符串匹配 字符串周期
笔记
西岸行者11 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
starlaky11 天前
Django入门笔记
笔记·django
勇气要爆发11 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
悠哉悠哉愿意11 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
勇气要爆发11 天前
吴恩达《LangChain LLM 应用开发精读笔记》2-Models, Prompts and Parsers 模型、提示和解析器
android·笔记·langchain