'Content negotiation defined security config for multiple oauth2 providers

I want to create an API that allows multiple oauth2 providers, and depending on the provider it calls a particular method via ACCEPT header(content negotiation) versioning.

I try to write controller that gets called via versioning in content negotiation.

I have written the below which works fine based off the Accept header.

@Controller pubic class LeagueController{

@Autowired
private LeagueService leagueService;

@Autowired// context is loaded via @EnableWebSecurity
private SecurityContext context;

// RESTful method
@RequestMapping(value="/leagues", produces = "application/vnd.com.gim.v1+json")
@ResponseStatus(HttpStatus.OK)
public @ResponseBody List<League> doSomethingOauth2FirstPreference(Principal principal) {
    return leagueService.getLeagues(principal);
}


// RESTful method
@RequestMapping(value="/league", produces = "application/vnd.com.me.model.v2+json")
@ResponseStatus(HttpStatus.OK)
public @ResponseBody List<List<League> > doSomethingOauth2SecondPrefence(Principal principal) {
    List<Authorities> authorities = context.getAuthoritiesForUser(principal) );
    return leagueService.getLeagues(authorities);
}

I have followed this to create multiple oauth2 security configurations using a filter:

https://spring.io/guides/tutorials/spring-boot-oauth2/

But what I want is if the version of the Accept header is "application/vnd.com.gim.v2+json" it goes through gmail login, otherwise to doSomethingOauth2FirstPreference as default which uses a different oauth2 provider.

Is this possible in spring boot? Multiple oauth2 providers that the security filter invokes on versions? Or is there another way of doing this other than content negotiation?



Sources

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

Source: Stack Overflow

Solution Source