Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 调用Spring authenticationManager.authenticate的堆栈溢出_Java_Spring_Spring Boot_Spring Security_Jwt - Fatal编程技术网

Java 调用Spring authenticationManager.authenticate的堆栈溢出

Java 调用Spring authenticationManager.authenticate的堆栈溢出,java,spring,spring-boot,spring-security,jwt,Java,Spring,Spring Boot,Spring Security,Jwt,我从一个SpringBootStarter项目版本2.3.5开始,但是当我调用 authenticationManager.authenticate 我得到一个堆栈溢出错误 java.lang.StackOverflowError: null at ch.qos.logback.classic.Logger.callTurboFilters(Logger.java:751) ~[logback-classic-1.2.3.jar:na] at ch.qos.logback.cla

我从一个SpringBootStarter项目版本2.3.5开始,但是当我调用 authenticationManager.authenticate 我得到一个堆栈溢出错误

java.lang.StackOverflowError: null
    at ch.qos.logback.classic.Logger.callTurboFilters(Logger.java:751) ~[logback-classic-1.2.3.jar:na]
    at ch.qos.logback.classic.Logger.isDebugEnabled(Logger.java:469) ~[logback-classic-1.2.3.jar:na]
    at ch.qos.logback.classic.Logger.isDebugEnabled(Logger.java:465) ~[logback-classic-1.2.3.jar:na]
    at org.apache.commons.logging.LogAdapter$Slf4jLog.isDebugEnabled(LogAdapter.java:310) ~[spring-jcl-5.2.10.RELEASE.jar:5.2.10.RELEASE]
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:186) ~[spring-security-core-5.3.5.RELEASE.jar:5.3.5.RELEASE]
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:524) ~[spring-security-config-5.3.5.RELEASE.jar:5.3.5.RELEASE]
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:219) ~[spring-security-core-5.3.5.RELEASE.jar:5.3.5.RELEASE]
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:524) ~[spring-security-config-5.3.5.RELEASE.jar:5.3.5.RELEASE]
最初,我没有使用user变量,而是在authenticationManager.authenticate中创建了新的UsernamePasswordAuthenticationToken。无论哪种方式,我都会得到堆栈溢出错误。我还尝试了@PostMapping而不是@requestmappingn和Method。所有这些都会导致相同的失败。 我得到的打印行显示我“在/验证”以下内容

2020-11-09 16:20:32.141 DEBUG 10456 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to io.javabrains.springsecurityjwt.HelloResource#createAuthenticationToken(AuthenticationRequest)
2020-11-09 16:20:32.188 DEBUG 10456 --- [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Read "application/json;charset=UTF-8" to [io.javabrains.springsecurityjwt.models.AuthenticationRequest@758a32c5]
in /Authenticate
2020-11-09 16:20:32.197 DEBUG 10456 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Failed to complete request: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError
2020-11-09 16:20:32.201 ERROR 10456 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause
这是这个类的完整代码 谢谢你提供我应该尝试的任何信息或东西

package io.javabrains.springsecurityjwt;

import io.javabrains.springsecurityjwt.models.AuthenticationRequest;
import io.javabrains.springsecurityjwt.models.AuthenticationResponse;
import io.javabrains.springsecurityjwt.services.MyUserDetailsService;
import io.javabrains.springsecurityjwt.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloResource {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private MyUserDetailsService userDetailsService;

    @Autowired
    private JwtUtil jwtTokenUtil;

    @RequestMapping( "/hello")
    public String hello() {
        return "Hello World";
    }

    @RequestMapping(value = "/authenticate", method = RequestMethod.POST)
    public ResponseEntity<?> createAuthenticationToken(@RequestBody AuthenticationRequest authenticationRequest) throws Exception {
        UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(), authenticationRequest.getPassword());
        System.out.println("in /Authenticate");
        try {
            authenticationManager.authenticate(
                    user
//                  new UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(), authenticationRequest.getPassword())
            );
            System.out.println("in Try in /Authenticate");
        } catch (BadCredentialsException e) {
            throw new Exception("Incorrect Username or Password", e);
        }
        System.out.println("outside the try catch");

        final UserDetails userDetails = userDetailsService
                .loadUserByUsername(authenticationRequest.getUsername());
        final String jwt = jwtTokenUtil.generateToken(userDetails);

        return ResponseEntity.ok(new AuthenticationResponse(jwt));

    }
}
包io.javabrains.springsecurityjwt;
导入io.javabrains.springsecurityjwt.models.AuthenticationRequest;
导入io.javabrains.springsecurityjwt.models.AuthenticationResponse;
导入io.javabrains.springsecurityjwt.services.MyUserDetailsService;
导入io.javabrains.springsecurityjwt.util.JwtUtil;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.http.ResponseEntity;
导入org.springframework.security.authentication.AuthenticationManager;
导入org.springframework.security.authentication.BadCredentialsException;
导入org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
导入org.springframework.security.core.userdetails.userdetails;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RestController;
@RestController
公共类HelloResource{
@自动连线
私人AuthenticationManager AuthenticationManager;
@自动连线
私有MyUserDetails服务UserDetails服务;
@自动连线
私有JwtUtil jwtTokenUtil;
@请求映射(“/hello”)
公共字符串hello(){
返回“你好世界”;
}
@RequestMapping(value=“/authenticate”,method=RequestMethod.POST)
public ResponseEntity createAuthenticationToken(@RequestBody AuthenticationRequest AuthenticationRequest)引发异常{
UsernamePasswordAuthenticationToken user=新的UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(),authenticationRequest.getPassword());
System.out.println(“输入/验证”);
试一试{
authenticationManager.authenticate(
用户
//新用户名PasswordAuthenticationToken(authenticationRequest.getUsername(),authenticationRequest.getPassword())
);
System.out.println(“in-Try-in/Authenticate”);
}捕获(BadCredentialsException e){
抛出新异常(“不正确的用户名或密码”,e);
}
System.out.println(“尝试捕捉之外”);
最终用户详细信息用户详细信息=用户详细信息服务
.loadUserByUsername(authenticationRequest.getUsername());
最终字符串jwt=jwtTokenUtil.generateToken(userDetails);
返回ResponseEntity.ok(新身份验证响应(jwt));
}
}

您给我们看错了班级。您需要向我们显示您的安全配置,例如
websecurityConfigureAdapter
的子类,以及
应用程序.properties
中与安全相关的值。感谢您的回复!虽然我有点困惑。Im未使用WebSecurity配置适配器,application.properties仅具有debug=true;在里面,你给我们看错了课。您需要向我们显示您的安全配置,例如
websecurityConfigureAdapter
的子类,以及
应用程序.properties
中与安全相关的值。感谢您的回复!虽然我有点困惑。Im未使用WebSecurity配置适配器,application.properties仅具有debug=true;在里面。