FebsSecurityConfigure.java 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package cc.mrbird.febs.auth.configure;
  2. import cc.mrbird.febs.auth.handler.FebsWebLoginFailureHandler;
  3. import cc.mrbird.febs.auth.handler.FebsWebLoginSuccessHandler;
  4. import cc.mrbird.febs.auth.filter.ValidateCodeFilter;
  5. import cc.mrbird.febs.common.core.entity.constant.EndpointConstant;
  6. import lombok.RequiredArgsConstructor;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.core.annotation.Order;
  9. import org.springframework.security.authentication.AuthenticationManager;
  10. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  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.core.userdetails.UserDetailsService;
  15. import org.springframework.security.crypto.password.PasswordEncoder;
  16. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  17. /**
  18. * WebSecurity配置
  19. *
  20. * @author MrBird
  21. */
  22. @Order(2)
  23. @EnableWebSecurity
  24. @RequiredArgsConstructor
  25. public class FebsSecurityConfigure extends WebSecurityConfigurerAdapter {
  26. private final UserDetailsService userDetailService;
  27. private final ValidateCodeFilter validateCodeFilter;
  28. private final PasswordEncoder passwordEncoder;
  29. private final FebsWebLoginSuccessHandler successHandler;
  30. private final FebsWebLoginFailureHandler failureHandler;
  31. @Bean
  32. @Override
  33. public AuthenticationManager authenticationManagerBean() throws Exception {
  34. return super.authenticationManagerBean();
  35. }
  36. @Override
  37. protected void configure(HttpSecurity http) throws Exception {
  38. http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
  39. .requestMatchers()
  40. .antMatchers(EndpointConstant.OAUTH_ALL, EndpointConstant.LOGIN)
  41. .and()
  42. .authorizeRequests()
  43. .antMatchers(EndpointConstant.OAUTH_ALL).authenticated()
  44. .and()
  45. .formLogin()
  46. .loginPage(EndpointConstant.LOGIN)
  47. .loginProcessingUrl(EndpointConstant.LOGIN)
  48. .successHandler(successHandler)
  49. .failureHandler(failureHandler)
  50. .permitAll()
  51. .and().csrf().disable()
  52. .httpBasic().disable();
  53. }
  54. @Override
  55. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  56. auth.userDetailsService(userDetailService).passwordEncoder(passwordEncoder);
  57. }
  58. }