Springboot项目

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

引入Mybatis依赖

 <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

引入Mybatis-plus的依赖

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>

引入yaml配置文件

数据库的配置和mybatis的配置

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文件夹

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

 

@MapperScan("com.example.demo2022.mapper")
@Configuration
public class MybatisPlusConfig {
    // 最新版
    @Bean  // <bean id=""/>
    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的依赖

 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.35</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

除了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<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername())
                .eq("password",user.getPassword());
        User userDb = userService.getOne(queryWrapper);
        if(userDb != null){
            // 获取用户所有的权限信息
            Set<String> 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();
    }

}

登录拦截器

登录接口

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

登录拦截器 

首先获取当前访问的路径 

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

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

 


然后是权限拦截器

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

需要用token获取用户实体类

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

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

 


拦截器完之后是aop

把操作日志存到数据里

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

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

3.接口路径

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

5.出参

6.操作时间

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

 操作日志实体类

 

全局变量

 

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值