how to provide basic auth in spring boot 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;
   // add endpoint you want to protect to this list
    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");
    }

}

Tags:

Java Example