OAuth2ResourceServerSecurityConfiguration.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2002-2019 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package sample;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.Optional;
  20. import javax.servlet.http.HttpServletRequest;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.context.annotation.Bean;
  23. import org.springframework.security.authentication.AuthenticationManager;
  24. import org.springframework.security.authentication.AuthenticationManagerResolver;
  25. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  26. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  27. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  28. import org.springframework.security.oauth2.jwt.JwtDecoder;
  29. import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
  30. import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationProvider;
  31. import org.springframework.security.oauth2.server.resource.authentication.OAuth2IntrospectionAuthenticationProvider;
  32. import org.springframework.security.oauth2.server.resource.introspection.NimbusOpaqueTokenIntrospector;
  33. import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector;
  34. /**
  35. * @author Josh Cummings
  36. */
  37. @EnableWebSecurity
  38. public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
  39. @Value("${tenantOne.jwk-set-uri}")
  40. String jwkSetUri;
  41. @Value("${tenantTwo.introspection-uri}")
  42. String introspectionUri;
  43. @Value("${tenantTwo.introspection-client-id}")
  44. String introspectionClientId;
  45. @Value("${tenantTwo.introspection-client-secret}")
  46. String introspectionClientSecret;
  47. @Override
  48. protected void configure(HttpSecurity http) throws Exception {
  49. // @formatter:off
  50. http
  51. .authorizeRequests(authorizeRequests ->
  52. authorizeRequests
  53. .antMatchers("/**/message/**").hasAuthority("SCOPE_message:read")
  54. .anyRequest().authenticated()
  55. )
  56. .oauth2ResourceServer(oauth2ResourceServer ->
  57. oauth2ResourceServer
  58. .authenticationManagerResolver(multitenantAuthenticationManager())
  59. );
  60. // @formatter:on
  61. }
  62. @Bean
  63. AuthenticationManagerResolver<HttpServletRequest> multitenantAuthenticationManager() {
  64. Map<String, AuthenticationManager> authenticationManagers = new HashMap<>();
  65. authenticationManagers.put("tenantOne", jwt());
  66. authenticationManagers.put("tenantTwo", opaque());
  67. return request -> {
  68. String[] pathParts = request.getRequestURI().split("/");
  69. String tenantId = pathParts.length > 0 ? pathParts[1] : null;
  70. return Optional.ofNullable(tenantId)
  71. .map(authenticationManagers::get)
  72. .orElseThrow(() -> new IllegalArgumentException("unknown tenant"));
  73. };
  74. }
  75. AuthenticationManager jwt() {
  76. JwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(this.jwkSetUri).build();
  77. return new JwtAuthenticationProvider(jwtDecoder)::authenticate;
  78. }
  79. AuthenticationManager opaque() {
  80. OpaqueTokenIntrospector introspectionClient =
  81. new NimbusOpaqueTokenIntrospector(this.introspectionUri,
  82. this.introspectionClientId, this.introspectionClientSecret);
  83. return new OAuth2IntrospectionAuthenticationProvider(introspectionClient)::authenticate;
  84. }
  85. }