11package com .admin4j .framework .security .configuration ;
22
33import com .admin4j .framework .security .ISecurityIgnoringUrl ;
4+ import com .admin4j .framework .security .authorization .PermissionAuthorizationManager ;
45import com .admin4j .framework .security .filter .ActuatorFilter ;
56import com .admin4j .framework .security .ignoringUrl .AnonymousAccessUrl ;
67import com .admin4j .framework .security .multi .MultiSecurityConfigurerAdapter ;
1617import org .springframework .context .annotation .Bean ;
1718import org .springframework .http .HttpMethod ;
1819import org .springframework .security .config .annotation .method .configuration .EnableGlobalMethodSecurity ;
20+ import org .springframework .security .config .annotation .web .AbstractRequestMatcherRegistry ;
1921import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
20- import org .springframework .security .config .annotation .web .configurers .ExpressionUrlAuthorizationConfigurer ;
22+ import org .springframework .security .config .annotation .web .configurers .AuthorizeHttpRequestsConfigurer ;
2123import org .springframework .security .config .http .SessionCreationPolicy ;
2224import org .springframework .security .web .AuthenticationEntryPoint ;
2325import org .springframework .security .web .SecurityFilterChain ;
3335
3436/**
3537 * TODO 需要注入,取消 UserDetailsServiceAutoConfiguration 开启
36- * value = { AuthenticationManager .class, AuthenticationProvider.class, UserDetailsService.class,
38+ * value = { PermissionAuthorizationManager .class, AuthenticationProvider.class, UserDetailsService.class,
3739 * AuthenticationManagerResolver.class },
3840 *
3941 * @author andanyang
@@ -79,7 +81,8 @@ public class SecurityConfiguration {
7981 CorsFilter corsFilter ;
8082 @ Autowired (required = false )
8183 MultiSecurityConfigurerAdapter multiSecurityConfigurerAdapter ;
82-
84+ @ Autowired (required = false )
85+ PermissionAuthorizationManager permissionAuthorizationManager ;
8386 /**
8487 * 取消ROLE_前缀
8588 */
@@ -131,11 +134,6 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
131134 // 添加Logout filter
132135 httpSecurity .logout ().logoutUrl (formLoginProperties .getLogOutProcessingUrl ()).permitAll ().logoutSuccessHandler (logoutSuccessHandler );
133136
134- // 授权请求配置
135- // 忽略URl配置
136- ignoringRequestMatcherRegistry (httpSecurity .authorizeRequests ());
137- // 除上面外的所有请求全部需要鉴权认证;其他路径必须验证
138- httpSecurity .authorizeRequests ().anyRequest ().authenticated ();
139137
140138 // 添加CORS filter
141139 if (corsFilter != null ) {
@@ -160,14 +158,31 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
160158 .permitAll ();
161159 }
162160
161+ // 授权请求配置 authorizeHttpRequests(6.0 新版) authorizeRequests(旧版) 区别
162+ // httpSecurity.authorizeRequests().anyRequest().authenticated();
163+ httpSecurity .authorizeHttpRequests (register -> {
164+
165+ // 忽略URl配置
166+ ignoringRequestMatcherRegistry (register );
167+ if (permissionAuthorizationManager != null ) {
168+ // 自定义授权
169+ register .anyRequest ().access (permissionAuthorizationManager );
170+ } else {
171+ // 除上面外的所有请求全部需要鉴权认证;其他路径必须验证
172+ register .anyRequest ().authenticated ();
173+ }
174+
175+ });
176+
163177 return httpSecurity .build ();
164178 }
165179
166180
167181 /**
168182 * 忽略URl配置
169183 */
170- private void ignoringRequestMatcherRegistry (ExpressionUrlAuthorizationConfigurer <HttpSecurity >.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry ) {
184+ private void ignoringRequestMatcherRegistry (AbstractRequestMatcherRegistry <AuthorizeHttpRequestsConfigurer <HttpSecurity >.AuthorizedUrl > matcherRegistry ) {
185+
171186
172187 if (securityIgnoringUrls != null && !securityIgnoringUrls .isEmpty ()) {
173188 securityIgnoringUrls .forEach (url -> {
@@ -177,33 +192,33 @@ private void ignoringRequestMatcherRegistry(ExpressionUrlAuthorizationConfigurer
177192 }
178193
179194 if (url .support () == null ) {
180- expressionInterceptUrlRegistry . antMatchers (url .ignoringUrls ()).permitAll ();
195+ matcherRegistry . mvcMatchers (url .ignoringUrls ()).permitAll ();
181196 } else {
182- expressionInterceptUrlRegistry .antMatchers (url .support (), url .ignoringUrls ()).permitAll ();
197+ matcherRegistry .antMatchers (url .support (), url .ignoringUrls ()).permitAll ();
183198 }
184199 });
185200 }
186201
187202 if (ignoringUrlProperties != null ) {
188203
189204 if (ignoringUrlProperties .getUris () != null && ignoringUrlProperties .getUris ().length > 0 ) {
190- expressionInterceptUrlRegistry .antMatchers (ignoringUrlProperties .getUris ()).permitAll ();
205+ matcherRegistry .antMatchers (ignoringUrlProperties .getUris ()).permitAll ();
191206 }
192207 if (ignoringUrlProperties .getGet () != null && ignoringUrlProperties .getGet ().length > 0 ) {
193- expressionInterceptUrlRegistry .antMatchers (HttpMethod .GET , ignoringUrlProperties .getGet ()).permitAll ();
208+ matcherRegistry .antMatchers (HttpMethod .GET , ignoringUrlProperties .getGet ()).permitAll ();
194209 }
195210
196211 if (ignoringUrlProperties .getPost () != null && ignoringUrlProperties .getPost ().length > 0 ) {
197- expressionInterceptUrlRegistry .antMatchers (HttpMethod .POST , ignoringUrlProperties .getPost ()).permitAll ();
212+ matcherRegistry .antMatchers (HttpMethod .POST , ignoringUrlProperties .getPost ()).permitAll ();
198213 }
199214 if (ignoringUrlProperties .getPut () != null && ignoringUrlProperties .getPut ().length > 0 ) {
200- expressionInterceptUrlRegistry .antMatchers (HttpMethod .PUT , ignoringUrlProperties .getPut ()).permitAll ();
215+ matcherRegistry .antMatchers (HttpMethod .PUT , ignoringUrlProperties .getPut ()).permitAll ();
201216 }
202217 if (ignoringUrlProperties .getPatch () != null && ignoringUrlProperties .getPatch ().length > 0 ) {
203- expressionInterceptUrlRegistry .antMatchers (HttpMethod .PATCH , ignoringUrlProperties .getPatch ()).permitAll ();
218+ matcherRegistry .antMatchers (HttpMethod .PATCH , ignoringUrlProperties .getPatch ()).permitAll ();
204219 }
205220 if (ignoringUrlProperties .getDelete () != null && ignoringUrlProperties .getDelete ().length > 0 ) {
206- expressionInterceptUrlRegistry .antMatchers (HttpMethod .DELETE , ignoringUrlProperties .getDelete ()).permitAll ();
221+ matcherRegistry .antMatchers (HttpMethod .DELETE , ignoringUrlProperties .getDelete ()).permitAll ();
207222 }
208223 }
209224
@@ -213,7 +228,7 @@ private void ignoringRequestMatcherRegistry(ExpressionUrlAuthorizationConfigurer
213228 Map <HttpMethod , String []> anonymousUrl = anonymousAccessUrl .getAnonymousUrl ();
214229 anonymousUrl .keySet ().forEach (i -> {
215230
216- expressionInterceptUrlRegistry .antMatchers (i , anonymousUrl .get (i )).permitAll ();
231+ matcherRegistry .antMatchers (i , anonymousUrl .get (i )).permitAll ();
217232 });
218233 }
219234 }
0 commit comments