WebSecurityConfig.java 4.8 KB

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