2017-05-05 89 views
-2

我配置了JWT的spring启动和安全功能,一切都有效。Spring Boot + Security +智威汤逊无法生成令牌

这是我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(); 

第一条路线 “/用户/ cadastrar” 工作正常。

问题是我的第二条路线“/ 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”的末尾,特别指向括号。

回答

0

问题解决了!显然,连续16小时编码会影响您的想法!

上面的代码没有什么错,由于某种原因,我设置我的新创建的用户默认情况下是禁用的!