【发布时间】:2018-05-04 09:47:41
【问题描述】:
从 Angular 向我的 django 后端发送请求会返回未经授权的 401。这是注销功能的http请求。
import { Injectable } from '@angular/core';
import { HttpClient,HttpHeaders } from '@angular/common/http';
import { RequestOptions } from '@angular/http';
import { authLoginUrl,authLogoutUrl } from '../../../../config/endpoints';
import 'rxjs/add/operator/map';
import { AlertService } from '../../../../../core/services/alert.service';
@Injectable()
export class LoginService{
public token: string;
constructor(private http: HttpClient) {
// set token if saved in local storage
var currentUser = JSON.parse(localStorage.getItem('currentUser'));
this.token = currentUser && currentUser.token;
}
logout(): void {
// notify backend of user logout
//authLogoutUrl = "http://127.0.0.1:8000/api/auth/logout/"
this.http.post(authLogoutUrl,{
headers: new HttpHeaders().set('Authorization', 'JWT ' + this.token)
})
.subscribe()
}
}
但是,当我通过 curl 发送请求时,请求被授权。
curl -X POST -H "Authorization: JWT <the_token>" http://localhost:8000/api/auth/logout/
注销视图在我的 django 后端:
class LogoutView(views.APIView):
permission_classes = (permissions.IsAuthenticated,)
def post(self, request, format=None):
logout(request)
return Response({}, status=status.HTTP_204_NO_CONTENT)
一切似乎都运行良好。预检请求返回 200,但请求本身是未经授权的。这是请求标头
Cors 设置 django 休息:
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_HEADERS = (
'accept',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
)
#Rest Framework
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
'DEFAULT_PAGINATION_CLASS':
'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE':100,
}
由于它适用于 curl 并且预检请求已获批准,我只能假设问题出在 angular 或 cors 上。
1) 标题设置是否正确? 2)这是一个cors问题吗?
【问题讨论】:
标签: angular http django-rest-framework django-cors-headers