ResourceServerConfig.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package com.yonge.audio.config;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  5. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  6. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  7. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  8. import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  9. import com.ym.mec.common.security.BaseAccessDeniedHandler;
  10. import com.ym.mec.common.security.BaseAuthenticationEntryPoint;
  11. @Configuration
  12. @EnableResourceServer
  13. @EnableGlobalMethodSecurity(prePostEnabled = true)
  14. public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
  15. @Autowired
  16. private BaseAccessDeniedHandler baseAccessDeniedHandler;
  17. @Autowired
  18. private BaseAuthenticationEntryPoint baseAuthenticationEntryPoint;
  19. @Override
  20. public void configure(HttpSecurity http) throws Exception {
  21. http.authorizeRequests()
  22. .antMatchers("/task/**")
  23. .hasIpAddress("0.0.0.0/0")
  24. .antMatchers("/v2/api-docs")
  25. .permitAll()
  26. // 任何人不登录都可以获取的资源
  27. // .antMatchers("/ipController/**").hasIpAddress("127.0.0.1") //特定ip可以不登录获取资源
  28. // .antMatchers("/ipControll/**").access("isAuthenticated() and hasIpAddress('127.0.0.1')")// 特定ip必须登录才能获取
  29. .anyRequest().authenticated().and().csrf().disable().exceptionHandling().accessDeniedHandler(baseAccessDeniedHandler)
  30. .authenticationEntryPoint(baseAuthenticationEntryPoint).and();
  31. }
  32. @Override
  33. public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
  34. resources.authenticationEntryPoint(baseAuthenticationEntryPoint).accessDeniedHandler(baseAccessDeniedHandler);
  35. }
  36. }