spring boot add basic authentication code example
Example: configure basic auth spring boot
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${app.basicAuthUserName}")
private String basicAuthUserName;
@Value("${app.basicAuthPassword}")
private String basicAuthPassword;
private String[] basicAuthEndpoints = {"/mytendpoint/path"};
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().antMatchers(basicAuthEndpoints).fullyAuthenticated()
.and().httpBasic().and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser(basicAuthUserName)
.password("{noop}" + basicAuthPassword).roles("USER");
}
}