SpringSecurity的默认登录页的使用

SpringSecurity的默认登录页的使用

01 前期准备

  • 引入依赖
xml 复制代码
	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		<!--mysql驱动-->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </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>
        <!--mybatisplus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
		<!--spring-security依赖-->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
  • 配置系统文件
yml 复制代码
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/(需要连接的数据库)?userSSL=false;serverTimezone=Asia/Shanghai
    username: (账号)
    password: (密码)
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
mybatis-plus:
  config-locations: classpath:mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  • 配置扫描包
Java 复制代码
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

02 编写测试接口

  • 测试类
java 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Category {

  @TableId
  private Long categoryId;
  private String categoryName;
  private String categoryPicture1;
  private String categoryPicture2;


}
  • 测试用的控制层接口
java 复制代码
@RestController
@RequestMapping("category")
public class CategoryController {
     @Autowired
     private ICategoryService iCategoryService;

    @GetMapping("all")
    public GetData selectAll(){
        List<Category> categories=iCategoryService.list();
        GetData getData = new GetData(200,"查询成功",categories);
        return getData;
    }

    @GetMapping("byId")
    public GetData selectDetail(Long id){
        Category category=iCategoryService.getById(id);
        GetData getData = new GetData(200,"查询成功",category);
        return getData;
    }
}

03 启动项目

  • 启动之后,会自动生成默认密码

  • Using generated security password: 8d97e198-138c-4093-9a5c-ac83e2dda426

  • 这时候访问接口被spring-security默认拦截,必须登录后才能访问

  • 默认界面的账号默认user,密码是Using generated security password随机生成的
相关推荐
佛祖让我来巡山21 天前
小明网站双登录系统实现——微信授权登录+用户名密码登录完整指南
oauth2·springsecurity·微信授权登录
佛祖让我来巡山22 天前
Spring Security 鉴权流程与过滤器链深度剖析
springsecurity·authenticationmanager
佛祖让我来巡山22 天前
大型项目基于Spring Security的登录鉴权与数据权限控制完整方案
springsecurity·保姆级鉴权·大型项目登录认证
佛祖让我来巡山22 天前
Spring Security前后端分离接入流程保姆级教程
权限校验·springsecurity·登录认证
佛祖让我来巡山23 天前
Spring Security 认证流程闭环与调用链路详解
springsecurity·authenticationmanager
佛祖让我来巡山23 天前
小明的Spring Security入门到深入实战
springsecurity
佛祖让我来巡山24 天前
⚠️登录认证功能的成长过程:整体概述
安全·登录·springsecurity·登录认证·认证授权
码熔burning3 个月前
Spring Security 深度学习(六): RESTful API 安全与 JWT
安全·spring·restful·springsecurity
A尘埃3 个月前
SpringSecurity版本的不同配置
认证·springsecurity·安全配置·不同版本
世纪摆渡人5 个月前
SpringSecurity-SpringSecurity入门介绍
springsecurity