java - SpringSecurity:仅针对特定 URL 注册过滤器
问题描述
我正在为我的应用程序使用 WebSecurityConfigurerAdapter,它使用 jwt 进行身份验证并且一切正常(第一个代码片段);我现在要做的是为特定的 URL 模式添加另一个过滤器,但现在无论如何都会调用这个过滤器(第二个代码片段)。有人请解释我做错了什么?
这是我的代码:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors()
.and().csrf().disable().httpBasic().disable()
.authorizeRequests()
.antMatchers("/auth").permitAll()
.antMatchers("/v2/api-docs",
"/swagger-resources/**",
"/configuration/**",
"/swagger-ui.html",
"/webjars/**").permitAll()
.antMatchers(
"/",
"/refreshconfig",
"/launchGameForFun/**",
"/*.html",
"/*.gif",
"/favicon.ico",
"/**/*.html",
"/**/*.gif",
"/**/*.css",
"/**/*.js").permitAll()
.anyRequest().authenticated()
.and()
.exceptionHandling().authenticationEntryPoint(new CustomAuthenticationEntryPoint())
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilterBefore(new Slf4jMDCFilter(), JwtAuthorizationFilter.class)
.addFilter(new JwtAuthorizationFilter(authenticationManager()));
}
在上面添加此代码无法按预期工作:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors()
.and().csrf().disable().httpBasic().disable()
.authorizeRequests()
.antMatchers("/auth").permitAll()
.antMatchers("/v2/api-docs",
"/swagger-resources/**",
"/configuration/**",
"/swagger-ui.html",
"/webjars/**").permitAll()
.antMatchers(
"/",
"/refreshconfig",
"/launchGameForFun/**",
"/*.html",
"/*.gif",
"/favicon.ico",
"/**/*.html",
"/**/*.gif",
"/**/*.css",
"/**/*.js").permitAll()
.anyRequest().authenticated()
.and()
.exceptionHandling().authenticationEntryPoint(new CustomAuthenticationEntryPoint())
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilterBefore(new Slf4jMDCFilter(), JwtAuthorizationFilter.class)
.addFilter(new JwtAuthorizationFilter(authenticationManager()))
.antMatcher("/*/*wager")
.addFilterAfter(new MultiReadServletFilter(), JwtAuthorizationFilter.class)
.addFilterAfter(new XauthFilter(), MultiReadServletFilter.class);
}
更清楚地说:我希望为每个 URL(/*/*wager
包括)调用 Slf4jMDCFilter 和 JwtAuthorizationFilter,并且只为 调用 MultiReadServletFilter 和 XauthFilter /*/*wager
。显然 permitAll URL 在这两种情况下都被排除在外。
解决方案
推荐阅读
- html - favicon 不适用于 chrome 中的一个(令牌)页面
- javascript - 检测耳机按键反应原生
- jquery - 如何使用 jQuery 可排序接收和更新异步等待?
- r - 每次迭代读取和存储数据帧
- python - 如何解决 Keras 中 `TypeError: 'Tensor' object does not support item assignment`的问题
- arrays - ReDim 保留二维变量数组
- android - android如何将字节[]数据添加到云Firestore数据库
- javascript - 用于旋转横幅的同一 HTML 页面上的多个脚本
- javascript - 带有 REST API 的云存储提供商在中国上市
- javascript - 在odoo10中,每当我们单击记录时,如何在仪表板中启用寻呼机?