package com.ydtech.config; import com.ydtech.security.JwtAuthenticationFilter; import com.ydtech.security.JwtAuthenticationProvider; import com.ydtech.security.UserDetailsServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler; @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsServiceImpl userDetailsService; @Autowired private MasterPasswordConfig masterPasswordConfig; @Value("${upload.file.url}") private String uploadFileUrl; @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { // 使用自定义登录身份认证组件 auth.authenticationProvider(new JwtAuthenticationProvider(userDetailsService, masterPasswordConfig)); } @Override protected void configure(HttpSecurity http) throws Exception { // 禁用 csrf, 由于使用的是JWT,我们这里不需要csrf http.cors().and().csrf().disable() .authorizeRequests() // 跨域预检请求 // .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() // web jars .antMatchers("/webjars/**").permitAll() // 查看SQL监控(druid) .antMatchers("/druid/**").permitAll() // 首页和登录页面 .antMatchers("/").permitAll() .antMatchers("/login").permitAll() .antMatchers("/sendMsg").permitAll() .antMatchers("/loginByPhone").permitAll() .antMatchers("/user/findById").permitAll() .antMatchers("/esmUserInternalcheck/userRegist").permitAll() .antMatchers("/insOrder/queryOrder").permitAll() .antMatchers("/insOrder/getPayCode").permitAll() .antMatchers("/zm/Underwriting").permitAll()//自动核保回调 .antMatchers("/zm/ArtificialUnderwriting").permitAll()//人工核保回调 .antMatchers("/esmUserInternalcheck/updateName").permitAll() // swagger .antMatchers("/doc.html").permitAll() .antMatchers("/swagger**/**").permitAll() .antMatchers("/webjars/**").permitAll() .antMatchers("/v2/**").permitAll() // .antMatchers("/swagger-ui.html").permitAll() // .antMatchers("/swagger-resources").permitAll() // .antMatchers("/v2/api-docs").permitAll() // .antMatchers("/webjars/springfox-swagger-ui/**").permitAll() // 字典 .antMatchers("/dict/**").permitAll() //静态资源 .antMatchers("/static/**").permitAll() //api .antMatchers("/api/**").permitAll() //app .antMatchers("/app/android/**").permitAll() // 文件上传访问路径 .antMatchers(uploadFileUrl + "**").permitAll() // 验证码 .antMatchers("/captcha.jpg**").permitAll() // 服务监控 .antMatchers("/actuator/**").permitAll() .antMatchers("/API/insCBIT/**").permitAll() .antMatchers("/CBIT/API/insCBIT/**").permitAll() .antMatchers("/CBITAPI/insCBIT/**").permitAll() .antMatchers("/api/yongan/**").permitAll() .antMatchers("/order/yongAn/**").permitAll() .antMatchers("/api/insRenbao/**").permitAll() .antMatchers("/ins/**").permitAll() .antMatchers("/wechat/**").permitAll() .antMatchers("/esm/policy/**").permitAll() // 中煤回调 .antMatchers("/order/zhongMeiApi/underwritingCallback").permitAll() .antMatchers("/order/zhongMeiApi/auditCallback").permitAll() // 其他所有请求需要身份认证 .anyRequest().authenticated(); // 退出登录处理器 http.logout().logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()); // 开启登录认证流程过滤器 // http.addFilterBefore(new JwtLoginFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class); // 访问控制时登录状态检查过滤器 http.addFilterBefore(new JwtAuthenticationFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class); } @Bean @Override public AuthenticationManager authenticationManager() throws Exception { return super.authenticationManager(); } }