Part 2 : Three main pillars in the Spring Security Authentication Mechanism–AuthenticationManager
AuthenticationManager
It’s an interface in org.springframework.security.authentication package and has only one method authenticate().
- authenticate(Authentication authentication) which returns a reference to the class implementing Authentication interface.
The following is what the documentation says about Authentication interface .
“Represents the token for an authentication request or for an authenticated principal once the been request has been processed by the AuthenticationManager.authenticate(Authentication) method.
Once the request has been authenticated, the Authentication will usually be stored in a thread-local SecurtyContext managed by the SecurtyContextHolderby the authentication mechanism which is being used. An explicit authentication can be achieved, without instance and using the code”.
SecurityContextHolder.getContext().setAuthentication(anAuthentication);
You can manually get the AuthenticationManager to be used in your controller to authenticate the request by configuring the bean in the spring security context like below.
From the docs:
- <authentication-manager alias="authenticationManager">
- <authentication-provider>
- <jdbc-user-service data-source-ref="someDataSource"/>
- <password-encoder hash="sha"/>
- <user-service>
- <user-name="jimi" password="343m4sdlskldkskdllsdkdskldklskdo3i433434" authorities="ROLE_USER,USER_ADMIN" />
- <user-name="bob" password="343m4sdl34skldkskdllsdkdskldklskd75o3i433434" authorities="ROLE_USER" />
- </user-service>
- </authentication-provider>
- </authentication-manager>
Here you can authenticate the request using a data -source containing the standard Spring Security user data tables and a password encoder is being used to encode the password.
In the Java file you can call it like below:
- @Autowired
- @Qualifier("authenticationManager");
- protected AuthenticationManager authenticationManager;
The summary of this note being AuthenticationManager passes an AuthenticationToken to the Authenticationprovider implementation and they try to authenticate the user, setting the isAuthenticated flag to true or false.
Comments
Post a Comment