Calling a custom middleware after Authentication Middleware
up vote
2
down vote
favorite
In django REST framework authentication middleware sets the user object in request ONLY after the views middleware is executed while any custom middleware is executed before that. is there someway to change this order and execute custom middleware AFTER user object is set by authentication middleware
As an alternative I create the user object in the middleware itself and it works fine but this is just a hack.
The middlewares as defined in common.py are:
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'application.middlewares.IPsBlockerMiddlewareHook',
'application.middlewares.UserMiddleware',
]
The custom middleware in question is UserMiddleware. I need it to be executed after authentication but doesnt seems to be the case
django django-rest-framework django-middleware
New contributor
add a comment |
up vote
2
down vote
favorite
In django REST framework authentication middleware sets the user object in request ONLY after the views middleware is executed while any custom middleware is executed before that. is there someway to change this order and execute custom middleware AFTER user object is set by authentication middleware
As an alternative I create the user object in the middleware itself and it works fine but this is just a hack.
The middlewares as defined in common.py are:
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'application.middlewares.IPsBlockerMiddlewareHook',
'application.middlewares.UserMiddleware',
]
The custom middleware in question is UserMiddleware. I need it to be executed after authentication but doesnt seems to be the case
django django-rest-framework django-middleware
New contributor
i meant custom middleware
– anubysh
Nov 9 at 14:52
1
Please show theUserMiddleware
. Since you are using Rest Framework, perhaps that code should go somewhere else, e.g. an authentication class.
– Alasdair
Nov 9 at 15:08
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
In django REST framework authentication middleware sets the user object in request ONLY after the views middleware is executed while any custom middleware is executed before that. is there someway to change this order and execute custom middleware AFTER user object is set by authentication middleware
As an alternative I create the user object in the middleware itself and it works fine but this is just a hack.
The middlewares as defined in common.py are:
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'application.middlewares.IPsBlockerMiddlewareHook',
'application.middlewares.UserMiddleware',
]
The custom middleware in question is UserMiddleware. I need it to be executed after authentication but doesnt seems to be the case
django django-rest-framework django-middleware
New contributor
In django REST framework authentication middleware sets the user object in request ONLY after the views middleware is executed while any custom middleware is executed before that. is there someway to change this order and execute custom middleware AFTER user object is set by authentication middleware
As an alternative I create the user object in the middleware itself and it works fine but this is just a hack.
The middlewares as defined in common.py are:
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'application.middlewares.IPsBlockerMiddlewareHook',
'application.middlewares.UserMiddleware',
]
The custom middleware in question is UserMiddleware. I need it to be executed after authentication but doesnt seems to be the case
django django-rest-framework django-middleware
django django-rest-framework django-middleware
New contributor
New contributor
edited Nov 9 at 15:09
Alasdair
174k25294303
174k25294303
New contributor
asked Nov 9 at 13:32
anubysh
113
113
New contributor
New contributor
i meant custom middleware
– anubysh
Nov 9 at 14:52
1
Please show theUserMiddleware
. Since you are using Rest Framework, perhaps that code should go somewhere else, e.g. an authentication class.
– Alasdair
Nov 9 at 15:08
add a comment |
i meant custom middleware
– anubysh
Nov 9 at 14:52
1
Please show theUserMiddleware
. Since you are using Rest Framework, perhaps that code should go somewhere else, e.g. an authentication class.
– Alasdair
Nov 9 at 15:08
i meant custom middleware
– anubysh
Nov 9 at 14:52
i meant custom middleware
– anubysh
Nov 9 at 14:52
1
1
Please show the
UserMiddleware
. Since you are using Rest Framework, perhaps that code should go somewhere else, e.g. an authentication class.– Alasdair
Nov 9 at 15:08
Please show the
UserMiddleware
. Since you are using Rest Framework, perhaps that code should go somewhere else, e.g. an authentication class.– Alasdair
Nov 9 at 15:08
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
anubysh is a new contributor. Be nice, and check out our Code of Conduct.
anubysh is a new contributor. Be nice, and check out our Code of Conduct.
anubysh is a new contributor. Be nice, and check out our Code of Conduct.
anubysh is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226670%2fcalling-a-custom-middleware-after-authentication-middleware%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
i meant custom middleware
– anubysh
Nov 9 at 14:52
1
Please show the
UserMiddleware
. Since you are using Rest Framework, perhaps that code should go somewhere else, e.g. an authentication class.– Alasdair
Nov 9 at 15:08