身份认证与应用安全 / 系统课时
创建认证工程与基础接口
创建认证工程与基础接口
这一节先搭出可运行的 Spring Boot 认证工程,不急着实现 JWT。目标是把认证接口、业务接口和安全配置分开,让后续加入令牌签发与校验时只替换认证细节,而不重写整个 Web 层。
学习目标
- 创建一个包含 Spring MVC 与 Spring Security 的最小 Maven 工程
- 将认证接口、业务接口和安全配置放到明确的代码边界
- 启动应用并用 HTTP 请求验证基础接口可访问
建立最小 Maven 工程
示例使用 Java 17 和 Spring Boot 3.5.0。项目根目录的 pom.xml 如下,Web 与 Security 依赖都交给 Spring Boot 管理版本:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.0</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>auth-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>auth-demo</name>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>这个配置会提供 Spring MVC、嵌入式 Web 服务器和 Spring Security。下一步只需要编写应用入口与三个边界:auth 负责认证相关 HTTP 契约,api 放业务接口,config 集中安全规则。
编写应用入口和两个接口
下面三个 Java 文件都是片段示例,依赖前面的 Maven 工程与对应包路径;它们共同组成可运行应用,统一运行验证放在后面的启动步骤。
应用入口放在根包 com.example.authdemo,这样 Spring Boot 能扫描下面的子包:
package com.example.authdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AuthDemoApplication {
public static void main(String[] args) {
SpringApplication.run(AuthDemoApplication.class, args);
}
}认证接口先只证明请求能够到达认证边界,不在这里伪造 JWT。创建 com.example.authdemo.auth.AuthController:
package com.example.authdemo.auth;
import java.util.Map;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/auth")
public class AuthController {
@PostMapping("/login")
public Map<String, String> login(@RequestBody LoginRequest request) {
// 当前只建立接口契约,真正的密码校验和 JWT 签发留到后续认证实现。
return Map.of(
"username", request.username(),
"message", "login endpoint ready"
);
}
public record LoginRequest(String username, String password) {
}
}业务接口独立放在 com.example.authdemo.api.ProfileController:
package com.example.authdemo.api;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/profile")
public class ProfileController {
@GetMapping
public Map<String, String> profile() {
return Map.of("message", "profile endpoint ready");
}
}把两个控制器分开后,后续登录逻辑可以继续演进,而业务接口不需要知道用户名密码如何校验、JWT 如何生成。
用安全配置保留后续接入点
下面同样是片段示例,依赖 spring-boot-starter-security。只要引入 Spring Security,Web 请求就会进入安全过滤链。当前阶段为了先验证接口骨架,显式允许所有请求;后续接入 JWT 时,再把业务路径改成必须认证。
创建 com.example.authdemo.config.SecurityConfig:
package com.example.authdemo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
// 这是 JSON API 骨架,当前不使用浏览器表单提交,因此先关闭 CSRF。
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/auth/**").permitAll()
.anyRequest().permitAll());
return http.build();
}
}这里故意没有把 /api/profile 标成 authenticated(),因为当前工程还没有 JWT 校验器。先保证基础接口可测试,后续再收紧规则,比一开始同时混入密码校验、令牌过滤器和异常处理更容易定位问题。
启动并验证接口
在项目根目录启动应用:
mvn spring-boot:run预期输出
...
Tomcat started on port 8080 (http)
Started AuthDemoApplication in <TIME> seconds<TIME> 是本机启动耗时,实际值会不同。保持进程运行,再打开另一个终端请求登录接口:
curl -i -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"alice","password":"demo-pass"}'预期结果
HTTP/1.1 200 OK
Content-Type: application/json
{"username":"alice","message":"login endpoint ready"}再验证业务接口:
curl -i http://localhost:8080/api/profile预期结果
HTTP/1.1 200 OK
Content-Type: application/json
{"message":"profile endpoint ready"}两类接口都能返回预期 JSON,就说明工程入口、包扫描、MVC 映射和安全配置边界已经跑通。此时最重要的结构已经固定:认证逻辑留在 auth,业务接口留在 api,访问控制集中在 config,后续加入 JWT 时不会把认证细节散落到每个控制器。