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随机生成的
相关推荐
北巷!!3 天前
SpringSecurity源码中核心类
源码·springsecurity
ghx123456ghx24 天前
springsecurity使用
springsecurity
小马爱打代码1 个月前
SpringSecurity框架核心组件详解
springsecurity
沿途欣赏i2 个月前
SpringSecurity剖析
springsecurity
筱顾大牛3 个月前
SpringSecurity实现登录功能实战!!!
spring boot·intellij-idea·登录·springsecurity
朝暮7823 个月前
SpringSecurity入门
spring boot·spring·springsecurity
Black_Cat_yyds4 个月前
SpringSecurity--DelegatingFilterProxy工作流程
filter·springsecurity
天启A4 个月前
微服务集成springsecurity实现认证
springsecurity·软件工程日报
冯宝宝^4 个月前
SpringSecurity通用权限管理系统
vue.js·人工智能·spring boot·spring·springsecurity
Black_Cat_yyds4 个月前
会话固定攻击
运维·安全·springsecurity