当前位置: 首页 > 图灵资讯 > 技术篇> 学习aop以及nginx

学习aop以及nginx

来源:图灵教育
时间:2023-09-01 15:25:16

天空外卖的第一天 构建前端和后端环境,首先将代码提交到本地 然后在git上创建仓库 https://gitee.com/lh03062021/sky-take-out.点击pushgit 定义远程仓库 把复制好的粘贴上去 你可以推动成功。导入sql文件,编译家庭项目

  1. nginx反向代理将前端发送的动态请求从nginx转发到后端服务器:负载平衡,提高访问速度,确保后端服务的安全image.pngimage.png

2.完善登录功能,修改明文密码,改为md5加密(不能解密)

3.使用swagererimage.png2.添加配置类3.设置静态资源映射,否则接口文档页面无法访问packagee 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@Slf4jpublic class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowiredprivate JwtTokenAdminInterceptor jwtTokenAdminInterceptor;/**

    • 接口文档通过knife4j生成
    • @return*/@Beanpublic 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*/@Overrideprotected void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}}

image.png

## - 32集insertid常用于自定义注释开发,inserttime,updateid,updatetime

  1. 创建枚举数据 insert或update
/** * 数据库操作类型 */public enum OperationType {    /**     * 更新操作     */    UPDATE,    /**     * 插入操作     */    INSERT}

@Target:描述注释的使用范围@Rention:注释信息需要保存在什么级别,以描述注释的生命周期。(source<class<runtime)2.创建注释界面类别

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface AutoFill { ///只有数据库更新和插入操作    OperationType value();}

3.创建aspectttaspecttt

package com.sky.aspect;import com.sky.anocation.AutoFill;import com.sky.constant.AutoFillConstant;import com.sky.context.BaseContext;import com.sky.entity.Employee;import com.sky.enumeration.OperationType;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.Signature;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;import java.lang.annotation.Annotation;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.text.SimpleDateFormat;import java.util.Date;/** * @author DHC */@Aspect@Component@Slf4jpublic class AutoFillAspect {    ///定义切点    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.anocation.AutoFill)")    public void PointCut() {    }    @Before("PointCut()")    public void autoFill(JoinPoint joinPoint) {        log.info("开始填充公共字段...");        MethodSignature signature = (MethodSignature) joinPoint.getSignature();        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);        OperationType operationType = autoFill.value(); ///获取数据库操作类型        Object[] args = joinPoint.getArgs();        if (args == null || args.length==0) {          return ;        }        Object entity = args[0];        Long currentId= BaseContext.getCurrentId();        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date a =new Date();        String s = simpleDateFormat.format(a);        if(operationType == OperationType.INSERT){            try {                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, String.class);                Method insertId = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);                Method updateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, String.class);                Method updateId = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);                setCreateTime.invoke(entity,s);                insertId.invoke(entity,currentId);                updateTime.invoke(entity,s);                updateId.invoke(entity,currentId);            } catch (Exception e) {                e.printStackTrace();            }        }else if(operationType == OperationType.UPDATE){            try {                Method updateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, String.class);                Method updateId = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);                updateTime.invoke(entity,s);                updateId.invoke(entity,currentId);            } catch (Exception e) {                e.printStackTrace();            }        }    }}