diff --git a/generators/server/templates/src/main/java/_package_/web/rest/errors/ExceptionTranslator.java.ejs b/generators/server/templates/src/main/java/_package_/web/rest/errors/ExceptionTranslator.java.ejs index a18b1b371be3..3857b52cb659 100644 --- a/generators/server/templates/src/main/java/_package_/web/rest/errors/ExceptionTranslator.java.ejs +++ b/generators/server/templates/src/main/java/_package_/web/rest/errors/ExceptionTranslator.java.ejs @@ -149,13 +149,10 @@ _%> private ProblemDetailWithCause getProblemDetailWithCause(Throwable ex) { <%_ if (!skipUserManagement) { _%> - if(ex instanceof <%= packageName %>.service.EmailAlreadyUsedException || - ex instanceof <%= packageName %>.service.UsernameAlreadyUsedException) { - // return 201 - CREATED on purpose to not reveal information to potential attackers - // see https://github.com/jhipster/generator-jhipster/issues/21731 - return ProblemDetailWithCauseBuilder.instance() - .withStatus(201).build(); - } + if(ex instanceof <%= packageName %>.service.UsernameAlreadyUsedException ) + return (ProblemDetailWithCause) new LoginAlreadyUsedException().getBody(); + if(ex instanceof <%= packageName %>.service.EmailAlreadyUsedException ) + return (ProblemDetailWithCause) new EmailAlreadyUsedException().getBody(); if(ex instanceof <%= packageName %>.service.InvalidPasswordException ) return (ProblemDetailWithCause) new InvalidPasswordException().getBody(); diff --git a/generators/server/templates/src/test/java/_package_/web/rest/AccountResourceIT.java.ejs b/generators/server/templates/src/test/java/_package_/web/rest/AccountResourceIT.java.ejs index 2ca7ef709d49..3df3bb753ac4 100644 --- a/generators/server/templates/src/test/java/_package_/web/rest/AccountResourceIT.java.ejs +++ b/generators/server/templates/src/test/java/_package_/web/rest/AccountResourceIT.java.ejs @@ -529,14 +529,14 @@ class AccountResourceIT { .contentType(MediaType.APPLICATION_JSON) .bodyValue(TestUtil.convertObjectToJsonBytes(secondUser)) .exchange() - .expectStatus().isCreated(); + .expectStatus().isBadRequest(); <%_ } else { _%> restAccountMockMvc.perform( post("/api/register") .contentType(MediaType.APPLICATION_JSON) .content(TestUtil.convertObjectToJsonBytes(secondUser))<% if (authenticationUsesCsrf) { %> .with(csrf())<% } %>) - .andExpect(status().isCreated()); + .andExpect(status().is4xxClientError()); <%_ } _%> } @@ -632,14 +632,14 @@ class AccountResourceIT { .contentType(MediaType.APPLICATION_JSON) .bodyValue(TestUtil.convertObjectToJsonBytes(userWithUpperCaseEmail)) .exchange() - .expectStatus().isCreated(); + .expectStatus().is4xxClientError(); <%_ } else { _%> restAccountMockMvc.perform( post("/api/register") .contentType(MediaType.APPLICATION_JSON) .content(TestUtil.convertObjectToJsonBytes(userWithUpperCaseEmail))<% if (authenticationUsesCsrf) { %> .with(csrf())<% } %>) - .andExpect(status().isCreated()); + .andExpect(status().is4xxClientError()); <%_ } _%> Optional<<%= user.persistClass %>> testUser4 = userRepository.findOneByLogin("test-register-duplicate-email-3")<% if (reactive) { %>.blockOptional()<% } %>; @@ -655,14 +655,14 @@ class AccountResourceIT { .contentType(MediaType.APPLICATION_JSON) .bodyValue(TestUtil.convertObjectToJsonBytes(secondUser)) .exchange() - .expectStatus().isCreated(); + .expectStatus().is4xxClientError(); <%_ } else { _%> restAccountMockMvc.perform( post("/api/register") .contentType(MediaType.APPLICATION_JSON) .content(TestUtil.convertObjectToJsonBytes(secondUser))<% if (authenticationUsesCsrf) { %> .with(csrf())<% } %>) - .andExpect(status().isCreated()); + .andExpect(status().is4xxClientError()); <%_ } _%> }