Add TestAuthentication convenience method

Issue gh-14597
This commit is contained in:
Josh Cummings
2024-03-14 10:05:19 -06:00
parent d169d5a835
commit ce54a6db18
2 changed files with 28 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,12 @@
package org.springframework.security.authentication;
import java.util.function.Consumer;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.PasswordEncodedUser;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
/**
@@ -42,6 +45,12 @@ public class TestAuthentication extends PasswordEncodedUser {
return authenticated(user());
}
public static Authentication authenticatedUser(Consumer<User.UserBuilder> consumer) {
User.UserBuilder builder = withUsername("user");
consumer.accept(builder);
return authenticated(builder.build());
}
public static Authentication authenticated(UserDetails user) {
return UsernamePasswordAuthenticationToken.authenticated(user, null, user.getAuthorities());
}