简体   繁体   English

如何知道在Java应用程序中登录的用户是谁?

[英]How know which is the user who is login in java application?

I created a java application where login credentials are required to have access. 我创建了一个Java应用程序,需要登录凭据才能访问。 How can I know which is the user who is logged in? 我怎么知道哪个用户登录? Is there any way to know? 有什么办法知道吗?

I do not know if they realized the doubt, if not I try to explain better. 我不知道他们是否意识到了疑问,如果不是,我会尝试更好地解释。

----------------CODE----------------- - - - - - - - - 码 - - - - - - - - -

private void jBLoginActionPerformed(java.awt.event.ActionEvent evt) {                                        

String sql="select * from logins where username=? and password=? and idTipoLogin=?";
try{
    pst=(PreparedStatement) conexao.prepareStatement(sql);
    pst.setString(1, jTUser.getText());
    pst.setString(2, jPass.getText());
    pst.setString(3, jComboBoxTipoLogin.getSelectedItem().toString());

    rs=pst.executeQuery();
    if(rs.next()){

        String idTipoLogin = rs.getString("idTipoLogin"); 

        if (idTipoLogin.equals("Administrador")) {

            jTTarefasAdmin ah = new jTTarefasAdmin();
            ah.setVisible(true);

        }
         else {
            jTTarefasTecnico eh = new jTTarefasTecnico();
            eh.setVisible(true);

        }
        this.setVisible(false);
        }
    else{
        JOptionPane.showMessageDialog(null, "User Invalid");
          }
        }catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
        }

} }

在此处输入图片说明

The idea is to know what is the user who is logged into the system. 这个想法是要知道登录到系统的用户是什么。 As there are several users, I want to know specifically what is being accessed to login. 由于有多个用户,因此我想特别了解要登录的内容。

You can maintain a Collection of the logged in user, I think a HashMap would be good enough for your purpose. 您可以维护已登录用户的Collection,我认为HashMap足以满足您的目的。

 // This should be a class member
 Map<String, String> loggedInUsers = new HashMap<String, String>();

 // Now when user logs in add it to the HashMap
 loggedInUser.put(jTUser.getText(), jTUser.getText());

 // When a user logs out remove the user from the Map
 loggedInUser.remove(jTUser.getText());

If you just want a plain list of Users then you can store them in an ArrayList. 如果只需要一个简单的用户列表,则可以将它们存储在ArrayList中。

 List<String> loggedInUsers = new ArrayList<String>();
 loggedInUser.add(jTUser.getText());

From what I understood, you have one system that does the authentication of the user and the same user has access to other systems. 据我了解,您拥有一个可以对用户进行身份验证的系统,并且同一用户可以访问其他系统。 On these other systems you want to know who the user is. 在这些其他系统上,您想知道用户是谁。

You could do this: Once the user is authenticated, create a unique id and put that in the session. 您可以这样做:验证用户身份后,创建一个唯一的ID并将其放入会话中。 Then access the session every time you need to know if the user is logged in. For other systems, pass that unique id to the system and allow them to query for the user. 然后,每次您需要知道用户是否登录时,都访问该会话。对于其他系统,请将该唯一ID传递给系统,并允许他们查询用户。

Flow: 1. User logs in 2. System creates a unique id 3. Move that id with every request the user makes to other systems 4. On the other systems, allow them to access the authentication server and query based on the id. 流程:1.用户登录2.系统创建唯一的ID。3.将用户提出的每个请求的ID移至其他系统。4.在其他系统上,允许他们访问身份验证服务器并基于该ID查询。

This is a lot to do manually and there are plenty of systems out there that handle it all for you. 手动要做很多事情,并且有很多系统可以为您处理所有这些事情。

Links to get you started 链接让您入门

http://en.wikipedia.org/wiki/Single_sign-on http://en.wikipedia.org/wiki/Single_sign-on

http://en.wikipedia.org/wiki/Federated_identity http://en.wikipedia.org/wiki/Federated_identity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM