WebSecurityConfig.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.ydtech.config;
  2. import com.ydtech.security.JwtAuthenticationFilter;
  3. import com.ydtech.security.JwtAuthenticationProvider;
  4. import com.ydtech.security.UserDetailsServiceImpl;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.security.authentication.AuthenticationManager;
  9. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  10. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  11. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  12. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  13. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  14. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  15. import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;
  16. @Configuration
  17. @EnableWebSecurity
  18. @EnableGlobalMethodSecurity(prePostEnabled = true)
  19. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  20. @Autowired
  21. private UserDetailsServiceImpl userDetailsService;
  22. @Autowired
  23. private MasterPasswordConfig masterPasswordConfig;
  24. @Override
  25. public void configure(AuthenticationManagerBuilder auth) throws Exception {
  26. // 使用自定义登录身份认证组件
  27. auth.authenticationProvider(new JwtAuthenticationProvider(userDetailsService, masterPasswordConfig));
  28. }
  29. @Override
  30. protected void configure(HttpSecurity http) throws Exception {
  31. // 禁用 csrf, 由于使用的是JWT,我们这里不需要csrf
  32. http.cors().and().csrf().disable()
  33. .authorizeRequests()
  34. // 跨域预检请求
  35. // .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
  36. // web jars
  37. .antMatchers("/webjars/**").permitAll()
  38. // 查看SQL监控(druid)
  39. .antMatchers("/druid/**").permitAll()
  40. // 首页和登录页面
  41. .antMatchers("/").permitAll()
  42. .antMatchers("/login").permitAll()
  43. .antMatchers("/sendMsg").permitAll()
  44. .antMatchers("/loginByPhone").permitAll()
  45. .antMatchers("/user/findById").permitAll()
  46. .antMatchers("/esmUserInternalcheck/userRegist").permitAll()
  47. .antMatchers("/insOrder/queryOrder").permitAll()
  48. .antMatchers("/insOrder/getPayCode").permitAll()
  49. .antMatchers("/zm/Underwriting").permitAll()//自动核保回调
  50. .antMatchers("/zm/ArtificialUnderwriting").permitAll()//人工核保回调
  51. .antMatchers("/esmUserInternalcheck/updateName").permitAll()
  52. // swagger
  53. .antMatchers("/doc.html").permitAll()
  54. .antMatchers("/swagger**/**").permitAll()
  55. .antMatchers("/webjars/**").permitAll()
  56. .antMatchers("/v2/**").permitAll()
  57. // .antMatchers("/swagger-ui.html").permitAll()
  58. // .antMatchers("/swagger-resources").permitAll()
  59. // .antMatchers("/v2/api-docs").permitAll()
  60. // .antMatchers("/webjars/springfox-swagger-ui/**").permitAll()
  61. // 字典
  62. .antMatchers("/dict/**").permitAll()
  63. //静态资源
  64. .antMatchers("/static/**").permitAll()
  65. //api
  66. .antMatchers("/api/**").permitAll()
  67. //app
  68. .antMatchers("/app/android/**").permitAll()
  69. //upload
  70. .antMatchers("/upload/**").permitAll()
  71. // 验证码
  72. .antMatchers("/captcha.jpg**").permitAll()
  73. // 服务监控
  74. .antMatchers("/actuator/**").permitAll()
  75. .antMatchers("/API/insCBIT/**").permitAll()
  76. .antMatchers("/CBIT/API/insCBIT/**").permitAll()
  77. .antMatchers("/CBITAPI/insCBIT/**").permitAll()
  78. .antMatchers("/api/yongan/**").permitAll()
  79. .antMatchers("/order/yongAn/**").permitAll()
  80. .antMatchers("/api/insRenbao/**").permitAll()
  81. .antMatchers("/ins/**").permitAll()
  82. .antMatchers("/wechat/**").permitAll()
  83. .antMatchers("/esm/policy/**").permitAll()
  84. // 中煤回调
  85. .antMatchers("/order/zhongMeiApi/underwritingCallback").permitAll()
  86. .antMatchers("/order/zhongMeiApi/auditCallback").permitAll()
  87. // 其他所有请求需要身份认证
  88. .anyRequest().authenticated();
  89. // 退出登录处理器
  90. http.logout().logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler());
  91. // 开启登录认证流程过滤器
  92. // http.addFilterBefore(new JwtLoginFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class);
  93. // 访问控制时登录状态检查过滤器
  94. http.addFilterBefore(new JwtAuthenticationFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class);
  95. }
  96. @Bean
  97. @Override
  98. public AuthenticationManager authenticationManager() throws Exception {
  99. return super.authenticationManager();
  100. }
  101. }