WebSecurityConfig.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package com.izouma.awesomeAdmin.security;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.http.HttpMethod;
  6. import org.springframework.security.authentication.AuthenticationManager;
  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.config.http.SessionCreationPolicy;
  14. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  15. import org.springframework.security.crypto.password.PasswordEncoder;
  16. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  17. @EnableWebSecurity
  18. @EnableGlobalMethodSecurity(prePostEnabled = true)
  19. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  20. @Autowired
  21. private JwtAuthenticationEntryPoint unauthorizedHandler;
  22. @Autowired
  23. private JwtUserDetailsService jwtUserDetailsService;
  24. // Custom JWT based security filter
  25. @Autowired
  26. JwtAuthorizationTokenFilter authenticationTokenFilter;
  27. @Value("${jwt.header}")
  28. private String tokenHeader;
  29. @Autowired
  30. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  31. auth.userDetailsService(jwtUserDetailsService)
  32. .passwordEncoder(passwordEncoderBean());
  33. }
  34. @Bean
  35. public PasswordEncoder passwordEncoderBean() {
  36. return new BCryptPasswordEncoder();
  37. }
  38. @Bean
  39. @Override
  40. public AuthenticationManager authenticationManagerBean() throws Exception {
  41. return super.authenticationManagerBean();
  42. }
  43. @Override
  44. protected void configure(HttpSecurity httpSecurity) throws Exception {
  45. // We don't need CSRF for this example
  46. httpSecurity.csrf().disable()
  47. .cors().and()
  48. // dont authenticate this particular request
  49. .authorizeRequests()
  50. //swagger-ui放行路径
  51. .antMatchers("/v2/api-docs", "/swagger-ui.html", "/swagger-resources/**", "/webjars/**").permitAll()
  52. .antMatchers("/user/register").permitAll()
  53. .antMatchers("/upload/**").permitAll()
  54. .antMatchers("/static/**").permitAll()
  55. .antMatchers("/auth/**").permitAll()
  56. .antMatchers("/admin/**").permitAll()
  57. .antMatchers("/orderNotify/**").permitAll()
  58. .antMatchers("/order/logistic").permitAll()
  59. .antMatchers("/systemVariable/all").permitAll()
  60. .antMatchers("/**/excel").permitAll()
  61. .antMatchers("/wx/**").permitAll()
  62. .antMatchers("/sms/sendVerify").permitAll()
  63. // all other requests need to be authenticated
  64. .anyRequest().authenticated().and()
  65. // make sure we use stateless session; session won't be used to
  66. // store user's state.
  67. .exceptionHandling().authenticationEntryPoint(unauthorizedHandler)
  68. .and().sessionManagement()
  69. .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  70. // Add a filter to validate the tokens with every request
  71. httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
  72. }
  73. @Override
  74. public void configure(WebSecurity web) throws Exception {
  75. // AuthenticationTokenFilter will ignore the below paths
  76. web.ignoring()
  77. .antMatchers("/auth/**")
  78. // allow anonymous resource requests
  79. .and()
  80. .ignoring()
  81. .antMatchers(
  82. HttpMethod.GET,
  83. "/",
  84. "/*.html",
  85. "/**/favicon.ico",
  86. "/**/*.html",
  87. "/**/*.css",
  88. "/**/*.js"
  89. );
  90. }
  91. }