Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signup posts generates 401 #21

Open
thomaschristensen opened this issue Jun 5, 2023 · 9 comments
Open

Signup posts generates 401 #21

thomaschristensen opened this issue Jun 5, 2023 · 9 comments

Comments

@thomaschristensen
Copy link

When trying to create users following the example in the docs I get 401 on posting to /api/auth/signup

@thomaschristensen
Copy link
Author

Appears to be when username is shorter than 3 letters and this error does not end up in the returned message

@tienbku
Copy link
Contributor

tienbku commented Jun 6, 2023

Hi, you can validate the request body. Kindly read following tutorial:
https://www.bezkoder.com/spring-boot-validate-request-body/

@bsuriyakuts
Copy link

Getting 401 issue while calling singup api. any solutions can be suggested?

@phiau
Copy link

phiau commented Aug 3, 2023

Getting 401 issue while calling singup api. any solutions can be suggested?

curl --location 'localhost:8080/api/auth/signup' \ --header 'Content-Type: application/json' \ --header 'Cookie: JSESSIONID=89CC086CF0D4CF7D8A3E542AFA8CDF54' \ --data-raw '{ "username":"phiauphiau", "email":"[email protected]", "password":"phiauphiau", "role":[] }'

Above curl request work fine. Below getting the 401 as you.

curl --location --request POST 'localhost:8080/api/auth/signup?username=phiauphiau&email=phiau%40phiau.com&password=phiauphiau&role=ROLE_USER' \ --header 'Cookie: JSESSIONID=89CC086CF0D4CF7D8A3E542AFA8CDF54'

@lthchinchin
Copy link

Addon ValidationExceptionHandler Class. See in the article: https://www.bezkoder.com/spring-boot-validate-request-body/

@VitusJoy
Copy link

VitusJoy commented May 7, 2024

@thomaschristensen
got the solution?

@thomaschristensen
Copy link
Author

No, but I haven't tried with newer versions

@VitusJoy
Copy link

VitusJoy commented May 7, 2024

No, but I haven't tried with newer versions

on java
java -version
java version "17.0.1" 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)

Screenshot 2024-05-07 at 4 30 24 PM

nothing on console

***property
spring.datasource.url=jdbc:mysql://localhost:3306/DB_1?useSSL=false
spring.datasource.username=root
spring.datasource.password=PASSWORD
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
bezkoder.app.jwtSecret= ======================BezKoder=Spring===========================
bezkoder.app.jwtExpirationMs=86400000

@yousuf-git
Copy link

yousuf-git commented Dec 30, 2024

Add this ExceptionHandler class for exception handling of validation issues

`import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, String>> handleValidationExceptions(MethodArgumentNotValidException ex) {
    Map<String, String> errors = new HashMap<>();
    for (FieldError error : ex.getBindingResult().getFieldErrors()) {
        errors.put(error.getField(), error.getDefaultMessage());
    }
    return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants