
在Spring Boot应用程序中集成JWT(JSON Web Token)进行认证时,一个常见的需求是只对特定URL模式的请求应用JWT过滤器,而不是所有请求。默认情况下,如果直接使用http.addFilterBefore(customJwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class),该自定义过滤器可能会在所有请求进入UsernamePasswordAuthenticationFilter之前被执行,这在某些场景下可能不是最优解,例如,对于公开的API或静态资源,我们不希望它们经过JWT认证逻辑。
为了实现对特定URL模式的精确过滤,Spring Security提供了AbstractAuthenticationProcessingFilter抽象类和RequestMatcher接口,它们是解决此类问题的关键。
AbstractAuthenticationProcessingFilter: 这是Spring Security中用于处理特定认证请求的抽象基类。它在内部持有一个RequestMatcher实例,只有当请求与该RequestMatcher匹配时,过滤器才会尝试进行认证处理(即调用attemptAuthentication方法)。这使得我们可以将认证逻辑与请求路径解耦,实现按需认证。
RequestMatcher: 这是一个核心接口,定义了如何判断一个HttpServletRequest是否匹配某种规则。Spring Security提供了多种内置实现,例如:
首先,我们需要修改原有的CustomJwtAuthenticationFilter,使其继承自AbstractAuthenticationProcessingFilter,并在构造函数中接收一个RequestMatcher。
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.RequestMatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CustomJwtAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
// 构造函数,接收一个RequestMatcher,该匹配器定义了哪些请求需要此过滤器处理
public CustomJwtAuthenticationFilter(RequestMatcher requiresAuthenticationRequestMatcher) {
super(requiresAuthenticationRequestMatcher);
}
// 实际的认证逻辑
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException, IOException, ServletException {
// 从请求中提取JWT令牌的逻辑
String token = extractJwtFromRequest(request);
if (token == null) {
// 如果没有令牌,则抛出认证异常,由AuthenticationEntryPoint处理
throw new BadCredentialsException("No JWT token found in request.");
}
// 假设JwtAuthenticationToken是一个自定义的Authentication实现,
// 包含了JWT令牌信息,等待AuthenticationManager处理
JwtAuthenticationToken authenticationToken = new JwtAuthenticationToken(token);
// 将令牌提交给AuthenticationManager进行认证
// AuthenticationManager会找到对应的AuthenticationProvider来验证令牌
return this.getAuthenticationManager().authenticate(authenticationToken);
}
// 辅助方法:从请求中提取JWT令牌
private String extractJwtFromRequest(HttpServletRequest request) {
// 示例:从Authorization头中提取Bearer Token
String bearerToken = request.getHeader("Authorization");
if (bearerToken != null && bearerToken.startsWith("Bearer ")) {
return bearerToken.substring(7); // 移除"Bearer "前缀
}
return null;
}
// 可以选择性地覆盖successfulAuthentication和unsuccessfulAuthentication方法
// 来处理认证成功或失败后的逻辑,例如设置安全上下文或记录日志。
}注意:JwtAuthenticationToken 和处理JWT令牌的AuthenticationProvider需要您自行实现。这里主要关注过滤器的结构。
为了让JWT过滤器仅作用于/api/**路径,我们可以使用AntPathRequestMatcher。
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
// ...
// 在您的Security配置类中或单独定义
RequestMatcher apiPathsMatcher = new AntPathRequestMatcher("/api/**");最后,在您的Spring Security配置类(通常是继承WebSecurityConfigurerAdapter的类)中,将这个定制的JWT过滤器添加到过滤器链中。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.AuthenticationEntryPoint; // 假设您有自定义的认证入口点
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final UserDetailsService userDetailsService; // 假设您有UserDetailsService
private final AuthenticationEntryPoint jwtAuthenticationEntryPoint; // 假设您有JWT认证入口点
public SecurityConfig(UserDetailsService userDetailsService, AuthenticationEntryPoint jwtAuthenticationEntryPoint) {
this.userDetailsService = userDetailsService;
this.jwtAuthenticationEntryPoint = jwtAuthenticationEntryPoint;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 配置您的AuthenticationManager,例如使用UserDetailsService和密码编码器
auth.userDetailsService(userDetailsService);
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
// 暴露AuthenticationManager为Bean,供CustomJwtAuthenticationFilter使用
return super.authenticationManagerBean();
}
// 定义CustomJwtAuthenticationFilter为Bean
@Bean
public CustomJwtAuthenticationFilter customJwtAuthenticationFilter() throws Exception {
// 创建一个匹配器,指定只有/api/**路径的请求才会被此JWT过滤器处理
AntPathRequestMatcher apiMatcher = new AntPathRequestMatcher("/api/**");
CustomJwtAuthenticationFilter filter = new CustomJwtAuthenticationFilter(apiMatcher);
// 必须设置AuthenticationManager,因为AbstractAuthenticationProcessingFilter需要它来执行认证
filter.setAuthenticationManager(authenticationManagerBean());
// 可以选择性设置认证成功/失败处理器
// filter.setAuthenticationSuccessHandler(...)
// filter.setAuthenticationFailureHandler(...)
return filter;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable() // 禁用CSRF,因为JWT通常是无状态的
.authorizeRequests()
// 确保/api/**路径需要认证。当请求到达这些路径时,如果尚未认证,
// customJwtAuthenticationFilter会尝试处理
.antMatchers("/api/**").authenticated()
// 其他路径可以设置为permitAll()或根据需求配置
.antMatchers("/users", "/login", "/").permitAll()
.anyRequest().authenticated() // 任何其他未匹配的请求也需要认证
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) // JWT是无状态的
.and()
.exceptionHandling()
.authenticationEntryPoint(jwtAuthenticationEntryPoint) // 未认证或认证失败的入口点
.accessDeniedPage("/403") // 访问被拒绝的页面
.and()
// 将自定义的JWT过滤器添加到UsernamePasswordAuthenticationFilter之前
// CustomJwtAuthenticationFilter现在只处理/api/**路径
.addFilterBefore(customJwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}
}在上述配置中,antMatchers("/api/**").authenticated() 确保了所有/api/**路径的请求都需要认证。当请求匹配/api/**时,customJwtAuthenticationFilter会尝试提取并验证JWT令牌。如果令牌有效,请求将继续处理;否则,jwtAuthenticationEntryPoint将介入处理认证失败。对于其他未匹配/api/**的路径,customJwtAuthenticationFilter根本不会被触发,从而实现了精确的过滤。
通过继承AbstractAuthenticationProcessingFilter并利用RequestMatcher,我们可以为Spring Boot Security中的JWT认证过滤器实现精准的URL模式匹配。这种方法不仅提高了应用程序的安全性,因为它只在必要时才执行认证逻辑,同时也优化了性能,避免了不必要的处理开销。掌握这种技术,能够帮助开发者构建更加健壮和高效的Spring Security认证体系。
以上就是Spring Boot Security:为特定URL模式定制JWT认证过滤器的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号