SpringSecurity的默认登录页的使用
01 前期准备
- 引入依赖
<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>
- 配置系统文件
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/(需要连接的数据库)?userSSL=false;serverTimezone=Asia/Shanghaiusername: (账号)password: (密码)mvc:pathmatch:matching-strategy: ant_path_matcher
mybatis-plus:config-locations: classpath:mapper/*.xmlconfiguration: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 {@TableIdprivate Long categoryId;private String categoryName;private String categoryPicture1;private String categoryPicture2;}
- 测试用的控制层接口
@RestController
@RequestMapping("category")
public class CategoryController {@Autowiredprivate 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随机生成的