Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is httpBasic method in spring security?

I override configure(HttpSecurity http) method in SampleSecurityConfig Class like this

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        .antMatchers("/delete/**").hasRole("ADMIN")
        .anyRequest().authenticated()
        .and()
        .formLogin().and().httpBasic();
}

If i don't use httpBasic method, it seems no problem occurred.

what does httpBasic method exactly do?

like image 517
sajjad jafari Avatar asked Dec 15 '25 11:12

sajjad jafari


2 Answers

Calling this method on HttpSecurity will enable Http Basic Authentication for your application with some "reasonable" defaults.

It will return a HttpBasicConfigurer for further customization.

You can test this by curl and passing a header like Authorization: Basic bzFbdGfmZrptWY30YQ== but base64 encoding a valid username/password combination.

Documentation for httpBasic

like image 158
Manos Nikolaidis Avatar answered Dec 17 '25 01:12

Manos Nikolaidis


What are we saying by calling httpBasic() ?

  • When httpBasic() is called, we are telling Spring to authenticate the request using the values passed by the Authorization request header. If the request is not authenticated you will get a returned status of 401 and a error message of Unauthorized

What is actually happening when httpBasic() is called ?

  • By calling httpBasic(), an instance of the BasicAuthenticationFilter is added to the filter chain. The BasicAuthenticationFilter will then proceed with trying to authenticate the request in the typical Spring Security fashion. If authentication is successful, the resulting Authentication object will be placed into the SecurityContextHolder, which can then be used for future authentication purposes.
like image 39
Tristan Elliott Avatar answered Dec 17 '25 02:12

Tristan Elliott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!