Spring Boot + Security + JWT 无法生成token

马塞尔·卡夫拉蒂

我使用 JWT 配置了 Spring Boot 和安全性,一段时间内一切都产生了奇迹。

这是我的 webSecurityConfig

httpSecurity
            .csrf().disable()
            .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .authorizeRequests()
            .antMatchers(HttpMethod.POST, "/user/cadastrar/**").permitAll()
            .antMatchers(HttpMethod.POST, "/auth/**").permitAll()
            .anyRequest().authenticated();
    httpSecurity
            .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
    httpSecurity.headers().cacheControl();

第一条路线“/users/cadadstrar”工作正常。

问题是我的第二条路线“/auth”在主体上使用用户名和密码调用 /auth 它将落在我的 JwtAuthenticationTokenFilter 类中的此函数上

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    String authToken = request.getHeader(this.tokenHeader);
    String username = jwtTokenUtil.getUsernameFromToken(authToken);
    logger.info("checking authentication for user " + username);
    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
        UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
        if (jwtTokenUtil.validateToken(authToken, userDetails)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            logger.info("authenticated user " + username + ", setting security context");
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }
    chain.doFilter(request, response);
}

然后它将转到我的 AuthenticationController 类并运行此函数

@RequestMapping(value = "${jwt.route.authentication.path}", method = RequestMethod.POST)
public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest, Device device) throws AuthenticationException {

    // Perform the security
    final Authentication authentication = authenticationManager.authenticate(
            new UsernamePasswordAuthenticationToken(
                    authenticationRequest.getUsername(),
                    authenticationRequest.getPassword()
            )
    );
    SecurityContextHolder.getContext().setAuthentication(authentication);

    // Reload password post-security so we can generate token
    final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername());
    final String token = jwtTokenUtil.generateToken(userDetails, device);

    // Return the token
    return ResponseEntity.ok(new JwtAuthenticationResponse(token));
}

问题似乎是代码的这个特定部分:

// Perform the security
    final Authentication authentication = authenticationManager.authenticate(
            new UsernamePasswordAuthenticationToken(
                    authenticationRequest.getUsername(),
                    authenticationRequest.getPassword()
            )
    );

当它尝试返回对象“UserNamePasswordAuthenticationToken”时,它只是在“chain.doFilter”调用之后将断点发送到函数“doFilterInternal”的末尾,专门发送到括号。

马塞尔·卡夫拉蒂

问题已经解决了!显然,连续 16 小时编码会影响您的思维方式!

上面的代码没有任何问题,出于某种原因,我将新创建的用户设置为默认禁用!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

生成自定义 jwt-token 并验证用户,Spring Security

来自分类Dev

从 Spring Boot 为角度应用程序生成 jwt 令牌

来自分类Dev

使用AspectJ的Spring Boot + Spring Security无法正常工作

来自分类Dev

Spring Boot或Spring Security内存可能泄漏

来自分类Dev

Spring Boot ServeletInitializer和Spring Security

来自分类Dev

Spring Boot中Spring Security的XML配置

来自分类Dev

使用Spring Boot配置Spring Security

来自分类Dev

Spring Boot或Spring Security内存可能泄漏

来自分类Dev

带有Spring Boot的Spring Security

来自分类Dev

Spring Boot JWT:GET API的403错误

来自分类Dev

生成JWT令牌时,authenticate方法在Java Spring Boot中给出内部服务器错误

来自分类Dev

使用Spring Boot在Spring Security中无法阻止同一用户的多个并发登录

来自分类Dev

使用Spring Security保护rest-api后,Spring Boot JS App无法正常工作

来自分类Dev

Spring Security无法登录

来自分类Dev

how to implement a authentication with spring boot security?

来自分类Dev

Spring Boot Security + Thymeleaf:缺少IProcessorDialect类

来自分类Dev

Spring Boot Security 403重定向

来自分类Dev

Spring Security使用有效的JWT返回403

来自分类Dev

Spring Security JWT - 400 错误请求:“无效授权”“错误凭据”

来自分类Dev

无法在Spring Boot Security中登录我的自定义登录页面

来自分类Dev

Spring Boot应用程序无法通过JspTagLib和Freemarker找到security.tld

来自分类Dev

Spring Boot Security-java.lang.IllegalArgumentException:无法传递空的GrantedAuthority集合

来自分类Dev

Spring Security无状态JWT身份验证-如何获取其他jwt字段?

来自分类Dev

Spring Boot / Spring Security正在忽略来自JavaScript的/ login调用

来自分类Dev

Spring Security / Spring Boot-如何为用户设置ROLES

来自分类Dev

spring-boot,spring-security和dropwizard指标

来自分类Dev

如何在Spring Boot中覆盖Spring Security默认配置

来自分类Dev

从Spring Boot Oauth2迁移到Spring Security 5

来自分类Dev

没有Spring Security的Spring-Boot登录

Related 相关文章

  1. 1

    生成自定义 jwt-token 并验证用户,Spring Security

  2. 2

    从 Spring Boot 为角度应用程序生成 jwt 令牌

  3. 3

    使用AspectJ的Spring Boot + Spring Security无法正常工作

  4. 4

    Spring Boot或Spring Security内存可能泄漏

  5. 5

    Spring Boot ServeletInitializer和Spring Security

  6. 6

    Spring Boot中Spring Security的XML配置

  7. 7

    使用Spring Boot配置Spring Security

  8. 8

    Spring Boot或Spring Security内存可能泄漏

  9. 9

    带有Spring Boot的Spring Security

  10. 10

    Spring Boot JWT:GET API的403错误

  11. 11

    生成JWT令牌时,authenticate方法在Java Spring Boot中给出内部服务器错误

  12. 12

    使用Spring Boot在Spring Security中无法阻止同一用户的多个并发登录

  13. 13

    使用Spring Security保护rest-api后,Spring Boot JS App无法正常工作

  14. 14

    Spring Security无法登录

  15. 15

    how to implement a authentication with spring boot security?

  16. 16

    Spring Boot Security + Thymeleaf:缺少IProcessorDialect类

  17. 17

    Spring Boot Security 403重定向

  18. 18

    Spring Security使用有效的JWT返回403

  19. 19

    Spring Security JWT - 400 错误请求:“无效授权”“错误凭据”

  20. 20

    无法在Spring Boot Security中登录我的自定义登录页面

  21. 21

    Spring Boot应用程序无法通过JspTagLib和Freemarker找到security.tld

  22. 22

    Spring Boot Security-java.lang.IllegalArgumentException:无法传递空的GrantedAuthority集合

  23. 23

    Spring Security无状态JWT身份验证-如何获取其他jwt字段?

  24. 24

    Spring Boot / Spring Security正在忽略来自JavaScript的/ login调用

  25. 25

    Spring Security / Spring Boot-如何为用户设置ROLES

  26. 26

    spring-boot,spring-security和dropwizard指标

  27. 27

    如何在Spring Boot中覆盖Spring Security默认配置

  28. 28

    从Spring Boot Oauth2迁移到Spring Security 5

  29. 29

    没有Spring Security的Spring-Boot登录

热门标签

归档