Skip to content

Commit 0545c83

Browse files
committed
security:移除硬编码,使用env存储数据库等信息,增加.env.example示例环境配置文件
1 parent 07e39e9 commit 0545c83

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

DjangoUserService/.env.example

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 环境配置
2+
3+
# JWT 配置
4+
JWT_SECRET_KEY=YOUR_JWT_SECRET_KEY
5+
6+
# 数据库配置
7+
DB_PORT=YOUR_DB_PORT
8+
DB_NAME=YOUR_DB_NAME
9+
DB_USER=YOUR_DB_USER
10+
DB_PASSWORD=YOUR_DB_PASSWORD
11+
DB_HOST=YOUR_DB_HOST
12+
13+
# Celery 配置
14+
CELERY_BROKER_URL=YOUR_CELERY_BROKER_URL
15+
CELERY_RESULT_BACKEND=YOUR_CELERY_RESULT_BACKEND
16+
CELERY_TASK_TIME_LIMIT=YOUR_CELERY_TASK_TIME_LIMIT
17+
CELERY_TASK_SOFT_TIME_LIMIT=YOUR_CELERY_TASK_SOFT_TIME_LIMIT
18+
CELERY_RESULT_EXPIRES=YOUR_CELERY_RESULT_EXPIRES
19+
20+
# redis 配置
21+
REDIS_CACHE_URL=YOUR_REDIS_CACHE_URL

DjangoUserService/DjangoUserService/settings.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
For the full list of settings and their values, see
1010
https://docs.djangoproject.com/en/5.2/ref/settings/
1111
"""
12-
12+
import os
1313
from pathlib import Path
1414

1515
import pymysql
16+
from dotenv import load_dotenv
17+
1618
pymysql.install_as_MySQLdb()
1719

18-
from celery import Celery
20+
load_dotenv()
1921

2022
# Build paths inside the project like this: BASE_DIR / 'subdir'.
2123
BASE_DIR = Path(__file__).resolve().parent.parent
@@ -25,7 +27,7 @@
2527
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
2628

2729
# SECURITY WARNING: keep the secret key used in production secret!
28-
SECRET_KEY = 'MY_JWT_SECRET_KWY_FOR_USR_AND_I_JUST_WRITE_THIS'
30+
SECRET_KEY = os.getenv('JWT_SECRET_KEY')
2931

3032
# SECURITY WARNING: don't run with debug turned on in production!
3133
DEBUG = True
@@ -80,7 +82,6 @@
8082

8183
WSGI_APPLICATION = 'DjangoUserService.wsgi.application'
8284

83-
# settings.py
8485
# settings.py
8586
SWAGGER_SETTINGS = {
8687
'USE_SESSION_AUTH': False,
@@ -100,11 +101,11 @@
100101
DATABASES = {
101102
'default': {
102103
'ENGINE': 'django.db.backends.mysql',
103-
'NAME': 'django_user_service',
104-
'USER': 'root',
105-
'PASSWORD': '060517',
106-
'HOST': 'localhost',
107-
'PORT': '3306',
104+
'NAME': os.getenv('DB_NAME'),
105+
'USER': os.getenv('DB_USER'),
106+
'PASSWORD': os.getenv('DB_PASSWORD'),
107+
'HOST': os.getenv('DB_HOST'),
108+
'PORT': os.getenv('DB_PORT'),
108109
'OPTIONS': {
109110
'charset': 'utf8mb4',
110111
'use_unicode': True,
@@ -114,8 +115,8 @@
114115
}
115116

116117
# Celery 配置
117-
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/1'
118-
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/2'
118+
CELERY_BROKER_URL = os.getenv('CELERY_BROKER_URL')
119+
CELERY_RESULT_BACKEND = os.getenv('CELERY_RESULT_BACKEND')
119120
# 统一时区设置
120121
CELERY_TIMEZONE = 'Asia/Shanghai'
121122
# 启用UTC
@@ -145,7 +146,7 @@
145146
CACHES = {
146147
'default': {
147148
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
148-
'LOCATION': 'redis://127.0.0.1:6379/3',
149+
'LOCATION': os.getenv('REDIS_CACHE_URL'),
149150
}
150151
}
151152

0 commit comments

Comments
 (0)