WebSecurityConfig.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package cn.seecoder.web.infrastructure.config;
  2. import cn.seecoder.web.infrastructure.security.JwtAuthenticationTokenFilter;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.http.HttpMethod;
  7. import org.springframework.security.authentication.AuthenticationManager;
  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.configuration.EnableWebSecurity;
  11. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  12. import org.springframework.security.config.http.SessionCreationPolicy;
  13. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  14. import org.springframework.web.cors.CorsConfiguration;
  15. import org.springframework.web.cors.CorsConfigurationSource;
  16. import org.springframework.web.cors.CorsUtils;
  17. import cn.seecoder.web.infrastructure.security.WebSecurityConstants;
  18. /**
  19. * @author PuHong Weng
  20. * @date 2021/3/5
  21. * @description:
  22. */
  23. @Configuration
  24. @EnableWebSecurity
  25. @EnableGlobalMethodSecurity(prePostEnabled = true) // 启用方法级别的权限认证
  26. //@DependsOn("userServiceImpl")
  27. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  28. private final JwtAuthenticationTokenFilter filter;
  29. private final AuthTools authTools;
  30. @Autowired
  31. public WebSecurityConfig(JwtAuthenticationTokenFilter filter, AuthTools authTools) {
  32. super();
  33. this.filter = filter;
  34. this.authTools = authTools;
  35. }
  36. @Override
  37. protected void configure(HttpSecurity http) throws Exception {
  38. //访问控制
  39. http
  40. .cors()
  41. .and()
  42. .csrf().disable()
  43. .sessionManagement()
  44. .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  45. .and()
  46. .addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class)
  47. .authorizeRequests()
  48. // API Test Controller
  49. .antMatchers(HttpMethod.GET, "/test/list/{projectId}").access("@authTools.checkProjOwnership(#projectId)")
  50. .antMatchers(HttpMethod.GET, "/test/delete/{testId}").access("@authTools.checkTestOwnership(#testId)")
  51. .antMatchers(HttpMethod.GET, "/test/execute/{testId}").access("@authTools.checkTestOwnership(#testId)")
  52. .antMatchers(HttpMethod.GET, "/test/result/{testId}").access("@authTools.checkTestOwnership(#testId)")
  53. .antMatchers(HttpMethod.GET, "/test/latest/{testId}").access("@authTools.checkTestOwnership(#testId)")
  54. .antMatchers(HttpMethod.POST, "/test/create").authenticated()
  55. // Auto Test Controller
  56. .antMatchers(HttpMethod.GET, "/auto_test/list").access("@authTools.checkProjOwnershipParam(request)")
  57. // Bug List Controller
  58. .antMatchers(HttpMethod.GET, "/bug_list/list").access("@authTools.checkProjOwnershipParam(request)")
  59. .antMatchers(HttpMethod.POST, "/bug_list").authenticated()
  60. .antMatchers(HttpMethod.PUT, "/bug_list").access("@authTools.checkBugOwnershipBody(request)")
  61. // Func Test Controller
  62. .antMatchers(HttpMethod.GET, "/func_test").access("@authTools.checkProjOwnershipParam(request)")
  63. .antMatchers(HttpMethod.GET, "/func_test/taskList").access("@authTools.checkProjOwnershipParam(request)")
  64. .antMatchers(HttpMethod.GET, "/func_test/steps").access("@authTools.checkTestCaseOwnershipParam(request)")
  65. .antMatchers(HttpMethod.POST, "/func_test").access("@authTools.checkProjOwnershipParam(request)")
  66. .antMatchers(HttpMethod.POST, "/func_test/steps").access("@authTools.checkTestCaseOwnershipParam(request)")
  67. .antMatchers(HttpMethod.PUT, "/func_test").access("@authTools.checkTestCaseOwnershipParam(request)")
  68. .antMatchers(HttpMethod.PUT, "/func_test/steps").access("@authTools.checkTestStepOwnershipParam(request)")
  69. .antMatchers(HttpMethod.PUT, "/func_test/finish").access("@authTools.checkTestCaseOwnershipParam(request)")
  70. .antMatchers(HttpMethod.PUT, "/func_test/reopen").access("@authTools.checkTestCaseOwnershipParam(request)")
  71. .antMatchers(HttpMethod.PUT, "/func_test/steps/state").access("@authTools.checkTestStepOwnershipParam(request)")
  72. .antMatchers(HttpMethod.PUT, "/func_test/record/latest").access("@authTools.checkProjOwnershipParam(request)")
  73. .antMatchers(HttpMethod.DELETE, "/func_test/steps").access("@authTools.checkTestStepOwnershipParam(request)")
  74. .antMatchers(HttpMethod.DELETE, "/func_test").access("@authTools.checkTestCaseOwnershipParam(request)")
  75. // Commit Controller
  76. .antMatchers(HttpMethod.GET, "/commits/tree").access("@authTools.checkTreeNodeOwnershipParam(request)")
  77. .antMatchers(HttpMethod.GET, "/commits/bug_list").access("@authTools.checkBugOwnershipParam(request)")
  78. .antMatchers(HttpMethod.GET, "/commits/list").access("@authTools.checkProjOwnershipParam(request)")
  79. // Deployment Controller
  80. .antMatchers(HttpMethod.GET, "/deployments/list").access("@authTools.checkProjOwnershipParam(request)")
  81. .antMatchers(HttpMethod.GET, "/deployments/log").access("@authTools.checkProjPipelineParam(request)")
  82. .antMatchers(HttpMethod.POST, "/deployments").access("@authTools.checkProjPipelineParam(request)")
  83. .antMatchers(HttpMethod.DELETE, "/deployments").access("@authTools.checkProjPipelineParam(request)")
  84. // Pipeline Controller
  85. .antMatchers(HttpMethod.GET, "/pipelines/list").access("@authTools.checkProjOwnershipParam(request)")
  86. .antMatchers(HttpMethod.GET, "/pipelines").access("@authTools.checkProjPipelineParam(request)")
  87. .antMatchers(HttpMethod.GET, "/pipelines/templates").authenticated()
  88. .antMatchers(HttpMethod.GET, "/pipelines/record/list").access("@authTools.checkProjPipelineParam(request)")
  89. .antMatchers(HttpMethod.GET, "/pipelines/record").access("@authTools.checkPipelineRecordOwnershipParam(request)")
  90. .antMatchers(HttpMethod.GET, "/pipelines/record/details").access("@authTools.checkPipelineRecordOwnershipParam(request)")
  91. .antMatchers(HttpMethod.POST, "/pipelines").authenticated()
  92. .antMatchers(HttpMethod.PUT, "/pipelines/config").access("@authTools.checkProjPipelineBody(request)")
  93. .antMatchers(HttpMethod.DELETE, "/pipelines").access("@authTools.checkProjPipelineParam(request)")
  94. // Stage Controller
  95. .antMatchers(HttpMethod.GET, "/stages").authenticated()
  96. // Project Controller
  97. .antMatchers(HttpMethod.GET, "/project/listByUser").authenticated()
  98. .antMatchers(HttpMethod.GET, "/project/members").access("@authTools.checkProjOwnershipParam(request)")
  99. .antMatchers(HttpMethod.GET, "/project/{projectId}").access("@authTools.checkProjOwnership(#projectId)")
  100. .antMatchers(HttpMethod.POST, "/project/create").authenticated()
  101. .antMatchers(HttpMethod.POST, "/project/members").access("@authTools.checkProjOwnershipParam(request)")
  102. .antMatchers(HttpMethod.POST, "/project/relate/code").access("@authTools.checkProjOwnershipParam(request)")
  103. .antMatchers(HttpMethod.POST, "/project/relate").access("@authTools.checkProjOwnershipParam(request)")
  104. // SQL Controller
  105. .antMatchers(HttpMethod.GET, "/sql/instances").access("@authTools.checkProjOwnershipParam(request)")
  106. .antMatchers(HttpMethod.POST, "/sql/exec").authenticated()
  107. // Tree Nodes Controller
  108. .antMatchers(HttpMethod.GET, "/tree/{projectId}").access("@authTools.checkProjOwnership(#projectId)")
  109. .antMatchers(HttpMethod.GET, "/tree/node/type/task/{projectId}").access("@authTools.checkProjOwnership(#projectId)")
  110. .antMatchers(HttpMethod.POST, "/tree/node").authenticated()
  111. .antMatchers(HttpMethod.POST, "/tree/subNode/{fatherId}").access("@authTools.checkTreeNodeOwnership(#fatherId)")
  112. .antMatchers(HttpMethod.PUT, "/tree/node").access("@authTools.checkTreeNodeOwnershipBody(request)")
  113. .antMatchers(HttpMethod.DELETE, "/tree/node/{nodeId}").access("@authTools.checkTreeNodeOwnership(#nodeId)")
  114. // Swagger
  115. .antMatchers("/**/*swagger*/**").permitAll()
  116. .antMatchers("/**/*api-docs*/**").permitAll()
  117. .antMatchers("/**/hook").permitAll()
  118. .antMatchers("/**/query/**").permitAll()
  119. // 跨域的 Options 请求进行放行
  120. // .requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
  121. // .antMatchers("/**").hasAnyRole(WebSecurityConstants.STUDENT_ROLE, WebSecurityConstants.ADMIN_ROLE, WebSecurityConstants.TEACHER_ROLE)
  122. // 拒绝其他请求
  123. .anyRequest().denyAll();
  124. }
  125. /**
  126. * 本地测试时的跨域设置
  127. */
  128. @Bean
  129. CorsConfigurationSource corsConfigurationSource() {
  130. return httpServletRequest -> {
  131. CorsConfiguration cfg = new CorsConfiguration();
  132. cfg.addAllowedHeader("*");
  133. cfg.addAllowedMethod("*");
  134. cfg.addAllowedOrigin("*");
  135. cfg.setAllowCredentials(true);
  136. cfg.checkOrigin("*");
  137. return cfg;
  138. };
  139. }
  140. }