SecurityConfig.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package com.ruoyi.framework.config;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.http.HttpMethod;
  5. import org.springframework.security.authentication.AuthenticationManager;
  6. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  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.WebSecurityConfigurerAdapter;
  10. import org.springframework.security.config.http.SessionCreationPolicy;
  11. import org.springframework.security.core.userdetails.UserDetailsService;
  12. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  13. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  14. import org.springframework.security.web.authentication.logout.LogoutFilter;
  15. import org.springframework.web.filter.CorsFilter;
  16. import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
  17. import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
  18. import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
  19. import javax.annotation.Resource;
  20. /**
  21. * spring security配置
  22. *
  23. * @author ruoyi
  24. */
  25. @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
  26. public class SecurityConfig extends WebSecurityConfigurerAdapter
  27. {
  28. /**
  29. * 自定义用户认证逻辑
  30. */
  31. @Resource
  32. private UserDetailsService userDetailsService;
  33. /**
  34. * 认证失败处理类
  35. */
  36. @Autowired
  37. private AuthenticationEntryPointImpl unauthorizedHandler;
  38. /**
  39. * 退出处理类
  40. */
  41. @Autowired
  42. private LogoutSuccessHandlerImpl logoutSuccessHandler;
  43. /**
  44. * token认证过滤器
  45. */
  46. @Autowired
  47. private JwtAuthenticationTokenFilter authenticationTokenFilter;
  48. /**
  49. * 跨域过滤器
  50. */
  51. @Autowired
  52. private CorsFilter corsFilter;
  53. /**
  54. * 解决 无法直接注入 AuthenticationManager
  55. *
  56. * @return
  57. * @throws Exception
  58. */
  59. @Bean
  60. @Override
  61. public AuthenticationManager authenticationManagerBean() throws Exception
  62. {
  63. return super.authenticationManagerBean();
  64. }
  65. /**
  66. * anyRequest | 匹配所有请求路径
  67. * access | SpringEl表达式结果为true时可以访问
  68. * anonymous | 匿名可以访问
  69. * denyAll | 用户不能访问
  70. * fullyAuthenticated | 用户完全认证可以访问(非remember-me下自动登录)
  71. * hasAnyAuthority | 如果有参数,参数表示权限,则其中任何一个权限可以访问
  72. * hasAnyRole | 如果有参数,参数表示角色,则其中任何一个角色可以访问
  73. * hasAuthority | 如果有参数,参数表示权限,则其权限可以访问
  74. * hasIpAddress | 如果有参数,参数表示IP地址,如果用户IP和参数匹配,则可以访问
  75. * hasRole | 如果有参数,参数表示角色,则其角色可以访问
  76. * permitAll | 用户可以任意访问
  77. * rememberMe | 允许通过remember-me登录的用户访问
  78. * authenticated | 用户登录后可访问
  79. */
  80. @Override
  81. protected void configure(HttpSecurity httpSecurity) throws Exception
  82. {
  83. httpSecurity
  84. // CSRF禁用,因为不使用session
  85. .csrf().disable()
  86. // 认证失败处理类
  87. .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
  88. // 基于token,所以不需要session
  89. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
  90. // 过滤请求
  91. .authorizeRequests()
  92. // 对于登录login 验证码captchaImage 允许匿名访问
  93. .antMatchers("/login", "/captchaImage","/getAzureAccessToken", "/getAccessToken").anonymous()
  94. .antMatchers(
  95. HttpMethod.GET,
  96. "/*.html",
  97. "/**/*.html",
  98. "/**/*.css",
  99. "/**/*.js"
  100. ).permitAll()
  101. .antMatchers("/profile/**").anonymous()
  102. .antMatchers("/common/download/exportDevList").anonymous()
  103. .antMatchers("/common/download**").anonymous()
  104. .antMatchers("/common/download/resource**").anonymous()
  105. .antMatchers("/swagger-ui.html").anonymous()
  106. .antMatchers("/swagger-resources/**").anonymous()
  107. .antMatchers("/webjars/**").anonymous()
  108. .antMatchers("/*/api-docs").anonymous()
  109. .antMatchers("/druid/**").anonymous()
  110. .antMatchers("/static/**").anonymous()
  111. .antMatchers("/common/template").anonymous()
  112. .antMatchers("/plant/generatereport/export").anonymous()
  113. .antMatchers("/plant/meeting/meetingReport").anonymous()
  114. .antMatchers("/training/training/trainingReport").anonymous()
  115. .antMatchers("/pdf/**").anonymous()
  116. .antMatchers("/**/exportPDF").anonymous()
  117. .antMatchers("/ehs/approvedanger/processImg/**").anonymous()
  118. .antMatchers("/sems/historyYlrq/exportPDFForYear").anonymous()
  119. .antMatchers("/sems/historyYlgd/exportPDFForYear").anonymous()
  120. .antMatchers("/invoice/bookingworkticket/word").anonymous()
  121. // 除上面外的所有请求全部需要鉴权认证
  122. .anyRequest().authenticated()
  123. .and()
  124. .headers().frameOptions().disable();
  125. httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
  126. // 添加JWT filter
  127. httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
  128. // 添加CORS filter
  129. httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
  130. httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);
  131. }
  132. /**
  133. * 强散列哈希加密实现
  134. */
  135. @Bean
  136. public BCryptPasswordEncoder bCryptPasswordEncoder()
  137. {
  138. return new BCryptPasswordEncoder();
  139. }
  140. /**
  141. * 身份认证接口
  142. */
  143. @Override
  144. protected void configure(AuthenticationManagerBuilder auth) throws Exception
  145. {
  146. auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
  147. }
  148. }