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

我有一个问题……我有这样的 SecurityConfig.class(这是一个扩展 WebSecurityConfigurerAdapter 的类):


/**

 * This method define which resources are public and which are secured

 */

@Override

protected 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 时,应用程序正在尝试使用此过滤器对用户进行身份验证:


@Override

protected 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 理论上不需要身份验证或任何令牌)这应该不会发生,有人可以帮我吗?当我在没有身份验证的情况下为所有用户打开安全性时,该方法成功执行


宝慕林4294392
浏览 237回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java