OAuth2ResourceServerSecurityConfiguration.java 3.2 KB

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