0

我正在使用 Wagtail 2.1、Django 1.11.13、django-auth-ldap 1.6.1 和 gunicorn 19.8.1(Nginx 作为代理服务器)。

我创建了两种不同的登录方式,一个 AJAX 视图:

def loginajax(request):
# Identation is okay, can't seem to paste it properly
if request.method == 'POST':
    login_form = AuthenticationForm(data=request.POST)
    response_data = {}
    if login_form.is_valid():
        response_data['result'] = '1'
        response_data['message'] = 'You"re logged in'
        user = authenticate(username=login_form.cleaned_data['username'],
                            password=login_form.cleaned_data['password'])
        login(request, user)
    else:
        response_data['result'] = '0'
        response_data['message'] = login_form.errors

    return JsonResponse(response_data)

- 一个常规的、非常标准的 Django 登录表单。

{% block content %}
<div class="login-form" style="width: 90%; margin: 0 auto;">
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}

{% if next %}
    {% if user.is_authenticated %}
    <p>Your account doesn't have access to this page. To proceed,
    please login with an account that has access.</p>
    {% else %}
    <p>Please login to see this page.</p>
    {% endif %}
{% endif %}

<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<table>
<tr>
    <td>{{ form.username.label_tag }}</td>
    <td>{{ form.username }}</td>
</tr>
<tr>
    <td>{{ form.password.label_tag }}</td>
    <td>{{ form.password }}</td>
</tr>
</table>

<input type="submit" value="login" />
<input type="hidden" name="next" value="{% if next %}{{ next }}{% endif %}" />
</form>
</div>
{% endblock %}

我正在使用 LDAP,并使用常规身份验证后端作为备份:

AUTHENTICATION_BACKENDS = [
  'django_auth_ldap.backend.LDAPBackend',
  'django.contrib.auth.backends.ModelBackend',
]

在开发模式下运行我的代码时(通过 manage.py runserver),登录对于两种登录方法都可以正常工作(我可以看到正在验证我的用户的 dlango-ldap 调试消息)。

但是,一旦我使用生产就绪模式(使用 gunicorn、禁用调试、切换到生产设置),我所有的登录请求都会收到 LDAPError:

在验证 bvolchok 时捕获 LDAPError:INVALID_CREDENTIALS({'desc': 'Invalid credentials', 'info': '80090308: LdapErr: DSID-0C09042A, comment: AcceptSecurityContext error, data 52e, v3839'},)

对于 Ajax 视图,打印 request.POST 对象返回与 Django 集成服务器和 Gunicorn 相同的结果:

QueryDict: {'csrfmiddlewaretoken': ['redacted'], 'username': ['redacted'], 'password': ['redacted'], 'next': ['']}>

基本上,我的问题是,在“生产模式”下,login_form.is_valid() 总是 False ......

4

1 回答 1

0

愚蠢的我,我将环境变量传递给 django-auth-ldap ,其中一个有一个空格,它的值被截断了。

于 2018-06-13T08:53:27.490 回答