Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have below three code snippets all doing the same thing creating in-memory authentication. So how it impacts defining it in different method names?

registerGlobal
configure
configureGlobal
configureGlobalSecurity

ONE

Java
public void registerGlobal(AuthenticationManagerBuilder auth) throws 

Exception {
    auth
    .inMemoryAuthentication()
      .withUser("user").password("password").roles("USER").and()
      .withUser("admin").password("password").roles("USER", 

"ADMIN");
    }
}



TWO

Java
@Override
    protected void configure(AuthenticationManagerBuilder auth)
        throws Exception {
    auth
         .inMemoryAuthentication()
              .withUser("user").password("password").roles

        ("USER");
 }


Three

Java
public void configureGlobal(AuthenticationManagerBuilder auth)
        throws Exception {
    auth
         .inMemoryAuthentication()
              .withUser("user").password("password").roles

     ("USER");
}


FOURTH

<pre lang="java">

@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth)     throws Exception {
 auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");
}


What I have tried:

Searched spring.io and other blogs but only found usages not explanations
Posted

1 solution

I renamed the method in my sample project to below and to my surprise it working and authenticating the users.

Java
@Autowired
public void anyMethodName(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");      
}


You name it anything and it works



The name of the configureGlobal method is not important. However, it is important to only configure AuthenticationManagerBuilder in a class annotated with either @EnableWebSecurity, @EnableWebMvcSecurity, @EnableGlobalMethodSecurity, or @EnableGlobalAuthentication. Doing otherwise has unpredictable results.

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900