SpringSecurity的默认登录页的使用

SpringSecurity的默认登录页的使用

01 前期准备

  • 引入依赖
	<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
		
        <dependency>
            <groupId>com.mysqlgroupId>
            <artifactId>mysql-connector-jartifactId>
            <scope>runtimescope>
        dependency>
		
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.1version>
        dependency>
		
        <dependency>
            <groupId>org.springframework.securitygroupId>
            <artifactId>spring-security-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-securityartifactId>
        dependency>
  • 配置系统文件
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
  • 配置扫描包
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

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

}

02 编写测试接口

  • 测试类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Category {

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


}
  • 测试用的控制层接口
@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 启动项目

  • 启动之后,会自动生成默认密码
    SpringSecurity的默认登录页的使用_第1张图片

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

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

SpringSecurity的默认登录页的使用_第2张图片

  • 默认界面的账号默认user,密码是Using generated security password随机生成的

你可能感兴趣的:(SpringSecurity,SpringSecurity)