Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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.lang.IllegalArgumentException:密钥字节数组不能为null或空_Java_Spring Boot_Spring Security_Jwt_Jwt Auth - Fatal编程技术网

java.lang.IllegalArgumentException:密钥字节数组不能为null或空

java.lang.IllegalArgumentException:密钥字节数组不能为null或空,java,spring-boot,spring-security,jwt,jwt-auth,Java,Spring Boot,Spring Security,Jwt,Jwt Auth,我使用JWT的弹簧靴 应用程序属性 jwt.secret=xyz Controller.java @PostMapping(path = "/authenticate") public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtRequest authenticationRequest) throws Exception { authenticate(authentication

我使用JWT的弹簧靴

应用程序属性

jwt.secret=xyz
Controller.java

@PostMapping(path = "/authenticate")
    public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtRequest authenticationRequest) throws Exception {

        authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());

        final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername());

        final String token = jwtTokenUtil.generateToken(userDetails);

        return ResponseEntity.ok(new JwtResponse(token));
    }
    @Value("${jwt.secret}")
    private String secret;
    public String generateToken(UserDetails userDetails) {
            Map<String, Object> claims = new HashMap<>();
            return doGenerateToken(claims, userDetails.getUsername());
        }

        private String doGenerateToken(Map<String, Object> claims, String subject) {

            return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis()))
                    .setExpiration(new Date(System.currentTimeMillis() + JWT_TOKEN_VALIDITY*1000)).signWith(SignatureAlgorithm.HS512, secret).compact();
        }
当我给出我的凭证时,我得到了这个错误。应用程序已经验证了我的凭据,但在生成令牌时,我遇到了此错误

更新:-

在调试过程中,我看到了如下的内部代码

 @Override
    public JwtBuilder signWith(SignatureAlgorithm alg, String base64EncodedSecretKey) {
        Assert.hasText(base64EncodedSecretKey, "base64-encoded secret key cannot be null or empty.");
        Assert.isTrue(alg.isHmac(), "Base64-encoded key bytes may only be specified for HMAC signatures.  If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.");
        byte[] bytes = TextCodec.BASE64.decode(base64EncodedSecretKey);
        return signWith(alg, bytes);
    }

在这个
字节中
我得到空值

我认为您的密钥需要进行base64编码 见资料来源:

请尝试以下操作:

    private String doGenerateToken(Map<String, Object> claims, String subject) {

String encodedString = Base64.getEncoder().encodeToString(secret.getBytes())
        return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis()))
                .setExpiration(new Date(System.currentTimeMillis() + JWT_TOKEN_VALIDITY*1000)).signWith(SignatureAlgorithm.HS512, encodedString ).compact();
    }
私有字符串doGenerateToken(地图声明,字符串主题){
String encodedString=Base64.getEncoder().EncodeTString(secret.getBytes())
返回Jwts.builder()
.setExpiration(新日期(System.currentTimeMillis()+JWT_TOKEN_VALIDITY*1000)).signWith(SignatureAlgorithm.HS512,encodedString).compact();
}

我也有类似的问题,原因是代码无法获取机密值,即使它是在应用程序属性中声明的,我正在读取它。在完成一组密钥后,它就能够生成令牌。

密钥太短。我也犯了同样的错误,只是从 jwt.secret=omg 到 jwt.secret=HRlELXqpSB
已解决问题。

您的密钥太短。请使用一个长的,如超过10个字符。然后就可以了。

例外是因为变量“secret”是null@jps错误消息很清楚,但我在application.properties文件中给出了
jwt.secret=xyz
,您只在代码中使用
secret
,不清楚它的定义位置或方式:
signWith(SignatureAlgorithm.HS512,secret)
。这是一个调试问题,设置一个断点并观察您的变量。@jps我没有在那里给出变量的刻度,但在调试过程中,我得到了
secret
值作为
xyz
,我编辑了我的问题
    private String doGenerateToken(Map<String, Object> claims, String subject) {

String encodedString = Base64.getEncoder().encodeToString(secret.getBytes())
        return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis()))
                .setExpiration(new Date(System.currentTimeMillis() + JWT_TOKEN_VALIDITY*1000)).signWith(SignatureAlgorithm.HS512, encodedString ).compact();
    }