I'm working on a java spring boot project which I'm trying to get spring security set up for user authentication with JWT, the tutorial I'm following(and also many tutorials and projects I found on the internet) talks about two sections- authentication and authorization.
In most tutorials there are two filter classes one handles Authentication, and the other handles Authorization!
(Some I have found with only one class which extends OncePerRequestFilter
class).
In those projects that have two filter classes,
The Authentication filter class extends UsernamePasswordAuthenticationFilter
class.
Authorization class extends BasicAuthenticationFilter
class.
Is there a way that I can only use authentication part in my project or should I use both classes to set up user authentication in spring security?
Any explanation will be appreciated.
u have to write ur userDetial to tell spring current user authorization and config that
ur filter could be like this
and u have to change ur userDetailService to spring know how to laod ur user
No, there is no concept of only authentication part, you have wrong perception about spring security, spring security is all about configuration either by using default or by implementing your custom configurations. (
AuthenticationFilters
,AuthenticationProviders
,AuthenticationToken
etc)Spring security is all about authentication and authorization, Spring security is configured by declaring a filter DelegatingFilterProxy in web.xml(In Spring boot it will be done by auto configuration).
Spring security puts a WALL(HttpFireWall) before your application in terms of proxy filters or spring managed beans. Request can reach your application if it succeeds in both authentication and authorization part.
1. Authentication is all about identification of user.
it will undergoes
Here in this step
Authentication
object will be created. From auth object you can getUserDetails
orAuthenticatedPrincipal
orPrincipal
)AuthenticationManager
)2. Authorization is all about access decision.
There will be
FilterSecurityInterceptor
which comes almost last in the filter chain which getsAuthentication
object fromSecurityContext
and gets granted authorities list(roles granted) and it will make a decision whether to allow this request to reach the requested resource or not, decision is made by matching with the allowed AntMatchers configured inHttpSecurityConfiguration
.Consider the exceptions 401-UnAuthorized and 403-Forbidden. These decisions will be done at the last in the filter chain
401-UnAuthorized: Un authenticated user trying to access secured resource.
403-Forbidden : Authenticated user trying to access restricted resource.
Un authenticated user will be allowed to access non restricted resources and he will not get UnAuthorized error but it is handled by
AnonymousAuthenticationFilter
which sets authorityROLE_ANONYMOUS
for unauthenticated user.Note
Just to give some idea of filters in spring securityBelow given filter ordering. where,
Authentication is @order-4
Authorization is @Order-9(Last)
Finally, if you are new to spring security. My suggestion is to try out maximum examples and spend more time on debug logs and try to understand the flow.