mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
[FEATURE][CRITICAL] Добавление поддержки Hibernate
This commit is contained in:
parent
03ef5265dd
commit
439b3f41c1
10 changed files with 249 additions and 0 deletions
|
@ -53,6 +53,7 @@ bundle project(':Radon')
|
|||
bundle 'commons-codec:commons-codec:1.12'
|
||||
bundle 'org.javassist:javassist:3.25.0-GA'
|
||||
bundle 'io.netty:netty-all:4.1.36.Final'
|
||||
bundle 'org.hibernate:hibernate-core:5.4.3.Final'
|
||||
|
||||
bundle 'org.slf4j:slf4j-simple:1.7.25'
|
||||
bundle 'org.slf4j:slf4j-api:1.7.25'
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package pro.gravit.launchserver.auth.provider;
|
||||
|
||||
import pro.gravit.launchserver.auth.AuthException;
|
||||
import pro.gravit.launchserver.hibernate.HibernateManager;
|
||||
import pro.gravit.utils.ProviderMap;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -21,6 +22,7 @@ public static void registerProviders() {
|
|||
providers.register("mysql", MySQLAuthProvider.class);
|
||||
providers.register("request", RequestAuthProvider.class);
|
||||
providers.register("json", JsonAuthProvider.class);
|
||||
providers.register("hibernate", HibernateAuthProvider.class);
|
||||
registredProv = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package pro.gravit.launchserver.auth.provider;
|
||||
|
||||
import pro.gravit.launchserver.auth.AuthException;
|
||||
import pro.gravit.launchserver.hibernate.User;
|
||||
import pro.gravit.launchserver.hibernate.UserService;
|
||||
import pro.gravit.utils.helper.SecurityHelper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class HibernateAuthProvider extends AuthProvider {
|
||||
@Override
|
||||
public AuthProviderResult auth(String login, String password, String ip) throws Exception {
|
||||
UserService service = new UserService();
|
||||
User user = service.findUserByUsername(login);
|
||||
if(user == null || !user.verifyPassword(password))
|
||||
{
|
||||
if(user ==null) throw new AuthException("Username incorrect");
|
||||
else throw new AuthException("Username or password incorrect");
|
||||
}
|
||||
return new AuthProviderResult(login, SecurityHelper.randomStringToken());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ public static void registerComponents() {
|
|||
if (!registredComp) {
|
||||
providers.register("authLimiter", AuthLimiterComponent.class);
|
||||
providers.register("commandRemover", CommandRemoverComponent.class);
|
||||
providers.register("hibernate", HibernateConfiguratorComponent.class);
|
||||
registredComp = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package pro.gravit.launchserver.components;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import pro.gravit.launchserver.LaunchServer;
|
||||
import pro.gravit.launchserver.hibernate.HibernateManager;
|
||||
import pro.gravit.launchserver.hibernate.User;
|
||||
import pro.gravit.launchserver.hibernate.UserService;
|
||||
import pro.gravit.utils.helper.LogHelper;
|
||||
|
||||
import javax.persistence.Persistence;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
||||
public class HibernateConfiguratorComponent extends Component {
|
||||
public String driver;
|
||||
public String url;
|
||||
public String username;
|
||||
public String password;
|
||||
public String pool_size;
|
||||
public String hibernateConfig;
|
||||
@Override
|
||||
public void preInit(LaunchServer launchServer) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(LaunchServer launchServer) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postInit(LaunchServer launchServer) {
|
||||
Configuration cfg = new Configuration()
|
||||
.addAnnotatedClass(User.class)
|
||||
.setProperty("hibernate.connection.driver_class", driver)
|
||||
.setProperty("hibernate.connection.url", url)
|
||||
.setProperty("hibernate.connection.username", username)
|
||||
.setProperty("hibernate.connection.password", password)
|
||||
.setProperty("hibernate.connection.pool_size", pool_size);
|
||||
if(hibernateConfig != null)
|
||||
cfg.configure(Paths.get(hibernateConfig).toFile());
|
||||
HibernateManager.sessionFactory = cfg.buildSessionFactory();
|
||||
//UserService service = new UserService();
|
||||
//List<User> users = service.findAllUsers();
|
||||
//User newUser = new User();
|
||||
//newUser.username = "VeryTestUser";
|
||||
//newUser.setPassword("12345");
|
||||
//service.saveUser(newUser);
|
||||
//for(User u : users)
|
||||
//{
|
||||
// LogHelper.info("Found User %s", u.username);
|
||||
//}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package pro.gravit.launchserver.hibernate;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
public class HibernateManager {
|
||||
public static SessionFactory sessionFactory;
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package pro.gravit.launchserver.hibernate;
|
||||
|
||||
import pro.gravit.utils.helper.LogHelper;
|
||||
import pro.gravit.utils.helper.SecurityHelper;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
@Column(unique = true)
|
||||
public String username;
|
||||
@Column(name = "password")
|
||||
private byte[] password;
|
||||
private String password_salt;
|
||||
public void setPassword(String password)
|
||||
{
|
||||
password_salt = SecurityHelper.randomStringAESKey();
|
||||
MessageDigest digest;
|
||||
try {
|
||||
digest = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
LogHelper.error(e);
|
||||
return;
|
||||
}
|
||||
this.password = digest.digest(password.concat(password_salt).getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
public boolean verifyPassword(String password)
|
||||
{
|
||||
MessageDigest digest;
|
||||
try {
|
||||
digest = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
LogHelper.error(e);
|
||||
return false;
|
||||
}
|
||||
byte[] enpassword = digest.digest(password.concat(password_salt).getBytes(StandardCharsets.UTF_8));
|
||||
LogHelper.info(Arrays.toString(enpassword));
|
||||
LogHelper.info(Arrays.toString(this.password));
|
||||
return Arrays.equals(enpassword, this.password);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package pro.gravit.launchserver.hibernate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserDAO {
|
||||
User findById(int id);
|
||||
User findByUsername(String username);
|
||||
void save(User user);
|
||||
void update(User user);
|
||||
void delete(User user);
|
||||
List<User> findAll();
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package pro.gravit.launchserver.hibernate;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
import java.util.List;
|
||||
|
||||
public class UserDAOImpl implements UserDAO {
|
||||
|
||||
public User findById(int id) {
|
||||
return HibernateManager.sessionFactory.openSession().get(User.class, id);
|
||||
}
|
||||
|
||||
public User findByUsername(String username) {
|
||||
EntityManager em = HibernateManager.sessionFactory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
|
||||
Root<User> rootUser = personCriteria.from(User.class);
|
||||
personCriteria.select(rootUser).where(cb.equal(rootUser.get("username"), username));
|
||||
User ret = em.createQuery(personCriteria).getSingleResult();
|
||||
em.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void save(User user) {
|
||||
Session session = HibernateManager.sessionFactory.openSession();
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
session.save(user);
|
||||
tx1.commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
public void update(User user) {
|
||||
Session session = HibernateManager.sessionFactory.openSession();
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
session.update(user);
|
||||
tx1.commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
public void delete(User user) {
|
||||
Session session = HibernateManager.sessionFactory.openSession();
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
session.delete(user);
|
||||
tx1.commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
public List<User> findAll() {
|
||||
List<User> users = (List<User>) HibernateManager.sessionFactory.openSession().createQuery("From User").list();
|
||||
return users;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package pro.gravit.launchserver.hibernate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UserService {
|
||||
|
||||
private UserDAO usersDao = new UserDAOImpl();
|
||||
|
||||
public UserService() {
|
||||
}
|
||||
|
||||
public User findUser(int id) {
|
||||
return usersDao.findById(id);
|
||||
}
|
||||
|
||||
public User findUserByUsername(String username) {
|
||||
return usersDao.findByUsername(username);
|
||||
}
|
||||
|
||||
public void saveUser(User user) {
|
||||
usersDao.save(user);
|
||||
}
|
||||
|
||||
public void deleteUser(User user) {
|
||||
usersDao.delete(user);
|
||||
}
|
||||
|
||||
public void updateUser(User user) {
|
||||
usersDao.update(user);
|
||||
}
|
||||
|
||||
public List<User> findAllUsers() {
|
||||
return usersDao.findAll();
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue