SpringBoot+MybatisPlus+SpringSecurity小案例

导入所需的依赖 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.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.pan</groupId>
    <artifactId>springboot_SpringSecurity</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_SpringSecurity</name>
    <description>springboot_SpringSecurity</description>


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


    <dependencies>
        <!--spring-security-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!--springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--mybatis plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

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

</project>
1、连接数据库、
server:
  port: 8011
spring:
  #配置链接数据库
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
#    driver-class-name: com.mysql.cj.jdbc.Driver
2、创建一个测试数据的表
CREATE TABLE `admin_user`
(
    `id`       int         NOT NULL AUTO_INCREMENT,
    `username` varchar(32) NOT NULL,
    `password` varchar(32) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb3;
3、编写相关的实体类

@Data
public class AdminUser {
    private Integer id;
    private String username;
    private String password;
}
4、编写mapper接口继承BaseMapper

@Mapper
@Repository
public interface UserMapper extends BaseMapper<AdminUser> {
}
5、编写配置类

@Configuration
public class SecurityConfigTest extends WebSecurityConfigurerAdapter {
    @Resource
    private UserDetailsService userDetailsService;

    @Bean
        // 不注入该对象会报错 There is no PasswordEncoder mapped for the id "null"
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }
}
6、编写业务层的方法

@Service("userDetailsService")
public class MyUserService implements UserDetailsService {
    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        //调用MP中的方法查询数据库  根据用户名查询
        //创建一个wrapper查询对象
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        // where username = ?
        queryWrapper.eq("userName", userName);
        AdminUser user = userMapper.selectOne(queryWrapper);
        //判断数据库是否存在该用户 不存在抛出异常
        if (user == null) {
            throw new UsernameNotFoundException("该用户不存在");
        }
        //创建一个权限集合 模拟数据库操作
        List<GrantedAuthority> admin = AuthorityUtils.commaSeparatedStringToAuthorityList("ADMIN");
        //从查询的数据库对象里得到账号密码并返回
        return new User(user.getUsername(), new BCryptPasswordEncoder().encode(user.getPassword()), admin);
    }
}
7、编写控制器

@RestController
@RequestMapping("test")
public class TestController {
    @RequestMapping("hello")
    public String helloSecurity() {
        return "hello security";
    }
}

8、访问控制器测试即可

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值