Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use httpbasic and OAuth2 both for an API depending on path variable?

In my application I want to provide OAuth2 security only to some specific API calls. My question is can I provide HttpBasic or Oauth2 authentication based on the path variable?

Below are two scenarios I will be considering.

1) Lets say for user(whose name is provided in path variable) xyz, if xyz does not have the feature of OAuth, I want to authenticate it using httpBasic

2) If another user abc has feature of OAuth, I want to authenticate it using Oauth/OpenId connect.

I have a table which assigns features to user, below is the glimpse of the table.

Name , Feature

xyz, HttpBasic

abc, Oauth

like image 258
Chintan Pandya Avatar asked Jun 11 '18 11:06

Chintan Pandya


1 Answers

Okay I did some research on my own and able to find a solution. Here's what I did,

-Created one httpbasic configuration with WebSecurityConfigurerAdapter, now before any interceptor begins it task I have created one request matcher which will check if the authorization header is Basic or Bearer.

      //By default this filter order is 100 and OAuth has filter order 3
      @Order(2)
    public class MicroserviceSecurityConfigurationHttpBasic extends  WebSecurityConfigurerAdapter { 
          @Override
          protected void configure(HttpSecurity http) throws Exception {
             http.csrf().disable().exceptionHandling()
            .authenticationEntryPoint(customAccessDeniedHandler())
            .and().headers().frameOptions().disable()
            .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .requestMatcher(new BasicRequestMatcher())
            .authorizeRequests()
            .antMatchers("/api/**").authenticated()
            .and().httpBasic();

          }
          private class BasicRequestMatcher implements RequestMatcher {
            @Override
            public boolean matches(HttpServletRequest httpRequest) {
             String auth = httpRequest.getHeader("Authorization");
             String requestUri = httpRequest.getRequestURI();
             //Fetching Identifier to provide OAuth Security to only specific urls
             String identifier= requestUri.substring(requestUri.lastIndexOf("/") + 1, requestUri.length());

            //Lets say for identifier ABC only, I want to secure it using OAuth2.0
           if (auth != null && auth.startsWith("Basic") && identifier.equalsIgnoreCase("ABC")) {
             auth=null;
              }
           //For ABC identifier this method will return null so then the authentication will be redirected to OAuth2.0 config.
           return (auth != null && auth.startsWith("Basic"));
            }
        }
  }

-After this I have created OAuth2.0 configuration with ResourceServerConfigurerAdapter, here is the glimpse of it.

    //Default filter order=3 so this will be executed after WebSecurityConfigurerAdapter 
    public class MicroserviceSecurityConfiguration extends ResourceServerConfigurerAdapter {
       ...
      //Here I am intercepting the same url but the config will look for bearer token only
      @Override
public void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().exceptionHandling()
    .and().headers().frameOptions().disable()
    .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and().authorizeRequests()
    .antMatchers("/api/**").authenticated();
    }
   }

References : https://github.com/spring-projects/spring-security-oauth/issues/1024

Spring security with Oauth2 or Http-Basic authentication for the same resource

like image 79
Chintan Pandya Avatar answered Oct 17 '22 04:10

Chintan Pandya