视图层
from rest_framework_simplejwt.tokens import AccessToken
from django.middleware.csrf import get_token
from django.contrib.auth import login
class JwtToSessionView(GenericViewSet):
@action(methods=['GET'], detail=False)
def set(self, request, *args, **kwargs):
token = request.GET.get('token')
if not token:
return APIResponse(code=102, msg='token不正确')
access_token = AccessToken(token)
user_id = access_token['user_id']
user = User.objects.get(id=user_id)
login(request, user)
csrf_token = get_token(request)
sessionid = request.session.session_key
return APIResponse(data={'sessionid': sessionid, 'csrftoken': csrf_token})
前端获取sessionid与csrftoken时,将其存入cookie中
Cookies.set('sessionid', response.data.data.sessionid);
Cookies.set('csrftoken', response.data.data.csrftoken);
window.open(BackendUrl) ;