Fix the problem that the username does not exist when the username does not exist (#4070)

#### What type of PR is this?

/kind bug
/area core

#### What this PR does / why we need it:

Catch UserNotFoundException instead of ExtensionNotFoundException to map correctly to BadCredentialsException.

#### Which issue(s) this PR fixes:

Fixes https://github.com/halo-dev/halo/issues/4057

#### Special notes for your reviewer:

1. Start Halo
2. Login with an username which does not exist
3. Check the response

#### Does this PR introduce a user-facing change?

```release-note
修复登录时出现用户名不存在的问题。
```
pull/4061/head
John Niang 2023-06-26 20:21:57 +08:00 committed by GitHub
parent ecc617c709
commit aaa3548c97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 8 deletions

View File

@ -15,7 +15,7 @@ import run.halo.app.core.extension.RoleBinding.Subject;
import run.halo.app.core.extension.service.RoleService;
import run.halo.app.core.extension.service.UserService;
import run.halo.app.extension.GroupKind;
import run.halo.app.extension.exception.ExtensionNotFoundException;
import run.halo.app.infra.exception.UserNotFoundException;
public class DefaultUserDetailService
implements ReactiveUserDetailsService, ReactiveUserDetailsPasswordService {
@ -38,6 +38,8 @@ public class DefaultUserDetailService
@Override
public Mono<UserDetails> findByUsername(String username) {
return userService.getUser(username)
.onErrorMap(UserNotFoundException.class,
e -> new BadCredentialsException("Invalid Credentials"))
.flatMap(user -> {
var subject = new Subject(KIND, username, GROUP);
return roleService.listRoleRefs(subject)
@ -49,9 +51,7 @@ public class DefaultUserDetailService
.password(user.getSpec().getPassword())
.roles(roleNames.toArray(new String[0]))
.build());
})
.onErrorMap(ExtensionNotFoundException.class,
e -> new BadCredentialsException("Invalid Credentials"));
});
}
private boolean isRoleRef(RoleRef roleRef) {

View File

@ -5,7 +5,6 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static run.halo.app.extension.GroupVersionKind.fromExtension;
import java.util.List;
import java.util.stream.Collectors;
@ -27,7 +26,7 @@ import run.halo.app.core.extension.RoleBinding.Subject;
import run.halo.app.core.extension.service.RoleService;
import run.halo.app.core.extension.service.UserService;
import run.halo.app.extension.Metadata;
import run.halo.app.extension.exception.ExtensionNotFoundException;
import run.halo.app.infra.exception.UserNotFoundException;
@ExtendWith(MockitoExtension.class)
class DefaultUserDetailServiceTest {
@ -164,8 +163,7 @@ class DefaultUserDetailServiceTest {
@Test
void shouldNotFindUserDetailsByNonExistingUsername() {
when(userService.getUser("non-existing-user")).thenReturn(
Mono.error(() -> new ExtensionNotFoundException(
fromExtension(run.halo.app.core.extension.User.class), "non-existing-user")));
Mono.error(() -> new UserNotFoundException("non-existing-user")));
var userDetailsMono = userDetailService.findByUsername("non-existing-user");