Skip to main content

SpringSecurity : Configure In Memory Authentication

Configure Spring Security to Authenticate user using In-Memory Authentication.

To implement inMemory authentication, all you need to do is

  1. extend WebSecurityConfigurerAdapter.
  2. override configure(AuthenticationManagerBuilder) method
  3. add username, password and roles/authorities for authentication.

After adding the following class to your application, you will be able to login using these username password pairs.

package com.ekiras.ss.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
* @author ekiras
*/

@EnableWebSecurity
public class SpringSecurityConfigurer extends WebSecurityConfigurerAdapter{


@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("ekansh")
.password("password")
.roles("USER", "ROLE");
auth.inMemoryAuthentication()
.withUser("admin")
.password("admin")
.roles("ADMIN");
}

}

As shown in the code above, we have configured two user-password pairs,

  1. username : user , password : user and role/authorities : USER.
  2. username : admin , password : admin and role/authorities : ADMIN.

Note

You can use any of the above pairs to login and authenticate user. First, can be used for requests that can be accessed by USER role and Second can be used to access the requests with ADMIN role.


Also Read


    Download project from Github

Comments