Springboot项目

Springboot项目_第1张图片

 下一步勾选lombok和web(SpringMVC)--------(引入lombok和SpringMVC依赖)

引入Mybatis依赖

 
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        
        
            mysql
            mysql-connector-java
            runtime
        

引入Mybatis-plus的依赖

        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.2
        

引入yaml配置文件

数据库的配置和mybatis的配置

Springboot项目_第2张图片

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/qcby_db?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 2020
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  typeAliasesPackage: com.example.demo2022.entity
  mapperLocations: classpath:mapper/*.xml
  # 全局配置id自增  =>
  global-config:
    db-config:
      id-type: auto

 创建controller,mapper,service,entity包,还有在resources中的mapper文件夹

Springboot项目_第3张图片

添加一个config的包,添加mybatisplus的分页插件配置,其中添加对mapper类的扫描

 Springboot项目_第4张图片

@MapperScan("com.example.demo2022.mapper")
@Configuration
public class MybatisPlusConfig {
    // 最新版
    @Bean  // 
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

新建通用返回结果类WebResultJson

@Data
public class WebResultJson {
    /**
     *  1 返回成功
     *  0 返回失败
     */
    public final static int OK = 1;
    public final static String OK_MSG = "操作成功!";
    public final static int FAIL = 0;
    public final static String FAIL_MSG = "操作失败!";


    private int code;
    private String msg;
    private Object data;


    private WebResultJson(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static WebResultJson ok(){
        return ok(OK_MSG,null);
    }
    public static WebResultJson ok(String msg){
        return new WebResultJson(OK,msg,null);
    }
    public static WebResultJson ok(Object data){
        return new WebResultJson(OK,OK_MSG,data);
    }
    public static WebResultJson ok(String msg,Object data){
        return new WebResultJson(OK,msg,data);
    }


    public static WebResultJson fail(){
        return fail(FAIL_MSG,null);
    }
    public static WebResultJson fail(String msg){
        return new WebResultJson(FAIL,msg,null);
    }
    public static WebResultJson fail(Object data){
        return new WebResultJson(FAIL,FAIL_MSG,data);
    }
    public static WebResultJson fail(String msg,Object data){
        return new WebResultJson(FAIL,msg,data);
    }
}

操作日志

引入aop和fastjson的依赖

 
            com.alibaba
            fastjson
            1.2.35
        
        
            org.springframework.boot
            spring-boot-starter-aop
        

除了controller ,service , mapper层以外,

拦截器

Interceptor 在 aop 的前面,通过了拦截器 才能到 aop

拦截器分为拦截规则 和 拦截器 

拦截规则制定了拦截哪些

拦截器是在什么样的情况下,拦截器通过

此项目组分为 登录拦截器 和 权限拦截器

UserController

package com.example.demo2022.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo2022.common.web.WebResultJson;
import com.example.demo2022.entity.User;
import com.example.demo2022.service.LoginLogService;
import com.example.demo2022.service.MenuService;
import com.example.demo2022.service.UserService;
import com.example.demo2022.util.TokenUtil;
import com.example.demo2022.vo.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private LoginLogService loginLogService;
    @Autowired
    private MenuService menuService;


    @RequestMapping("add")
    public WebResultJson add(User user){
        return WebResultJson.ok(userService.save(user));
    }

    @RequestMapping("listAll")
    public WebResultJson listAll(){
        return WebResultJson.ok(userService.list());
    }



    @RequestMapping(value = "login",method = {RequestMethod.GET,RequestMethod.POST})
    public WebResultJson login(User user){
        if(StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword()) ){
            return WebResultJson.fail("用户或者密码为空!");
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername())
                .eq("password",user.getPassword());
        User userDb = userService.getOne(queryWrapper);
        if(userDb != null){
            // 获取用户所有的权限信息
            Set menuUrlList = menuService.listUrlByUserId(userDb.getId());
            userDb.setMenuUrlList(menuUrlList);
            // 登陆成功了
            String token = TokenUtil.generateToken(userDb);
            userDb.setToken(token);
            loginLogService.saveLoginLog(userDb.getId());
            return WebResultJson.ok(userDb);
        }else{
            return WebResultJson.fail("用户名或密码错误!");
        }

    }


    /**
     * 更新用户的角色信息: 修改用户和角色的关联表
     *
     * 1、// 典型的多对多关丽表的更新
     * 2、 复杂参数的传递
     *
     * {
     *       "userId":1,
     *      "roleIdList":[2,3,4]
     * }
     */
    @RequestMapping("updateUserRoles")
    public  WebResultJson updateUserRoles(@RequestBody UserRoleVo userRoleVo){
        userService.updateUserRoles(userRoleVo);
        return WebResultJson.ok();
    }

}

登录拦截器

登录接口

Springboot项目_第5张图片

登陆完成之后,其他被限制的接口会被登录拦截器拦截

登录拦截器 

首先获取当前访问的路径 

从请求当前路径的请求头中获取 token (token在登录成功的时候就可以获取到),此时token,user 已经存入map

Springboot项目_第6张图片

通过token调用tokenUtil中的验证token方法,如果token在map中说明登录过了,放行

Springboot项目_第7张图片

 


然后是权限拦截器

需要验证登录的用户实体类中是否包含对应的menuUrl

需要用token获取用户实体类

登录成功之后,通过当前用户id查询对应的权限url,把权限url存入到用户实体类中,把用户实体类通过tokenUtil获取token,并存入map中

同样是把登陆成功拿到的token(token,user已经在tokenUtil中存入map)当作请求头请求接口

Springboot项目_第8张图片

Springboot项目_第9张图片

Springboot项目_第10张图片 


拦截器完之后是aop

把操作日志存到数据里

需要存的变量:1.用户id

2.方法全名(类名+方法名)

3.接口路径

4.入参(参数名+参数)

5.出参

6.操作时间

其中方法全名和入参是通过连接点的反射拿到的

Springboot项目_第11张图片

 操作日志实体类

Springboot项目_第12张图片

 

全局变量

Springboot项目_第13张图片 

 

你可能感兴趣的:(springboot,java)