为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的 spring 安全配置不起作用,我的预期如何?

为什么我的 spring 安全配置不起作用,我的预期如何?

宝慕林4294392 2021-07-01 10:15:07
我有一个问题……我有这样的 SecurityConfig.class(这是一个扩展 WebSecurityConfigurerAdapter 的类):/** * This method define which resources are public and which are secured */@Overrideprotected void configure(HttpSecurity http) throws Exception {    http.cors().and().csrf().disable().authorizeRequests()            .antMatchers(HttpMethod.POST, SecurityConstants.SIGN_UP_URL).permitAll().anyRequest()            .authenticated()            .and().addFilter(new JWTAuthenticationFilter(authenticationManager()))            .addFilter(new JWTAuthorizationFilter(authenticationManager()))         .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);}但我的问题是,当我尝试访问 SIGN_UP_URL 时,应用程序正在尝试使用此过滤器对用户进行身份验证:@Overrideprotected void doFilterInternal(HttpServletRequest req,                                HttpServletResponse res,                                FilterChain chain) throws IOException, ServletException {    String header = req.getHeader(SecurityConstants.HEADER_STRING);    if (header == null || !header.startsWith(SecurityConstants.TOKEN_PREFIX)) {        chain.doFilter(req, res);        res.sendError(HttpServletResponse.SC_UNAUTHORIZED,"Access Denied");        return;    }    UsernamePasswordAuthenticationToken authentication = getAuthentication(req);    SecurityContextHolder.getContext().setAuthentication(authentication);    chain.doFilter(req, res);}检查到那时为空的标头(因为该 URL 理论上不需要身份验证或任何令牌)这应该不会发生,有人可以帮我吗?当我在没有身份验证的情况下为所有用户打开安全性时,该方法成功执行
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 236 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信