'Spring Security: don't redirect to login page in case unauthorised

I have Spring Security with oAuth2 authorisation.

I use it for REST API.

My configuration:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/health").permitAll()
                .antMatchers("/**").authenticated()
                .and()
                .oauth2Login()
                .and()
                .httpBasic();
    }
}

I need to make all requests return me 401 when I didn't authorise.

But now when I'm not authorised I got redirect to /login page.

I need to use it like usual REST API: if I did authorise then get content, otherwise get 401 Unauthorised.

How I can make it?

Thanks in addition for help.



Solution 1:[1]

Basically you need to configure an AuthenticationEntryPoint which is invoked when Spring Security detects a non-authenticated request. Spring also gives you a handy implementation which enables you to return whatever HttpStatus you need:

 @Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
            //rest of your config...
            .exceptionHandling()
                .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED));

}

Solution 2:[2]

You cas use uksort() and a closure to access to 1. array keys and 2. array values :

Eg. :

<?php

$array = [
    'Key-1' => ['Element-1' => 'a', 'Element-2' => 'b', 'Element-3' => 'c', 'Element-4' => 'd'],
    'Key-2' => ['Element-1' => 'e', 'Element-2' => 'f', 'Element-3' => 'g', 'Element-4' => 'h'],
    'Key-3' => ['Element-1' => 'i', 'Element-2' => 'j', 'Element-3' => 'k', 'Element-4' => 'l'],
    'Key-4' => ['Element-1' => 'm', 'Element-2' => 'n', 'Element-3' => 'o', 'Element-4' => 'p'],
];

uksort($array, function ($key_a, $key_b) use ($array) {
    echo "===================\r\n";
    echo "First key : {$key_a}, first element : ";
    print_r($array[$key_a]);
    echo "Second key : {$key_b}, second element : ";
    print_r($array[$key_b]);
    // ...
});

Gives :

===================
First key : Key-1, first element : Array
(
    [Element-1] => a
    [Element-2] => b
    [Element-3] => c
    [Element-4] => d
)
Second key : Key-2, second element : Array
(
    [Element-1] => e
    [Element-2] => f
    [Element-3] => g
    [Element-4] => h
)
===================
First key : Key-2, first element : Array
(
    [Element-1] => e
    [Element-2] => f
    [Element-3] => g
    [Element-4] => h
)
Second key : Key-3, second element : Array
(
    [Element-1] => i
    [Element-2] => j
    [Element-3] => k
    [Element-4] => l
)
===================
First key : Key-3, first element : Array
(
    [Element-1] => i
    [Element-2] => j
    [Element-3] => k
    [Element-4] => l
)
Second key : Key-4, second element : Array
(
    [Element-1] => m
    [Element-2] => n
    [Element-3] => o
    [Element-4] => p
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Mališ
Solution 2 JCH77