WebSecurityConfig.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package com.izouma.nineth.security;
  2. import org.springframework.beans.factory.annotation.Qualifier;
  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.core.userdetails.UserDetailsService;
  16. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  17. import org.springframework.security.crypto.password.PasswordEncoder;
  18. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  19. @EnableWebSecurity
  20. @EnableGlobalMethodSecurity(prePostEnabled = true)
  21. @EnableConfigurationProperties({JwtConfig.class})
  22. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  23. private final JwtAuthenticationEntryPoint unauthorizedHandler;
  24. private final UserDetailsService userDetailsService;
  25. private final JwtAuthorizationTokenFilter authenticationTokenFilter;
  26. private final String tokenHeader;
  27. public WebSecurityConfig(JwtAuthenticationEntryPoint unauthorizedHandler,
  28. @Qualifier("jwtUserDetailsService") UserDetailsService userDetailsService,
  29. JwtAuthorizationTokenFilter authenticationTokenFilter,
  30. @Value("${jwt.header}") String tokenHeader) {
  31. this.unauthorizedHandler = unauthorizedHandler;
  32. this.userDetailsService = userDetailsService;
  33. this.authenticationTokenFilter = authenticationTokenFilter;
  34. this.tokenHeader = tokenHeader;
  35. }
  36. @Override
  37. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  38. auth.userDetailsService(userDetailsService)
  39. .passwordEncoder(passwordEncoderBean());
  40. }
  41. @Bean
  42. public PasswordEncoder passwordEncoderBean() {
  43. return new BCryptPasswordEncoder(4);
  44. }
  45. @Bean
  46. @Override
  47. public AuthenticationManager authenticationManagerBean() throws Exception {
  48. return super.authenticationManagerBean();
  49. }
  50. @Override
  51. protected void configure(HttpSecurity httpSecurity) throws Exception {
  52. // We don't need CSRF for this example
  53. httpSecurity.csrf().disable()
  54. .cors().and()
  55. // dont authenticate this particular request
  56. .authorizeRequests()
  57. //swagger-ui放行路径
  58. .antMatchers("/v2/api-docs", "/swagger-ui.html", "/swagger-resources/**", "/webjars/**").permitAll()
  59. .antMatchers("/user/register").permitAll()
  60. .antMatchers("/upload/**").permitAll()
  61. .antMatchers("/files/**").permitAll()
  62. .antMatchers("/static/**").permitAll()
  63. .antMatchers("/auth/**").permitAll()
  64. .antMatchers("/captcha/**").permitAll()
  65. .antMatchers("/admin/**").permitAll()
  66. .antMatchers("/systemVariable/all").permitAll()
  67. .antMatchers("/**/excel").permitAll()
  68. .antMatchers("/wx/**").permitAll()
  69. .antMatchers("/sms/sendVerify").permitAll()
  70. .antMatchers("/sms/sendSecureVerify").permitAll()
  71. .antMatchers("/sms/verify").permitAll()
  72. .antMatchers("/error").permitAll()
  73. .antMatchers("/401").permitAll()
  74. .antMatchers("/404").permitAll()
  75. .antMatchers("/500").permitAll()
  76. .antMatchers("/MP_verify*").permitAll()
  77. .antMatchers("/payOrder/**").permitAll()
  78. .antMatchers("/notify/**").permitAll()
  79. .antMatchers("/banner/all").permitAll()
  80. .antMatchers("/setting/all").permitAll()
  81. .antMatchers("/setting/byFlag").permitAll()
  82. .antMatchers("/collection/all").permitAll()
  83. .antMatchers("/collection/get/**").permitAll()
  84. .antMatchers("/asset/get/**").permitAll()
  85. .antMatchers("/asset/tokenHistory").permitAll()
  86. .antMatchers("/user/all").permitAll()
  87. .antMatchers("/user/get/*").permitAll()
  88. .antMatchers("/news/all").permitAll()
  89. .antMatchers("/news/get/*").permitAll()
  90. .antMatchers("/user/forgotPassword").permitAll()
  91. .antMatchers("/sysConfig/get/*").permitAll()
  92. .antMatchers("/sysConfig/getDecimal/*").permitAll()
  93. .antMatchers("/user/code2openId").permitAll()
  94. .antMatchers("/blindBoxItem/all").permitAll()
  95. .antMatchers("/collection/recommend").permitAll()
  96. .antMatchers("/order/**/status", "/giftOrder/*/status", "/mintOrder/*/status", "/rechargeOrder/*/status", "/auctionOrder/*/status").permitAll()
  97. .antMatchers("/mintOrder/**/status").permitAll()
  98. .antMatchers("/activity/all").permitAll()
  99. .antMatchers("/activity/get/*").permitAll()
  100. .antMatchers("/mintActivity/all").permitAll()
  101. .antMatchers("/mintActivity/get/**").permitAll()
  102. .antMatchers("/purchaseLevel/all").permitAll()
  103. .antMatchers("/purchaseLevel/get/**").permitAll()
  104. .antMatchers("/news/all").permitAll()
  105. .antMatchers("/news/get/**").permitAll()
  106. .antMatchers("/druid/**").permitAll()
  107. .antMatchers("/identityAuth/autoAuth").permitAll()
  108. .antMatchers("/statistic/weekTop").permitAll()
  109. .antMatchers("/showroom/all").permitAll()
  110. .antMatchers("/showroom/get/**").permitAll()
  111. .antMatchers("/testClass/**").permitAll()
  112. .antMatchers("/appVersion/**").permitAll()
  113. .antMatchers("/sandpay/**").permitAll()
  114. .antMatchers("/hmpay/**").permitAll()
  115. .antMatchers("/payease/**").permitAll()
  116. .antMatchers("/order/calcSettle").permitAll()
  117. .antMatchers("/auctionOrder/all").permitAll()
  118. .antMatchers("/auctionOrder/get/**").permitAll()
  119. .antMatchers("/auctionActivity/all").permitAll()
  120. .antMatchers("/auctionActivity/get/**").permitAll()
  121. .antMatchers("/auctionRecord/all").permitAll()
  122. .antMatchers("/ossNotify").permitAll()
  123. .antMatchers("/priceList/list").permitAll()
  124. .antMatchers("/priceList/priceListVo").permitAll()
  125. .antMatchers("/user/collectionInvitorList").permitAll()
  126. .antMatchers("/auth/oasisLogin").permitAll()
  127. .antMatchers("/auth/oasisLoginPhone").permitAll()
  128. .antMatchers("/payOrder/v2/**/sandQuick").permitAll()
  129. .antMatchers("/pay/v2/**/sandQuick").permitAll()
  130. .antMatchers("/user/faceAuthNotify/*").permitAll()
  131. .antMatchers("/blindBoxItem/rare/*").permitAll()
  132. .antMatchers("/user/synchronizationData").permitAll()
  133. .antMatchers("/rarityLabel/label/*").permitAll()
  134. .antMatchers("/collection/count").permitAll()
  135. .antMatchers("/asset/*/metaPlayerRole").permitAll()
  136. .antMatchers("/metaPlayerInfo/**").permitAll()
  137. .antMatchers("/metaSpatialInfo/**").permitAll()
  138. .antMatchers("/onOff/**").permitAll()
  139. .antMatchers("/metaBonusScene/**").permitAll()
  140. .antMatchers("/alipay/notify").permitAll()
  141. .antMatchers("/metaPlayerWear/**").permitAll()
  142. .antMatchers("/userHold/app/top").permitAll()
  143. .antMatchers("/company/get/*").permitAll()
  144. .antMatchers("/websocket/**").permitAll()
  145. .antMatchers("/user/websocket/*").permitAll()
  146. .antMatchers("/purchaseLevel/websocket/*").permitAll()
  147. .antMatchers("/metaAdvertRecord/metaQuery").permitAll()
  148. .antMatchers("/metaTask/**").permitAll()
  149. .antMatchers("/asset/destroy").permitAll()
  150. .antMatchers("/user/topTen").permitAll()
  151. .antMatchers("/metaUserTaskProgress/**").permitAll()
  152. .antMatchers("/metaUserGold/**").permitAll()
  153. .antMatchers("/metaTask/findAll").permitAll()
  154. .antMatchers("/metaTaskToUser/**").permitAll()
  155. .antMatchers("/metaTaskActivity/queryPublishActivity").permitAll()
  156. .antMatchers("/metaAdvertRecord/**").permitAll()
  157. .antMatchers("/metaDestroyActivity/*/metaQuery").permitAll()
  158. .antMatchers("/metaResourceVersion/**").permitAll()
  159. .antMatchers("/asset/topTen").permitAll()
  160. .antMatchers("/metaUser/internalTest").permitAll()
  161. .antMatchers("/metaShowRoomAsset/**").permitAll()
  162. .antMatchers("/metaCacheable/clearMMO").permitAll()
  163. .antMatchers("/u951658VPf.txt").permitAll()
  164. .antMatchers("/u9s1658vPf.txt").permitAll()
  165. // all other requests need to be authenticated
  166. .anyRequest().authenticated().and()
  167. // make sure we use stateless session; session won't be used to
  168. // store user's state.
  169. .exceptionHandling().authenticationEntryPoint(unauthorizedHandler)
  170. .and().sessionManagement()
  171. .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  172. // Add a filter to validate the tokens with every request
  173. httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
  174. }
  175. @Override
  176. public void configure(WebSecurity web) throws Exception {
  177. // AuthenticationTokenFilter will ignore the below paths
  178. web.ignoring()
  179. .antMatchers("/auth/**")
  180. .antMatchers("/.well-known/**")
  181. // allow anonymous resource requests
  182. .and()
  183. .ignoring()
  184. .antMatchers(
  185. HttpMethod.GET,
  186. "/",
  187. "/*.html",
  188. "/**/favicon.ico",
  189. "/**/*.html",
  190. "/**/*.css",
  191. "/**/*.js"
  192. );
  193. }
  194. }