WebSecurityConfig.java 10 KB

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