sessionauthenticator.java

来自「piweurrrrq i o fhsadhfka fd dskajc zxkjc」· Java 代码 · 共 74 行

JAVA
74
字号
/* * Copyright (c) 2000 RUChat.com */package com.ruchat.auth;import java.io.IOException;import java.util.Properties;import com.lyrisoft.util.properties.*;import com.lyrisoft.chat.server.remote.AccessDenied;import com.lyrisoft.chat.server.remote.IAuthenticator;import com.lyrisoft.chat.server.remote.Auth;import com.lyrisoft.chat.server.remote.ChatServer;import java.sql.*;/** */public class SessionAuthenticator implements IAuthenticator {    private Connection _connection = null;    public SessionAuthenticator() {        try {            Properties p = PropertyTool.loadProperties("mysqlauth.conf");            String driver = PropertyTool.getString("mysql.class", p);            String url = PropertyTool.getString("mysql.dburl", p);            Class.forName(driver).newInstance();            _connection = DriverManager.getConnection(url);                        ChatServer.log("Connection to Mysql initialized");        }        catch (Exception e) {            ChatServer.log(e);            throw new RuntimeException(e.toString());        }    }    public Auth authenticate(String userId, String password) throws AccessDenied {        String sessionId = userId;                String query = "SELECT username, access\n" +                "FROM acl\n" +                "WHERE string = '" + sessionId + "'";                        Statement stmt = null;        try {            ChatServer.log("SessionAuthenticator: validating session id " + sessionId);            stmt = _connection.createStatement();            ResultSet rs = stmt.executeQuery(query);            if (rs.next()) {                String username = rs.getString("username");                int access = rs.getInt("access");                return new Auth(username, access);            } else {                ChatServer.log("SessionAuthenticator: session id " + sessionId + " is not authenticated");                throw new AccessDenied("You are not authenticated.");            }        }        catch (SQLException e) {            ChatServer.log(e);            throw new AccessDenied("Database access error");        }        finally {            if (stmt != null) {                try {                    stmt.close();                }                 catch (SQLException e) {}            }        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?