WebSecurityConfig.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.genersoft.iot.vmp.conf.security;
  2. import com.genersoft.iot.vmp.conf.UserSetup;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  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.authentication.dao.DaoAuthenticationProvider;
  10. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  11. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  12. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  13. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  14. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  15. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  16. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  17. import java.util.List;
  18. /**
  19. * 配置Spring Security
  20. */
  21. @Configuration
  22. @EnableWebSecurity
  23. @EnableGlobalMethodSecurity(prePostEnabled = true)
  24. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  25. private final static Logger logger = LoggerFactory.getLogger(WebSecurityConfig.class);
  26. @Autowired
  27. private UserSetup userSetup;
  28. @Autowired
  29. private DefaultUserDetailsServiceImpl userDetailsService;
  30. /**
  31. * 登出成功的处理
  32. */
  33. @Autowired
  34. private LoginFailureHandler loginFailureHandler;
  35. /**
  36. * 登录成功的处理
  37. */
  38. @Autowired
  39. private LoginSuccessHandler loginSuccessHandler;
  40. /**
  41. * 登出成功的处理
  42. */
  43. @Autowired
  44. private LogoutHandler logoutHandler;
  45. /**
  46. * 未登录的处理
  47. */
  48. @Autowired
  49. private AnonymousAuthenticationEntryPoint anonymousAuthenticationEntryPoint;
  50. // /**
  51. // * 超时处理
  52. // */
  53. // @Autowired
  54. // private InvalidSessionHandler invalidSessionHandler;
  55. // /**
  56. // * 顶号处理
  57. // */
  58. // @Autowired
  59. // private SessionInformationExpiredHandler sessionInformationExpiredHandler;
  60. // /**
  61. // * 登录用户没有权限访问资源
  62. // */
  63. // @Autowired
  64. // private LoginUserAccessDeniedHandler accessDeniedHandler;
  65. /**
  66. * 描述: 静态资源放行,这里的放行,是不走 Spring Security 过滤器链
  67. **/
  68. @Override
  69. public void configure(WebSecurity web) {
  70. if (!userSetup.isInterfaceAuthentication()) {
  71. web.ignoring().antMatchers("**");
  72. }else {
  73. // 可以直接访问的静态数据
  74. web.ignoring()
  75. .antMatchers("/")
  76. .antMatchers("/#/**")
  77. .antMatchers("/static/**")
  78. .antMatchers("/index.html")
  79. .antMatchers("/doc.html") // "/webjars/**", "/swagger-resources/**", "/v3/api-docs/**"
  80. .antMatchers("/webjars/**")
  81. .antMatchers("/swagger-resources/**")
  82. .antMatchers("/v3/api-docs/**")
  83. .antMatchers("/js/**");
  84. List<String> interfaceAuthenticationExcludes = userSetup.getInterfaceAuthenticationExcludes();
  85. for (String interfaceAuthenticationExclude : interfaceAuthenticationExcludes) {
  86. if (interfaceAuthenticationExclude.split("/").length < 4 ) {
  87. logger.warn("{}不满足两级目录,已忽略", interfaceAuthenticationExclude);
  88. }else {
  89. web.ignoring().antMatchers(interfaceAuthenticationExclude);
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * 配置认证方式
  96. * @param auth
  97. * @throws Exception
  98. */
  99. @Override
  100. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  101. DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
  102. // 设置不隐藏 未找到用户异常
  103. provider.setHideUserNotFoundExceptions(true);
  104. // 用户认证service - 查询数据库的逻辑
  105. provider.setUserDetailsService(userDetailsService);
  106. // 设置密码加密算法
  107. provider.setPasswordEncoder(passwordEncoder());
  108. auth.authenticationProvider(provider);
  109. }
  110. @Override
  111. protected void configure(HttpSecurity http) throws Exception {
  112. http.cors().and().csrf().disable();
  113. // 设置允许添加静态文件
  114. http.headers().contentTypeOptions().disable();
  115. http.authorizeRequests()
  116. // 放行接口
  117. .antMatchers("/api/user/login","/index/hook/**").permitAll()
  118. // 除上面外的所有请求全部需要鉴权认证
  119. .anyRequest().authenticated()
  120. // 异常处理(权限拒绝、登录失效等)
  121. .and().exceptionHandling()
  122. .authenticationEntryPoint(anonymousAuthenticationEntryPoint)//匿名用户访问无权限资源时的异常处理
  123. // .accessDeniedHandler(accessDeniedHandler)//登录用户没有权限访问资源
  124. // 登入
  125. .and().formLogin().permitAll()//允许所有用户
  126. .successHandler(loginSuccessHandler)//登录成功处理逻辑
  127. .failureHandler(loginFailureHandler)//登录失败处理逻辑
  128. // 登出
  129. .and().logout().logoutUrl("/api/user/logout").permitAll()//允许所有用户
  130. .logoutSuccessHandler(logoutHandler)//登出成功处理逻辑
  131. .deleteCookies("JSESSIONID")
  132. // 会话管理
  133. // .and().sessionManagement().invalidSessionStrategy(invalidSessionHandler) // 超时处理
  134. // .maximumSessions(1)//同一账号同时登录最大用户数
  135. // .expiredSessionStrategy(sessionInformationExpiredHandler) // 顶号处理
  136. ;
  137. }
  138. /**
  139. * 描述: 密码加密算法 BCrypt 推荐使用
  140. **/
  141. @Bean
  142. public BCryptPasswordEncoder passwordEncoder() {
  143. return new BCryptPasswordEncoder();
  144. }
  145. /**
  146. * 描述: 注入AuthenticationManager管理器
  147. **/
  148. @Override
  149. @Bean
  150. public AuthenticationManager authenticationManager() throws Exception {
  151. return super.authenticationManager();
  152. }
  153. }