2
I created a project by putting system authentication including login and password directly in the Java code, but I know this is super wrong, the most recommended for implementation of authentication is the system search login and password direct from the bank, in addition to authentication the user will also need permission, that is, it does not mean that when logging in the user will have direct view of the pages, the rules of navigability will put in Spring Security, what is being complicated for me is to perform entity mapping in the Java classes.
I know how to create the database through SQL, but I don’t know how to abstract it for entity mapping in Java, see the model that build in SQL;
Entity User;
CREATE TABLE usuario (
codigo BIGINT(20) PRIMARY KEY AUTO_INCREMENT,
nome VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
senha VARCHAR(120) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Entity Group;
CREATE TABLE grupo (
codigo BIGINT(20) PRIMARY KEY,
nome VARCHAR(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Entity Permission;
CREATE TABLE permissao (
codigo BIGINT(20) PRIMARY KEY,
nome VARCHAR(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Entity Group surgery;
CREATE TABLE grupopermissao (
codigo_grupo BIGINT(20) NOT NULL,
codigo_permissao BIGINT(20) NOT NULL,
PRIMARY KEY (codigo_grupo, codigo_permissao),
FOREIGN KEY (codigo_grupo) REFERENCES grupo(codigo),
FOREIGN KEY (codigo_permissao) REFERENCES permissao(codigo)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Entity User group;
CREATE TABLE usuariogrupo (
codigo_usuario BIGINT(20) NOT NULL,
codigo_grupo BIGINT(20) NOT NULL,
PRIMARY KEY (codigo_usuario, codigo_grupo),
FOREIGN KEY (codigo_usuario) REFERENCES usuario(codigo),
FOREIGN KEY (codigo_grupo) REFERENCES grupo(codigo)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I will make an explanation of my intentions regarding these tables above, the tables would behave as follows.
In the system the user would enter the registration screen permissions and would register only two permissions;
- VISUALIZAR_CADASTRO
- VISUALIZAR_PESQUISA
After the user would enter the group registration screen and would register only two groups;
- Administrators
- Salesmen
Then the user would enter a registration screen to associate the groups to the permissions, then you imagine you enter a screen with two Combobox, one would list the groups and the other list the permissions, after choosing would only sobmeter the form on that screen.
if administrators were code 1 and sellers were code 2 then permission to register code 1 and view searches if code 2 would look something like this;
Group surgery
- 1,1
- 1,2
- 2,1
This means that administrator would be in the group that has permission to search and register and group of sellers only to register.
I’m sorry about the long post, but it was necessary to understand my context.
My only difficulty is that I don’t know how to map my entities in relation to the reality described above, I don’t even know where to go.
Hi wladyband. I don’t know if I understood the doubt right, but to me it seems that you have two relationships
@ManyToMany
, that is, if you want to handle this with JPA you will have an entityGrupo
with aList<Usuario>
and aList<Permisao>
. On the side ofPermisao
and ofUsuario
you will have aList<Grupo>
. Note however that if you will use Spring Security, there is no reason to invent the wheel. It has the scheme Group Authorities and theUserDetailsService
.– Anthony Accioly