项目初始化引入SpringSecurity

2022-09-05 SpringSecurity

本系列文章讲述如何在项目中快速集成SpringSecurity,所以最开始都不用连接数据库,用ArrayList代替数据库的功能。后面会有专门的文章配合RBAC权限架构集成SpringSecurity实现鉴权,接下来我们先来开始准备工作。

你不需要和我的依赖一样,只需要银日SpringBoot和security的依赖即可,我的依赖只是作为参考,核心是SpringSecurity的依赖。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

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

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

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

编写一个测试接口,确定项目可以正常运行。

@RestController
public class TestController {

    @GetMapping
    public String test() {
        return "Hello World";
    }
}

通过ip地址加上端口号我们发现出现了SpringSecurity默认的拦截页面,账号是user密码在控制台会打印。

image-20220904193059375

创建一个User类,用于登录认证。

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private String id;

    private String username;

    private String password;

    private String nickName;
}
上次更新: 5 个月前