0

I have created a REST service using spring and security with oauth2 currently when I hit the url I get the access token and then using it I send my basic login credentials which is static in my file in the spring_security.xml file. How can I provide user authentication using database?

<authentication-manager alias="authenticationManager"
xmlns="http://www.springframework.org/schema/security">
<authentication-provider>
<user-service>
<user name="majid" password="majid" authorities="ROLE_APP" />
<user name="majid1" password="majid1" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>

1 Answer 1

1

You have to provide your custom implementation of AuthenticationProvider interface

<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider ref="authenticationProvider"/>
</security:authentication-manager>

<bean id="authenticationProvider" 
     class="com.mycompany.security.MyDatabaseAuthenticationProvider"/>

Implementation would look sth. like this

public class MyDatabaseAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(final Authentication authentication) {
        final String username = (String) authentication.getPrincipal();
        final String password = (String) authentication.getCredentials();

        // perform your database stuff and return Authentication object

    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.