spring-mvc - Spring Security with JWT:不调用验证令牌的 JWT 过滤器
问题描述
我正在使用带有 JWT 的 Spring Security 来验证 rest api。登录时,会生成 JWT 令牌并将其共享给移动客户端。但是,后续请求中的令牌未经过验证。安全配置有什么问题吗?
Spring 安全版本 - 5.1.6.RELEASE
// 安全配置
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
@Autowired
private JwtTokenAuthenticationFilter jwtTokenAuthenticationFilter;
@Autowired
private CustomUserDetailsService customUserDetailsService;
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.
httpBasic().disable().
csrf().disable().
exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).
and().
addFilterBefore(jwtTokenAuthenticationFilter, UsernamePasswordAuthenticationFilter.class).
sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).
and().
authorizeRequests().antMatchers("/user/login").permitAll().
antMatchers("/user/test").authenticated().
anyRequest().authenticated();
}
}
// JWT 令牌认证过滤器 - 这永远不会被调用
@Component
public class JwtTokenAuthenticationFilter extends GenericFilterBean {
@Autowired
private JwtTokenProvider jwtTokenProvider;
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
String token = jwtTokenProvider.resolveToken((HttpServletRequest) req);
if (null != token && jwtTokenProvider.validateToken(token)) {
Authentication auth = jwtTokenProvider.getAuthentication(token);
if (null != auth) {
SecurityContextHolder.getContext().setAuthentication(auth);
}
}
filterChain.doFilter(req, res);
}
}
我期待登录后的所有请求都将根据 JWT 令牌进行身份验证。我尝试将要验证的服务名称如下:
antMatchers("/user/test").authenticated().
此外,还添加了任何经过身份验证的请求,但它们都不起作用。
anyRequest().authenticated();
解决方案
尝试更换
addFilterBefore(jwtTokenAuthenticationFilter, UsernamePasswordAuthenticationFilter.class).
经过
addFilterBefore(jwtTokenAuthenticationFilter), BasicAuthenticationFilter.class);
如果没有运行,请更改 jwtTokenAuthenticationFilter 类以避免成为 Spring bean 并像这样使用:
addFilterBefore(new JwtTokenAuthenticationFilter(jwtTokenProvider)), BasicAuthenticationFilter.class);
并在安全类上添加以下代码:
@Autowired
private JwtTokenProvider jwtTokenProvider;
推荐阅读
- angular - 从 Angular v7.2 更新到 v8 时出现“不兼容的对等依赖项”错误
- docker - 使用 Dockerfile 时找不到 Go 二进制文件
- python - 对变量所做的更改未反映在控制台中
- prolog - 有没有办法在使用较少变量的子句中达到相同的结果?
- python - 如何在python 3中输入数组
- bitmap - 如何使自动图块对 Godot 中的其他图块做出反应?
- android - Android - 如何在 res xml 中存储类数组
- html - 当使用 if 语句过滤前 3 个图像时,如何在从左侧开始的数组中显示(即)第 3 个索引(第 4 个)图像?
- r - 长 selectInput 列表中的 R 闪亮收藏夹
- javascript - 有没有办法将 Python 变量传递给 Javascript 而无需将其写入文件?