定义用户认证模块

pull/1/head
smallbailangui 7 months ago
parent 1b46b5e6e5
commit a2de7b29a2

@ -0,0 +1,46 @@
package com.mathgenerator.auth;
import com.mathgenerator.model.Level;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import java.util.stream.Collectors;
/**
*
*/
public class Authenticator {
private static final Map<String, User> USER_DATABASE = initializeUsers();
/**
*
*/
private static Map<String, User> initializeUsers() {
return Stream.of(
new User("张三1", "123", Level.PRIMARY),
new User("张三2", "123", Level.PRIMARY),
new User("张三3", "123", Level.PRIMARY),
new User("李四1", "123", Level.JUNIOR_HIGH),
new User("李四2", "123", Level.JUNIOR_HIGH),
new User("李四3", "123", Level.JUNIOR_HIGH),
new User("王五1", "123", Level.SENIOR_HIGH),
new User("王五2", "123", Level.SENIOR_HIGH),
new User("王五3", "123", Level.SENIOR_HIGH)
).collect(Collectors.toMap(User::username, user -> user));
}
/**
*
* @param username
* @param password
* @return UserOptionalOptional
*/
public Optional<User> login(String username, String password) {
User user = USER_DATABASE.get(username);
if (user != null && user.password().equals(password)) {
return Optional.of(user);
}
return Optional.empty();
}
}

@ -0,0 +1,12 @@
package com.mathgenerator.auth;
import com.mathgenerator.model.Level;
/**
* (Record)
* @param username
* @param password
* @param level
*/
public record User(String username, String password, Level level) {
}
Loading…
Cancel
Save