WebSecurityConfig.java 5.4 KB

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