OAuth2ResourceServerSecurityConfiguration.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  19. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  20. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  21. /**
  22. * @author Josh Cummings
  23. */
  24. @EnableWebSecurity
  25. public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
  26. @Value("${spring.security.oauth2.resourceserver.opaque.introspection-uri}") String introspectionUri;
  27. @Value("${spring.security.oauth2.resourceserver.opaque.introspection-client-id}") String clientId;
  28. @Value("${spring.security.oauth2.resourceserver.opaque.introspection-client-secret}") String clientSecret;
  29. @Override
  30. protected void configure(HttpSecurity http) throws Exception {
  31. // @formatter:off
  32. http
  33. .authorizeRequests(authorizeRequests ->
  34. authorizeRequests
  35. .mvcMatchers("/message/**").hasAuthority("SCOPE_message:read")
  36. .anyRequest().authenticated()
  37. )
  38. .oauth2ResourceServer(oauth2ResourceServer ->
  39. oauth2ResourceServer
  40. .opaqueToken(opaqueToken ->
  41. opaqueToken
  42. .introspectionUri(this.introspectionUri)
  43. .introspectionClientCredentials(this.clientId, this.clientSecret)
  44. )
  45. );
  46. // @formatter:on
  47. }
  48. }