OAuth2ResourceServerSecurityConfiguration.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 javax.servlet.http.HttpServletRequest;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.context.annotation.Bean;
  22. import org.springframework.security.authentication.AuthenticationManager;
  23. import org.springframework.security.authentication.AuthenticationManagerResolver;
  24. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  25. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  26. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  27. import org.springframework.security.oauth2.jwt.JwtDecoder;
  28. import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
  29. import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationProvider;
  30. import org.springframework.security.oauth2.server.resource.authentication.OAuth2IntrospectionAuthenticationProvider;
  31. import org.springframework.security.oauth2.server.resource.introspection.NimbusOAuth2TokenIntrospectionClient;
  32. import org.springframework.security.oauth2.server.resource.introspection.OAuth2TokenIntrospectionClient;
  33. import static org.springframework.security.web.authentication.MultiTenantAuthenticationManagerResolver.resolveFromPath;
  34. /**
  35. * @author Josh Cummings
  36. */
  37. @EnableWebSecurity
  38. public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
  39. @Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}")
  40. String jwkSetUri;
  41. @Value("${spring.security.oauth2.resourceserver.opaque.introspection-uri}")
  42. String introspectionUri;
  43. @Override
  44. protected void configure(HttpSecurity http) throws Exception {
  45. // @formatter:off
  46. http
  47. .authorizeRequests(authorizeRequests ->
  48. authorizeRequests
  49. .antMatchers("/**/message/**").hasAuthority("SCOPE_message:read")
  50. .anyRequest().authenticated()
  51. )
  52. .oauth2ResourceServer(oauth2ResourceServer ->
  53. oauth2ResourceServer
  54. .authenticationManagerResolver(multitenantAuthenticationManager())
  55. );
  56. // @formatter:on
  57. }
  58. @Bean
  59. AuthenticationManagerResolver<HttpServletRequest> multitenantAuthenticationManager() {
  60. Map<String, AuthenticationManager> authenticationManagers = new HashMap<>();
  61. authenticationManagers.put("tenantOne", jwt());
  62. authenticationManagers.put("tenantTwo", opaque());
  63. return resolveFromPath(authenticationManagers::get);
  64. }
  65. AuthenticationManager jwt() {
  66. JwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(this.jwkSetUri).build();
  67. return new JwtAuthenticationProvider(jwtDecoder)::authenticate;
  68. }
  69. AuthenticationManager opaque() {
  70. OAuth2TokenIntrospectionClient introspectionClient =
  71. new NimbusOAuth2TokenIntrospectionClient(this.introspectionUri, "client", "secret");
  72. return new OAuth2IntrospectionAuthenticationProvider(introspectionClient)::authenticate;
  73. }
  74. }