WebSecurityConfiguration.java 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.shkpr.service.alambizplugin.configuration;
  2. import com.shkpr.service.alambizplugin.controllerfilter.CustomAuthenticationProvider;
  3. import com.shkpr.service.alambizplugin.controllerfilter.third.ApiJWTBizFilterMgr;
  4. import com.shkpr.service.alambizplugin.constants.ApiURI;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  8. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  9. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  10. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  11. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  12. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  13. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  14. /**
  15. * 该类主要用来做权限控制的配置、以及注册各种过滤器
  16. * 执行顺序
  17. * (1) 注册验证组件 - configure(AuthenticationManagerBuilder auth)方法中注册自定义验证组件
  18. * (2) 设置验证规则 - configure(HttpSecurity http)方法中设置了各种路由访问规则
  19. * (3) 初始化过滤组件 - JWTLoginFilter 和 JWTAuthenticationFilter 类会初始化
  20. */
  21. @Configuration
  22. @EnableWebSecurity
  23. @EnableGlobalMethodSecurity(prePostEnabled = true) //@PreAuthorize对权限的注解需要设置prePostEnabled = true
  24. public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
  25. @Value("${global.test.pressure:false}")
  26. private boolean mBForPressureTest;
  27. @Value("${global.ops.lan.ip:127.0.0.1}")
  28. private String mStrOpsServerLanIP;
  29. @Override
  30. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  31. //使用自定义身份验证组件
  32. auth.authenticationProvider(new CustomAuthenticationProvider());
  33. }
  34. // 设置 HTTP 验证规则
  35. @Override
  36. protected void configure(HttpSecurity http) throws Exception {
  37. String[] arrOpsServerLanIPs = mStrOpsServerLanIP.split(";");
  38. String strAccessFilterForOps = "hasIpAddress('127.0.0.1')";
  39. for (String strTmp:arrOpsServerLanIPs){
  40. strAccessFilterForOps += " or hasIpAddress('"+ strTmp +"')";
  41. }
  42. http.csrf().disable() // 关闭csrf验证
  43. .authorizeRequests() // 对请求进行认证
  44. .antMatchers(ApiURI.URI_ACCESS_TOKEN_CHECK).permitAll()
  45. .antMatchers(ApiURI.URI_FILE_BUSI_XXX).permitAll()
  46. .antMatchers("/").permitAll()
  47. .anyRequest().authenticated() //所有其他请求需要身份认证
  48. .and()
  49. .addFilterBefore(new ApiJWTBizFilterMgr(ApiURI.URI_ALL_BUSI_XXX, authenticationManager()),
  50. UsernamePasswordAuthenticationFilter.class);
  51. /*.addFilterBefore(new ServerStatusMonitorFilter(ThirdApiURI.URI_HGAS_MONITOR_XXX, authenticationManager()),
  52. UsernamePasswordAuthenticationFilter.class);*/
  53. }
  54. @Override
  55. public void configure(WebSecurity web) throws Exception {
  56. /*web.ignoring()
  57. .antMatchers("/error")
  58. .antMatchers("/static")
  59. .antMatchers("/static/**"); // 所有/static下的静态资源请求时都忽略访问规则
  60. */
  61. }
  62. }