10 changed files with 138 additions and 13 deletions
Binary file not shown.
@ -1,18 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin> |
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
||||
<plugin> |
||||
<id>com.fr.decision.auth.http</id> |
||||
<main-package>com.fr.decision.auth.http</main-package> |
||||
<name><![CDATA[http认证]]></name> |
||||
<active>yes</active> |
||||
<version>1.0</version> |
||||
<version>1.1</version> |
||||
<env-version>10.0~</env-version> |
||||
<jartime>2018-10-20</jartime> |
||||
<vendor>author</vendor> |
||||
<description><![CDATA[定制http认证。]]></description> |
||||
<change-notes><![CDATA[ |
||||
[2019-09-20]增加了灵活的脚本配置。<br/> |
||||
[2018-11-22]增加了插件功能说明。<br/> |
||||
]]></change-notes> |
||||
<extra-core> |
||||
<LocaleFinder class="com.fr.decision.auth.http.HttpAuthorizeLocaleBridge"/> |
||||
</extra-core> |
||||
<extra-decision> |
||||
<HttpAuthorizeProvider class="com.fr.decision.auth.http.HttpAuthorizeBridge"/> |
||||
</extra-decision> |
||||
<lifecycle-monitor class="com.fr.decision.auth.http.HttpAuthorizeInitializeMonitor"/> |
||||
<function-recorder class="com.fr.decision.auth.http.HttpAuthorizeBridge"/> |
||||
</plugin> |
@ -1,43 +1,67 @@
|
||||
package com.fr.decision.auth.http; |
||||
|
||||
import com.fr.decision.authorize.impl.HttpPassport; |
||||
import com.fr.decision.config.FSConfig; |
||||
import com.fanruan.api.decision.login.LoginKit; |
||||
import com.fanruan.api.log.LogKit; |
||||
import com.fanruan.api.net.http.HttpKit; |
||||
import com.fanruan.api.script.ScriptKit; |
||||
import com.fanruan.api.util.StringKit; |
||||
import com.fr.decision.auth.http.config.HttpAuthorizeConfig; |
||||
import com.fr.decision.fun.impl.AbstractHttpAuthorizeProvider; |
||||
import com.fr.general.http.HttpToolbox; |
||||
import com.fr.intelli.record.Focus; |
||||
import com.fr.intelli.record.Original; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.record.analyzer.EnableMetrics; |
||||
|
||||
import javax.script.ScriptEngine; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* http认证更改内置的认证逻辑,只要返回值不是"false",则认为是认证成功的。 |
||||
* http认证更改内置的认证逻辑,可以通过脚本制定判断认证成功或者失败的策略 |
||||
*/ |
||||
@EnableMetrics |
||||
public class HttpAuthorizeBridge extends AbstractHttpAuthorizeProvider { |
||||
|
||||
private static final String SCRIPT_TPL = "(function(username, value) {return %s;})(\"%s\", \"%s\")"; |
||||
|
||||
private ScriptEngine engine = ScriptKit.newScriptEngine(); |
||||
|
||||
@Override |
||||
public Scope scope() { |
||||
return Scope.REPLACE; |
||||
} |
||||
|
||||
@Override |
||||
@Focus(id="com.fr.decision.auth.http", text = "", source = Original.PLUGIN) |
||||
@Focus(id = "com.fr.decision.auth.http", text = "", source = Original.PLUGIN) |
||||
public boolean authorize(String username, String inputPassword, String savedPassword, String hashPassword) { |
||||
Map<String, String> queryMap = new HashMap<String, String>(); |
||||
queryMap.put("username", username); |
||||
queryMap.put("inputPassword", inputPassword); |
||||
HttpPassport httpPassport = (HttpPassport) FSConfig.getInstance().getPassport(); |
||||
String httpUrl = LoginKit.getHttpPassportUrl(); |
||||
if (StringKit.isEmpty(httpUrl)) { |
||||
return false; |
||||
} |
||||
try { |
||||
String resultText = HttpToolbox.get(httpPassport.getUrl(), queryMap); |
||||
if (!"false".equals(resultText)) { |
||||
return true; |
||||
String resultText = HttpKit.get(httpUrl, queryMap); |
||||
String condition = HttpAuthorizeConfig.getInstance().getCondition(); |
||||
if (StringKit.isBlank(condition)) { |
||||
if (!"false".equals(resultText)) { |
||||
return true; |
||||
} |
||||
} else { |
||||
Object r = engine.eval(String.format(SCRIPT_TPL, HttpAuthorizeConfig.getInstance().getCondition(), username, resultText)); |
||||
if (Boolean.TRUE == r) { |
||||
return true; |
||||
} |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
LogKit.error(e.getMessage(), e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static void main(String... args) throws Exception { |
||||
ScriptEngine scriptEngine = ScriptKit.newScriptEngine(); |
||||
Object r = scriptEngine.eval(String.format(SCRIPT_TPL, "value!=\"false\"", "alex", "false")); |
||||
System.out.println("r=" + r); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,23 @@
|
||||
package com.fr.decision.auth.http; |
||||
|
||||
import com.fr.decision.auth.http.config.HttpAuthorizeConfig; |
||||
import com.fr.plugin.context.PluginContext; |
||||
import com.fr.plugin.observer.inner.AbstractPluginLifecycleMonitor; |
||||
|
||||
/** |
||||
* @author richie |
||||
* @version 10.0 |
||||
* Created by richie on 2019/9/20 |
||||
*/ |
||||
public class HttpAuthorizeInitializeMonitor extends AbstractPluginLifecycleMonitor { |
||||
|
||||
@Override |
||||
public void afterRun(PluginContext pluginContext) { |
||||
HttpAuthorizeConfig.getInstance(); |
||||
} |
||||
|
||||
@Override |
||||
public void beforeStop(PluginContext pluginContext) { |
||||
|
||||
} |
||||
} |
@ -0,0 +1,15 @@
|
||||
package com.fr.decision.auth.http; |
||||
|
||||
import com.fr.stable.fun.impl.AbstractLocaleFinder; |
||||
|
||||
/** |
||||
* @author richie |
||||
* @version 10.0 |
||||
* Created by richie on 2019/9/20 |
||||
*/ |
||||
public class HttpAuthorizeLocaleBridge extends AbstractLocaleFinder { |
||||
@Override |
||||
public String find() { |
||||
return "com/fr/decision/auth/http/http"; |
||||
} |
||||
} |
@ -0,0 +1,42 @@
|
||||
package com.fr.decision.auth.http.config; |
||||
|
||||
import com.fanruan.api.conf.BaseConfiguration; |
||||
import com.fanruan.api.conf.HolderKit; |
||||
import com.fr.config.Identifier; |
||||
import com.fr.config.Status; |
||||
import com.fr.config.holder.Conf; |
||||
|
||||
/** |
||||
* @author richie |
||||
* @version 10.0 |
||||
* Created by richie on 2019/9/20 |
||||
*/ |
||||
public class HttpAuthorizeConfig extends BaseConfiguration { |
||||
|
||||
private static volatile HttpAuthorizeConfig instance; |
||||
|
||||
public static HttpAuthorizeConfig getInstance() { |
||||
if (instance == null) { |
||||
instance = getConfigInstance(HttpAuthorizeConfig.class); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
@Identifier(value = "condition", name = "Plugin-Http_Config_Property_Text", description = "Plugin-Http_Config_Property_Text_Description", status = Status.SHOW) |
||||
private Conf<String> condition = HolderKit.simple("res != \"false\""); |
||||
|
||||
public String getCondition() { |
||||
return condition.get(); |
||||
} |
||||
|
||||
public void setCondition(String condition) { |
||||
this.condition.set(condition); |
||||
} |
||||
|
||||
@Override |
||||
public Object clone() throws CloneNotSupportedException { |
||||
HttpAuthorizeConfig cloned = (HttpAuthorizeConfig) super.clone(); |
||||
cloned.condition = (Conf<String>) condition.clone(); |
||||
return cloned; |
||||
} |
||||
} |
@ -0,0 +1,2 @@
|
||||
Plugin-Http_Config_Property_Text=Condition Script Text |
||||
Plugin-Http_Config_Property_Text_Description=ES5 script text, value as http return text and username as input username |
@ -0,0 +1,2 @@
|
||||
Plugin-Http_Config_Property_Text=Condition Script Text |
||||
Plugin-Http_Config_Property_Text_Description=ES5 script text, value as http return text and username as input username |
@ -0,0 +1,2 @@
|
||||
Plugin-Http_Config_Property_Text=\u5224\u65AD\u811A\u672C |
||||
Plugin-Http_Config_Property_Text_Description=\u652F\u6301ES5\u7684JavaScript\u811A\u672C\uFF0C\u53C2\u6570username\u548Cvalue\u5206\u522B\u8868\u793A\u8F93\u5165\u7684\u7528\u6237\u540D\u548Chttp\u8BA4\u8BC1\u670D\u52A1\u5668\u8FD4\u56DE\u7684\u6587\u672C |
Loading…
Reference in new issue