本系列旨在通過實戰項目演示 Spring Boot + Shiro 的權限控制體系。第一章專注於「約定優於配置」理念下,利用 Spring Boot Starter 极速構建一個可運行的 Web MVC 基礎項目,為後續整合 Shiro、數據庫、前端頁面打下堅實基礎。
pom.xml)核心策略:引入 `spring-boot-starter-web` (含 MVC + Tomcat),`spring-boot-starter-test`,`lombok` 簡化代碼。
<project xmlns="https://maven.apache.org/POM/4.0.0" ...>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version> <!-- 統一版本管理 -->
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-shiro-chapter01</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>SpringBoot Shiro Chapter01</name>
<properties>
<java.version>17</java.version>
<lombok.version>1.18.30</lombok.version>
</properties>
<dependencies>
<!-- 核心 Web MVC Starter (含 Spring MVC, Tomcat, Jackson) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Lombok: 獲取器/構造器/日誌注解 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- 測試 Starter -->
<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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml)採用 YAML 格式,配置端口、日誌級別、靜態資源映射等。
server:
port: 8080
servlet:
context-path: /shiro-demo
logging:
level:
root: INFO
com.example: DEBUG
org.springframework.web: DEBUG
spring:
mvc:
static-path-pattern: /static/**
web:
resources:
static-locations: classpath:/static/,classpath:/public/
# 模板引擎 (Thymeleaf 示例,後續 Shiro 標簽需模板引擎支持)
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML
encoding: UTF-8
cache: false # 開發關閉緩存
@SpringBootApplication
// @MapperScan("com.example.mapper") // 後續整合 MyBatis 時啟用
public class Chapter01Application {
public static void main(String[] args) {
SpringApplication.run(Chapter01Application.class, args);
}
}
@Configuration
@EnableWebMvc // 全面接管 MVC 配置 (可選,不加則為自動配置增強模式)
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 靜態資源映射
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("/favicon.ico")
.addResourceLocations("classpath:/static/favicon.ico");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// 無 Controller 直接跳轉頁面
registry.addViewController("/").setViewName("forward:/static/index.html");
registry.addViewController("/login").setViewName("login");
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
// Spring Boot 3.0+ 默認不再支持矩陣變量,如需兼容可配置
configurer.setPatternParser(null);
}
}
@RestController // @Controller + @ResponseBody
@RequestMapping("/api")
@Slf4j // Lombok 日誌
public class Chapter01Controller {
@Autowired
private HelloService helloService; // 業務服務注入
@GetMapping("/hello")
public Result hello() {
log.info("訪問 /api/hello");
return Result.success(helloService.getMessage());
}
@GetMapping("/health")
public Result health() {
return Result.success("UP - " + LocalDateTime.now());
}
}
public interface HelloService {
String getMessage();
}
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String getMessage() {
return "Hello Spring Boot 3 + Shiro Series!";
}
}
@Data @AllArgsConstructor @NoArgsConstructor @Builder public class Result{ private Integer code; private String message; private T data; public static Result success(T data) { return Result. builder().code(200).message("SUCCESS").data(data).build(); } public static Result error(String msg) { return Result. builder().code(500).message(msg).build(); } }
Tomcat started on port(s): 8080 (http)。https://localhost:8080/shiro-demo/api/hello → 返回 JSON: {"code":200,"message":"SUCCESS","data":"Hello Spring Boot 3 + Shiro Series!"}https://localhost:8080/shiro-demo/api/health → 健康檢查端點。src/main/java/com/example
├── Chapter01Application.java # 啟動類
├── config
│ └── WebMvcConfig.java # MVC 配置
├── controller
│ └── Chapter01Controller.java # 控制器
├── service
│ ├── HelloService.java # 接口
│ └── impl
│ └── HelloServiceImpl.java # 實現
├── common
│ └── Result.java # 統一響應
└── util # 工具類
src/main/resources
├── application.yml # 配置文件
├── static/ # 靜態資源 (JS, CSS, 圖片)
│ └── index.html
└── templates/ # 模板文件 (Thymeleaf)
└── login.html
本章完成了 Spring Boot 3.x Web MVC 基礎環境的「零配置」搭建,關鍵點:
下章預告:引入 MyBatis-Plus 整合 MySQL,完成實體類、Mapper、Service 的 CRUD 基礎設施,為 Shiro 權限模型(User/Role/Permission)建表準備數據層。