OAuth2ResourceServerSecurityConfiguration.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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.security.config.annotation.web.builders.HttpSecurity;
  18. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  19. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  20. import static org.springframework.security.config.Customizer.withDefaults;
  21. /**
  22. * @author Josh Cummings
  23. */
  24. @EnableWebSecurity
  25. public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
  26. @Override
  27. protected void configure(HttpSecurity http) throws Exception {
  28. // @formatter:off
  29. http
  30. .authorizeRequests(authorizeRequests ->
  31. authorizeRequests
  32. .antMatchers("/message/**").hasAuthority("SCOPE_message:read")
  33. .anyRequest().authenticated()
  34. )
  35. .oauth2ResourceServer(oauth2ResourceServer ->
  36. oauth2ResourceServer
  37. .jwt(withDefaults())
  38. );
  39. // @formatter:on
  40. }
  41. }