ResourceServerConfig.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package com.lqkj.framework.security;
  2. import com.lqkj.framework.security.handler.CustomAuthExceptionEntryPoint;
  3. import com.lqkj.framework.security.handler.CustomAuthenctiationFailureHandler;
  4. import com.lqkj.framework.security.handler.CustomAuthenticationSuccessHandler;
  5. import com.lqkj.framework.security.handler.CustomLogoutSuccessHandler;
  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. @Configuration
  16. @EnableResourceServer
  17. public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
  18. @Autowired
  19. private CustomAuthenctiationFailureHandler customAuthenctiationFailureHandler;
  20. @Autowired
  21. private CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;
  22. @Autowired
  23. private CustomAuthExceptionEntryPoint customAuthExceptionEntryPoint;
  24. @Autowired
  25. private CustomLogoutSuccessHandler customLogoutSuccessHandler;
  26. @Override
  27. public void configure(ResourceServerSecurityConfigurer resources) {
  28. resources.authenticationEntryPoint(customAuthExceptionEntryPoint);
  29. }
  30. @Override
  31. public void configure(HttpSecurity http) throws Exception {
  32. http.formLogin()
  33. .loginProcessingUrl("/login")
  34. .successHandler(customAuthenticationSuccessHandler)
  35. .failureHandler(customAuthenctiationFailureHandler)
  36. .and()
  37. .logout()
  38. .logoutUrl("/logout").logoutSuccessHandler(customLogoutSuccessHandler)
  39. .and()
  40. .authorizeRequests()
  41. .antMatchers("/oauth/**",
  42. "/v1/captchaImage",
  43. "/business/cmsNews/front/**",
  44. "/business/cmsCategory/front/**",
  45. "/login",
  46. "/**/*.css",
  47. "/**/*.js",
  48. "/profile/**").permitAll() //不需要身份认证即可访问
  49. .antMatchers("/swagger-resources/**").anonymous()
  50. .antMatchers("/webjars/**").anonymous()
  51. .antMatchers("/*/api-docs").anonymous()
  52. .anyRequest().authenticated() //其他请求路径都需要身份认证
  53. .and().headers().frameOptions().disable()//支持前端vue中iframe中访问
  54. .and().cors()
  55. .and().csrf().disable();
  56. }
  57. }