Protection Agent Mode-access Proxy
Protection Agent Mode (Access proxy), also known as protect proxy. This proxy is used to make some access restrictions on the functionality of real objects and to authenticate at the proxy layer. Validation is used to invoke the corresponding method of the real principal object.
The simulation scenario is as follows: A platform of the system has a query function, can be based on keywords to query, before the query to check the keyword, if the key words are sensitive words, then will not be processed. If the keyword is successfully queried, a log is logged.
(Sensitive word verification also can be replaced with ' user authentication ', such as other authentication)
Searchable interface
Both the entity class and the proxy class need to implement this interface. Because the main function in this example is search. What the proxy class does is take over (proxy) This method, and then perform the method before performing the sensitive word validation, and perform this method successfully when logging.
Public interface Searchable { string search (string keyword);
Realsearch class
This is the real entity object, his main method search () in this case is just an SQL statement, in the real scene can be SQL query ' result set '.
public class Realsearch implements searchable {public Realsearch () { } /** * Real Query */Public String search (string keyword) { return "real query: SELECT * from users WHERE keyword =" + keyword; }}
Loggerhelper class
Tool class for logging (of course, only analog logging in this lining)
public class Loggerhelper { /** * Record log * /public static void log (String keyword) { SYSTEM.OUT.PRINTLN ("Analog log4j logging >> info >> login >> keyword: [" + keyword + "]");} }
Validatorhelper class
A tool class that verifies whether sensitive information is being used.
public class Validatorhelper { /** * Sensitive word blacklist */ private static final hashset<string> blacklist = New Hashset<string> () {{ Add ("jack123"); Add ("json898"); Add ("Nancy");} ; /** * Sensitive word verification * If keyword is in the blacklist blacklist, then return false * If keyword is not in blacklist blacklist, then return True */ public static Boolean validate (String userId) { return!blacklist.contains (Userid.trim ());} }
Proxysearch class
The proxy class, the thing to do is to take over the search () method of the Realsearch class, and then perform this method before performing the sensitive word validation, which is performed when the method is successfully logged.
public class Proxysearch implements searchable { private realsearch searcher; Public Proxysearch () { searcher = new Realsearch (); } /** * Pre-Query Authorization action */ Private boolean check (String keyword) { if (validatorhelper.validate (keyword)) { System.out.println ("Not sensitive words:" + keyword); return true; } else { System.out.println ("is a sensitive word:" + keyword); return false; } } /** * Query action template * /Public String search (string keyword) { if (check (keyword)) { //is not a sensitive word, Then query processing String result = searcher.search (keyword); The query succeeds and then records a log . LoggerHelper.log (keyword); Returns the result of the query , or else { //is a sensitive word that cannot be processed properly. return null;}} }
Main
Invoke/debug/run in this class
public class Main {public static void Main (string[] args) { searchable searcher = new Proxysearch (); String keyword = "football"; String result = searcher.search (keyword); SYSTEM.OUT.PRINTLN (result);} }
The results of the operation are as follows:
Protection Proxy Mode-access proxy (Java implementation)