Skip to content

Commit b7d5f40

Browse files
authored
Enable login interface captcha function (fastapi-practices#165)
* enable captcha login * Delete menu meta column * Update the login interface rate limit
1 parent bf7943d commit b7d5f40

4 files changed

Lines changed: 8 additions & 9 deletions

File tree

backend/app/api/v1/auth/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async def swagger_user_login(form_data: OAuth2PasswordRequestForm = Depends()) -
2626
'/login',
2727
summary='用户登录',
2828
description='json 格式登录, 仅支持在第三方api工具调试接口, 例如: postman',
29-
dependencies=[Depends(RateLimiter(times=5, minutes=15))],
29+
dependencies=[Depends(RateLimiter(times=5, minutes=1))],
3030
)
3131
async def user_login(request: Request, obj: AuthLogin, background_tasks: BackgroundTasks):
3232
access_token, refresh_token, access_expire, refresh_expire, user = await AuthService().login(

backend/app/models/sys_menu.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class Menu(Base):
2222
icon: Mapped[str | None] = mapped_column(String(100), default=None, comment='菜单图标')
2323
path: Mapped[str | None] = mapped_column(String(200), default=None, comment='路由地址')
2424
menu_type: Mapped[int] = mapped_column(default=0, comment='菜单类型(0目录 1菜单 2按钮)')
25-
meta: Mapped[dict | None] = mapped_column(JSON, default=None, comment='菜单元数据')
2625
component: Mapped[str | None] = mapped_column(String(255), default=None, comment='组件路径')
2726
perms: Mapped[str | None] = mapped_column(String(100), default=None, comment='权限标识')
2827
status: Mapped[int] = mapped_column(default=1, comment='菜单状态(0停用 1正常)')

backend/app/schemas/user.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ class Auth(SchemaBase):
1717

1818

1919
class AuthLogin(Auth):
20-
# captcha: str
21-
pass
20+
captcha: str
2221

2322

2423
class CreateUser(Auth):

backend/app/services/auth_service.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from backend.app.common.exception import errors
1313
from backend.app.common.jwt import get_token
1414
from backend.app.common.redis import redis_client
15+
from backend.app.common.response.response_code import CustomCode
1516
from backend.app.core.conf import settings
1617
from backend.app.crud.crud_user import UserDao
1718
from backend.app.database.db_mysql import async_db_session
@@ -50,11 +51,11 @@ async def login(self, *, request: Request, obj: AuthLogin, background_tasks: Bac
5051
raise errors.AuthorizationError(msg='密码错误')
5152
elif not current_user.status:
5253
raise errors.AuthorizationError(msg='用户已锁定, 登陆失败')
53-
# captcha_code = await redis_client.get(f'{settings.CAPTCHA_LOGIN_REDIS_PREFIX}:{request.state.ip}')
54-
# if not captcha_code:
55-
# raise errors.AuthorizationError(msg='验证码失效,请重新获取')
56-
# if captcha_code.lower() != obj.captcha.lower():
57-
# raise errors.CustomError(error=CustomCode.CAPTCHA_ERROR)
54+
captcha_code = await redis_client.get(f'{settings.CAPTCHA_LOGIN_REDIS_PREFIX}:{request.state.ip}')
55+
if not captcha_code:
56+
raise errors.AuthorizationError(msg='验证码失效,请重新获取')
57+
if captcha_code.lower() != obj.captcha.lower():
58+
raise errors.CustomError(error=CustomCode.CAPTCHA_ERROR)
5859
await UserDao.update_login_time(db, obj.username, self.login_time)
5960
user = await UserDao.get(db, current_user.id)
6061
access_token, access_token_expire_time = await jwt.create_access_token(

0 commit comments

Comments
 (0)