FebsResourceServerConfigurer.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package cc.mrbird.febs.auth.configure;
  2. import cc.mrbird.febs.auth.properties.FebsAuthProperties;
  3. import cc.mrbird.febs.common.handler.FebsAccessDeniedHandler;
  4. import cc.mrbird.febs.common.handler.FebsAuthExceptionEntryPoint;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  9. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  10. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  11. import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  12. /**
  13. * 资源服务器配置
  14. *
  15. * @author MrBird
  16. */
  17. @Configuration
  18. @EnableResourceServer
  19. public class FebsResourceServerConfigurer extends ResourceServerConfigurerAdapter {
  20. @Autowired
  21. private FebsAccessDeniedHandler accessDeniedHandler;
  22. @Autowired
  23. private FebsAuthExceptionEntryPoint exceptionEntryPoint;
  24. @Autowired
  25. private FebsAuthProperties properties;
  26. @Override
  27. public void configure(HttpSecurity http) throws Exception {
  28. String[] anonUrls = StringUtils.splitByWholeSeparatorPreserveAllTokens(properties.getAnonUrl(), ",");
  29. http.csrf().disable()
  30. .requestMatchers().antMatchers("/**")
  31. .and()
  32. .authorizeRequests()
  33. .antMatchers(anonUrls).permitAll()
  34. .antMatchers("/**").authenticated()
  35. .and().httpBasic();
  36. }
  37. @Override
  38. public void configure(ResourceServerSecurityConfigurer resources) {
  39. resources.authenticationEntryPoint(exceptionEntryPoint)
  40. .accessDeniedHandler(accessDeniedHandler);
  41. }
  42. }