WebSecurityConfig.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package cn.seecoder.fdroidrepository.config;
  2. import cn.seecoder.fdroidrepository.Service.ServiceImpl.UserServiceImpl;
  3. import cn.seecoder.fdroidrepository.security.JwtAuthenticationEntryPoint;
  4. import cn.seecoder.fdroidrepository.security.JwtAuthenticationTokenFilter;
  5. import cn.seecoder.fdroidrepository.utils.JwtUtil;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.core.env.Environment;
  10. import org.springframework.security.authentication.AuthenticationManager;
  11. import org.springframework.security.config.BeanIds;
  12. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  13. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  14. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  15. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  16. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  17. import org.springframework.security.config.http.SessionCreationPolicy;
  18. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  19. import org.springframework.security.crypto.password.PasswordEncoder;
  20. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  21. @Configuration(proxyBeanMethods = false)
  22. @EnableGlobalMethodSecurity(prePostEnabled = true)
  23. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  24. /**
  25. * The constant AUTHORIZATION_HEADER.
  26. */
  27. public static final String AUTHORIZATION_HEADER = "Authorization";
  28. /**
  29. * The constant AUTHORIZATION_TOKEN.
  30. */
  31. public static final String AUTHORIZATION_TOKEN = "access_token";
  32. /**
  33. * The constant SECURITY_IGNORE_URLS_SPILT_CHAR.
  34. */
  35. public static final String SECURITY_IGNORE_URLS_SPILT_CHAR = ",";
  36. /**
  37. * The constant TOKEN_PREFIX.
  38. */
  39. public static final String TOKEN_PREFIX = "Bearer ";
  40. @Autowired
  41. private UserServiceImpl userDetailsService;
  42. @Autowired
  43. private JwtAuthenticationEntryPoint unauthorizedHandler;
  44. @Autowired
  45. private JwtUtil tokenProvider;
  46. @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
  47. @Override
  48. public AuthenticationManager authenticationManagerBean() throws Exception {
  49. return super.authenticationManagerBean();
  50. }
  51. @Override
  52. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  53. auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
  54. }
  55. @Override
  56. public void configure(WebSecurity web) {
  57. String ignoreURLs = "/public/**,/open-api/**,/static/**,/css/**,/js/**,/images/**,/api/**";
  58. for (String ignoreURL : ignoreURLs.trim().split(SECURITY_IGNORE_URLS_SPILT_CHAR)) {
  59. web.ignoring().antMatchers(ignoreURL.trim());
  60. }
  61. }
  62. @Override
  63. protected void configure(HttpSecurity http) throws Exception {
  64. http.authorizeRequests().anyRequest().authenticated().and()
  65. // custom token authorize exception handler
  66. .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
  67. // since we use jwt, session is not necessary
  68. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
  69. // since we use jwt, csrf is not necessary
  70. .csrf().disable();
  71. http.addFilterBefore(new JwtAuthenticationTokenFilter(tokenProvider),
  72. UsernamePasswordAuthenticationFilter.class);
  73. // disable cache
  74. http.headers().cacheControl();
  75. }
  76. /**
  77. * Password encoder password encoder.
  78. *
  79. * @return the password encoder
  80. */
  81. @Bean
  82. public static PasswordEncoder passwordEncoder() {
  83. return new BCryptPasswordEncoder();
  84. }
  85. }