Java学习之Mybatis-plus与Security的简单整合

本文介绍了如何在Java项目中整合Mybatis-plus和Spring Security,基于Session实现用户认证。步骤包括:导入相关依赖,配置Mybatis-plus,配置Security,自定义验证码生成与验证,以及前后端JSON交互的登陆过滤器设置。通过这个教程,读者可以掌握如何在实际项目中进行这两种技术的集成。
摘要由CSDN通过智能技术生成

Mybatis-plus与Security的简单整合(基于Session)


1.文件目录

文件目录


(1)导入依赖

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.oldbai</groupId>
    <artifactId>my-mp-security</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-mp-security</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--        security-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!--添加MyBatis Plus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!--        逆向工程-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.1.tmp</version>
        </dependency>
        <!--        逆向工程的模板  默认的velocity-->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

(2)Mybatis-Plus的简单配制

  • MyBatisPlusConfig
package com.oldbai.config;

import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

//Version配置类
@Configuration
@EnableTransactionManagement
@MapperScan("com.oldbai.mapper")
public class MyBatisPlusConfig {
   
//    Version配置类 乐观锁
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
   
        return new OptimisticLockerInterceptor();
    }
//    分页查询配制
    @Bean
    public PaginationInterceptor paginationInterceptor(){
   
        return new PaginationInterceptor();
    }
}

  • 其他生成类使用逆向工程生成即可
  • 下面放出主要代码
  • User(注意,实现了UserDetails接口)
package com.oldbai.entity;

import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class User implements Serializable , UserDetails {
   

    private static final long serialVersionUID=1L;

      private Integer id;

    private String username;

    private String password;

    private String nickname;

    private Integer enabled;

    private String email;

    private String userface;

    @TableField("regTime")
    private LocalDateTime regTime;

    private String role;


    @Override
    //返回用户的角色信息
    public Collection<? extends GrantedAuthority> getAuthorities() {
   
        List<SimpleGrantedAuthority> authorities =new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority(role));
        return authorities;
    }

    /**
     *
     * 如果用户未锁定,则返回<code>true</code>,否则返回false
     *
     */
    @Override
    //账号没有过期
    public boolean isAccountNonExpired() {
   
        return true;
    }

    @Override
    //没有被锁定
    public boolean isAccountNonLocked() {
   
        return true;
    }

    @Override
    //密码没有过期
    public boolean isCredentialsNonExpired() {
   
        return true;
    }

    @Override
    //是否可用
    public boolean isEnabled() {
   
        return true;
    }
}

  • UserService(也就是UserServiceImpl)
  • 注意实现了UserDetailsService接口
package com.oldbai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oldbai.entity.User;
import com.oldbai.mapper.UserMapper;
import com.oldbai.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereoty
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值