认证接口
DNS-Go 使用 JWT(JSON Web Token)进行用户认证。
登录
用户登录获取访问令牌。
请求
http
POST /api/auth/login
Content-Type: application/json请求体:
json
{
"username": "admin",
"password": "admin123"
}参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| username | string | ✅ | 用户名 |
| password | string | ✅ | 密码 |
响应
成功响应(200):
json
{
"code": 200,
"message": "登录成功",
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"expires_in": 86400,
"user": {
"id": 1,
"username": "admin",
"nickname": "管理员",
"role": "super_admin",
"email": "admin@example.com",
"avatar": ""
}
}
}错误响应:
json
// 用户名或密码错误(401)
{
"code": 401,
"message": "用户名或密码错误"
}
// 账号被锁定(403)
{
"code": 403,
"message": "账号已被锁定,请15分钟后再试"
}登出
用户登出,使当前 Token 失效。
请求
http
POST /api/auth/logout
Authorization: Bearer {token}响应
成功响应(200):
json
{
"code": 200,
"message": "登出成功"
}刷新 Token
刷新访问令牌,延长登录时间。
请求
http
POST /api/auth/refresh
Authorization: Bearer {token}响应
成功响应(200):
json
{
"code": 200,
"message": "刷新成功",
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"expires_in": 86400
}
}错误响应(401):
json
{
"code": 401,
"message": "Token 已过期"
}获取当前用户信息
获取当前登录用户的详细信息。
请求
http
GET /api/user/info
Authorization: Bearer {token}响应
成功响应(200):
json
{
"code": 200,
"data": {
"id": 1,
"username": "admin",
"nickname": "管理员",
"role": "super_admin",
"email": "admin@example.com",
"phone": "",
"avatar": "",
"status": "enabled",
"last_login": "2025-01-15T10:30:00Z",
"created_at": "2025-01-01T00:00:00Z"
}
}修改密码
修改当前用户的密码。
请求
http
PUT /api/user/password
Authorization: Bearer {token}
Content-Type: application/json请求体:
json
{
"old_password": "oldpass123",
"new_password": "newpass123"
}参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| old_password | string | ✅ | 原密码 |
| new_password | string | ✅ | 新密码(至少6位) |
响应
成功响应(200):
json
{
"code": 200,
"message": "密码修改成功"
}错误响应(400):
json
{
"code": 400,
"message": "原密码错误"
}更新个人资料
更新当前用户的个人资料。
请求
http
PUT /api/user/profile
Authorization: Bearer {token}
Content-Type: application/json请求体:
json
{
"nickname": "新昵称",
"email": "newemail@example.com",
"phone": "13800138000",
"avatar": "https://example.com/avatar.jpg"
}参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| nickname | string | ❌ | 昵称 |
| string | ❌ | 邮箱 | |
| phone | string | ❌ | 手机号 |
| avatar | string | ❌ | 头像 URL |
响应
成功响应(200):
json
{
"code": 200,
"message": "更新成功",
"data": {
"id": 1,
"nickname": "新昵称",
"email": "newemail@example.com",
"phone": "13800138000",
"avatar": "https://example.com/avatar.jpg"
}
}使用示例
cURL 示例
bash
# 登录
TOKEN=$(curl -s -X POST http://localhost:8085/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin123"}' \
| jq -r '.data.token')
# 使用 Token 访问其他接口
curl -s http://localhost:8085/api/user/info \
-H "Authorization: Bearer $TOKEN"
# 刷新 Token
NEW_TOKEN=$(curl -s -X POST http://localhost:8085/api/auth/refresh \
-H "Authorization: Bearer $TOKEN" \
| jq -r '.data.token')
# 登出
curl -s -X POST http://localhost:8085/api/auth/logout \
-H "Authorization: Bearer $NEW_TOKEN"JavaScript 示例
javascript
// 登录
const login = async (username, password) => {
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
const data = await response.json();
if (data.code === 200) {
localStorage.setItem('token', data.data.token);
return data.data.user;
}
throw new Error(data.message);
};
// 带认证的请求
const fetchWithAuth = async (url, options = {}) => {
const token = localStorage.getItem('token');
const response = await fetch(url, {
...options,
headers: {
...options.headers,
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
if (response.status === 401) {
// Token 过期,跳转登录
localStorage.removeItem('token');
window.location.href = '/login';
}
return response.json();
};
// 获取用户信息
const getUserInfo = () => fetchWithAuth('/api/user/info');
// 修改密码
const changePassword = (oldPassword, newPassword) =>
fetchWithAuth('/api/user/password', {
method: 'PUT',
body: JSON.stringify({
old_password: oldPassword,
new_password: newPassword
})
});Python 示例
python
import requests
class DNSGoClient:
def __init__(self, base_url='http://localhost:8085'):
self.base_url = base_url
self.token = None
def login(self, username, password):
response = requests.post(
f'{self.base_url}/api/auth/login',
json={'username': username, 'password': password}
)
data = response.json()
if data['code'] == 200:
self.token = data['data']['token']
return data['data']['user']
raise Exception(data['message'])
def _headers(self):
return {'Authorization': f'Bearer {self.token}'}
def get_user_info(self):
response = requests.get(
f'{self.base_url}/api/user/info',
headers=self._headers()
)
return response.json()
def change_password(self, old_password, new_password):
response = requests.put(
f'{self.base_url}/api/user/password',
headers=self._headers(),
json={
'old_password': old_password,
'new_password': new_password
}
)
return response.json()
# 使用示例
client = DNSGoClient()
client.login('admin', 'admin123')
user = client.get_user_info()
print(user)错误码说明
| 错误码 | 说明 | 处理建议 |
|---|---|---|
| 400 | 请求参数错误 | 检查请求参数格式 |
| 401 | 未认证或 Token 过期 | 重新登录获取 Token |
| 403 | 权限不足 | 检查用户角色权限 |
| 404 | 资源不存在 | 检查请求路径和参数 |
| 500 | 服务器内部错误 | 查看服务器日志 |