QuickServer Development Guide (4) - Add Certification

xiaoxiao2021-03-06  42

Now we add authentication capabilities to the servers you have just created.

View Org.quickServer.Net.server.quickServer document (DOCS folder) You can notice that there is a method

Public void setAuthenTicator (java.lang.string authenticator)

Reading documentation You can see the full name of the Authenticator string in this method is the method of implementing the org.quickserver.net.server.AuthenTICator interface.

The Authenticator interface has two implementations:

Org.quickServer.Net.server.quickAuthenTicator: This class is used to verify the client connected to the QuickServer. It uses only one example to handle all QuickServer authentication. (recommend)

Org.quickServer.Net.Server.ServeraThenticator: This class is also used to verify the client connected to QuickServer, but the process of each authentication creates an instance.

Next, give the EchoServer to the EchoServer. Simple point, the username and password entered by the client are consistent. Even if the verification passes.

First, create a verification class in the same folder: EchoServerQuickAuthenticator

01 Package EchoServer; 02

03 Import Org.quickServer.net.server. *;

04 Import java.io. *;

05

06 Public Class EchoServerquickAuthenticator Extends QuickAuthenTicator {

07

08 Public Boolean Askauthorisation (ClientHandler Clienthandler)

09 throws oException {

10 string username = askSTRINPUT (ClientHandler, "User Name:");

11 string password = askSTRINPUT (ClientHandler, "Password:");

12

13 IF (username == null || Password == Null)

14 returnaf;

15

16 IF (username.equals (password)) {

17 Sendstring (ClientHandler, "Auth OK");

18 returnography;

19} else {

20 SendString (ClientHandler, "Auth Failed");

21 Return False;

twenty two }

twenty three }

twenty four }

This class expands org.quickServer.Net.Server.quickAuthenticator (line 6), in the askAUTHORISATION () method (8 lines), by askSTRINPUT () method Requires the client to enter the username and password, and read into the client input Information (10-11 line). This method inherits from QuickAuthenTicator. If the username is equal to the password, send the correct information and return "True" (16-18 rows), otherwise send an error message and returns "false" (20-21 rows).

Next we have to tell QuickServer to use our newly created verification class to do the verifier. Modify the echoserver.java file created the previous chapter, the code is as follows (bold as a modified code): 01 package echoserver;

02

03 Import com.ddost.net. *;

04 Import com.ddost.net.server. *;

05

06 import java.io. *;

07

08 Public Class EchoServer {

09

10 public static void main (String s []) {

11

12 Quick Server MyServer =

13 New Quick Server ("EchoServer.echocommandrandler);

14 MyServer.SetAuthenticator (15 "EchoServer.echoServerQuickAuthenTicator");

16 MyServer.Setport (4123);

17 MyServer.setName ("EchoServer V 1.0");

18 TRY {

19 myserver.startServer ();

20} catches (APPEXCEPTION E) {

21 System.err.Println ("Error In Server:" E);

twenty two }

twenty three }

twenty four }

OK, compile the modified file, run the program according to the method described in the previous chapter. This time when we click "Connect", the browser will ask us to enter the username and password. If you enter the username and password, you can log in. If you enter an error 5 times, the browser will prompt "-ERR MAX Auth Try Reached" and automatically disconnect. This number and prompt information can be modified by setMaxAuth () and setMaxAuthTrymsg () and setMaxAuthTrymsg () of the QuickServer class. Sometimes we need to exit in the verification instead of waiting for the verification, then enter "quit" does not work. We can modify the code like this: one is to throw an org.quickServer.Net.Appexception from the Askauthorisation () method from the EchoServerQuickAuthenticator class, the code is as follows: string username = askSTRINPUT (ClientHandler, "User Name:") IF (username! = Null && username.equalsignorecase ("quit")) {sendString ("logged out."); Throw new appexception ("quit");} or refer to ClientHandler, turn off the connection, code as follows: string: String username = askStringInput (clientHandler, "User Name:"); if (username = null && username.equalsIgnoreCase ( "QUIT")!) {sendString (clientHandler, "Logged out."); clientHandler.closeConnection (); return false; } ClientHandler objects provide useful information for many client connections, such as IP addresses. For more information, please refer to the API documentation. Note: O Don't store any client-related information in the verification gear class, if needed, you must be stored in the ClientData class - the next chapter will explain the content. O Must confirm that the askauthorisation () method is threaded.

转载请注明原文地址:https://www.9cbs.com/read-53808.html

New Post(0)