OAuth2ResourceServerSecurityConfiguration.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /**
  33. * @author Josh Cummings
  34. */
  35. @EnableWebSecurity
  36. public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
  37. @Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}")
  38. String jwkSetUri;
  39. @Value("${spring.security.oauth2.resourceserver.opaque.introspection-uri}")
  40. String introspectionUri;
  41. @Override
  42. protected void configure(HttpSecurity http) throws Exception {
  43. // @formatter:off
  44. http
  45. .authorizeRequests()
  46. .antMatchers("/**/message/**").hasAuthority("SCOPE_message:read")
  47. .anyRequest().authenticated()
  48. .and()
  49. .oauth2ResourceServer()
  50. .authenticationManagerResolver(multitenantAuthenticationManager());
  51. // @formatter:on
  52. }
  53. @Bean
  54. AuthenticationManagerResolver<HttpServletRequest> multitenantAuthenticationManager() {
  55. Map<String, AuthenticationManager> authenticationManagers = new HashMap<>();
  56. authenticationManagers.put("tenantOne", jwt());
  57. authenticationManagers.put("tenantTwo", opaque());
  58. return request -> {
  59. String tenantId = request.getPathInfo().split("/")[1];
  60. return Optional.ofNullable(authenticationManagers.get(tenantId))
  61. .orElseThrow(() -> new IllegalArgumentException("unknown tenant"));
  62. };
  63. }
  64. AuthenticationManager jwt() {
  65. JwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(this.jwkSetUri).build();
  66. return new JwtAuthenticationProvider(jwtDecoder)::authenticate;
  67. }
  68. AuthenticationManager opaque() {
  69. return new OAuth2IntrospectionAuthenticationProvider(this.introspectionUri, "client", "secret")::authenticate;
  70. }
  71. }