7 changed files with 217 additions and 1 deletions
Binary file not shown.
@ -1,3 +1,6 @@
|
||||
# open-JSD-8308 |
||||
|
||||
JSD-8308开源任务材料 |
||||
JSD-8308开源任务材料\ |
||||
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系hugh处理。 |
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
||||
<plugin> |
||||
<id>com.fr.plugin.xxxx.sso</id> |
||||
<name><![CDATA[单点登录]]></name> |
||||
<active>yes</active> |
||||
<version>1.0</version> |
||||
<env-version>10.0</env-version> |
||||
<jartime>2018-07-31</jartime> |
||||
<vendor>fr.open</vendor> |
||||
<description><![CDATA[单点登录]]></description> |
||||
<change-notes><![CDATA[]]></change-notes> |
||||
<extra-decision> |
||||
<GlobalRequestFilterProvider class="com.fr.plugin.xxxx.sso.SsoFilter"/> |
||||
</extra-decision> |
||||
<function-recorder class="com.fr.plugin.xxxx.sso.SsoFilter"/> |
||||
</plugin> |
@ -0,0 +1,6 @@
|
||||
此次完成功能如下 |
||||
a、单点登录 |
||||
|
||||
1、将压缩文件解压后的rili.properties配置文件拷贝至 %部署路径%/WEB-INF/resources |
||||
2、安装本插件,插件安装见连接http://help.finereport.com/doc-view-2198.html |
||||
3、进入系统测试单点登录,访问地址为http://ip:port/webroot/decision?sessionToken=XXXXXXXXXXXXXXX |
@ -0,0 +1,90 @@
|
||||
package com.fr.plugin.xxxx.sso; |
||||
|
||||
import com.fr.data.NetworkHelper; |
||||
import com.fr.decision.authority.data.User; |
||||
import com.fr.decision.mobile.terminal.TerminalHandler; |
||||
import com.fr.decision.webservice.utils.DecisionServiceConstants; |
||||
import com.fr.decision.webservice.v10.login.LoginService; |
||||
import com.fr.decision.webservice.v10.login.TokenResource; |
||||
import com.fr.decision.webservice.v10.user.UserService; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.stable.web.Device; |
||||
|
||||
import javax.servlet.FilterChain; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.util.Properties; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @since 2021/7/15 |
||||
*/ |
||||
public class CommonUtils { |
||||
|
||||
public static String getProperty(Properties props, String key, String defaultValue, boolean allowBlank) { |
||||
String value = props.getProperty(key); |
||||
if (StringUtils.isNotBlank(value)) { |
||||
return value; |
||||
} else { |
||||
if (allowBlank) { |
||||
return defaultValue; |
||||
} else { |
||||
throw new IllegalArgumentException("Property[" + key + "] cann't be blank."); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public static String getProperty(Properties props, String key, boolean allowBlank) { |
||||
return getProperty(props, key, null, allowBlank); |
||||
} |
||||
|
||||
public static String getProperty(Properties props, String key) { |
||||
return getProperty(props, key, null, true); |
||||
} |
||||
|
||||
public static boolean isLogin(HttpServletRequest request) { |
||||
String oldToken = TokenResource.COOKIE.getToken(request); |
||||
return oldToken != null && checkTokenValid(request, (String) oldToken); |
||||
} |
||||
|
||||
private static boolean checkTokenValid(HttpServletRequest req, String token) { |
||||
try { |
||||
Device device = NetworkHelper.getDevice(req); |
||||
LoginService.getInstance().loginStatusValid(token, TerminalHandler.getTerminal(req, device)); |
||||
return true; |
||||
} catch (Exception ignore) { |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 跳转到过滤器链中的下一个过滤器 |
||||
* |
||||
* @param request |
||||
* @param response |
||||
* @param chain |
||||
*/ |
||||
public static void next(HttpServletRequest request, HttpServletResponse response, FilterChain chain) { |
||||
try { |
||||
chain.doFilter(request, response); |
||||
} catch (Exception e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
|
||||
public static void login(String username, HttpServletRequest request, HttpServletResponse response) { |
||||
try { |
||||
User user = UserService.getInstance().getUserByUserName(username); |
||||
if (user == null) { |
||||
throw new RuntimeException("系统未授权, 当前用户是\"" + username + "\""); |
||||
} |
||||
String token = LoginService.getInstance().login(request, response, username); |
||||
request.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, token); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error("sso >> Failed to login with[" + username + "]", e); |
||||
throw new RuntimeException("用户\"" + username +"\"登录失败"); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,100 @@
|
||||
package com.fr.plugin.rili.sso; |
||||
|
||||
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
||||
import com.fr.general.PropertiesUtils; |
||||
import com.fr.general.http.HttpToolbox; |
||||
import com.fr.json.JSONObject; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.transform.FunctionRecorder; |
||||
import com.fr.stable.StringUtils; |
||||
|
||||
import javax.servlet.FilterChain; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.IOException; |
||||
import java.util.Collections; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
import java.util.Properties; |
||||
import java.util.stream.Stream; |
||||
|
||||
import static com.fr.plugin.rili.sso.CommonUtils.*; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @since 2021/7/15 |
||||
*/ |
||||
@FunctionRecorder |
||||
public class SsoFilter extends AbstractGlobalRequestFilterProvider { |
||||
|
||||
private static String[] NOT_FILTER = { |
||||
"/decision/file", |
||||
"/decision/resources", |
||||
"/system", |
||||
"/materials.min.js.map", |
||||
"/remote", |
||||
"/login", |
||||
"/login/config" |
||||
}; |
||||
|
||||
private final String apiGetUser; |
||||
|
||||
public SsoFilter() { |
||||
Properties props = PropertiesUtils.getProperties("xxxx"); |
||||
this.apiGetUser = getProperty(props, "api.get-user", false); |
||||
} |
||||
|
||||
@Override |
||||
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) { |
||||
if (isAccept(request)) { |
||||
next(request, response, chain); |
||||
return; |
||||
} |
||||
|
||||
try { |
||||
String token = request.getParameter("sessionToken"); |
||||
FineLoggerFactory.getLogger().info("参数 sessionToken 的值为\"{}\"", token); |
||||
if (StringUtils.isNotBlank(token)) { |
||||
login(getUsername(token), request, response); |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error("单点登录处理失败.", e); |
||||
} |
||||
next(request, response, chain); |
||||
} |
||||
|
||||
@Override |
||||
public String filterName() { |
||||
return "sso"; |
||||
} |
||||
|
||||
@Override |
||||
public String[] urlPatterns() { |
||||
return new String[]{"/*"}; |
||||
} |
||||
|
||||
private boolean isAccept(HttpServletRequest request) { |
||||
String url = request.getRequestURL().toString(); |
||||
return isLogin(request) || Stream.of(NOT_FILTER).anyMatch(url::contains); |
||||
} |
||||
|
||||
private String getUsername(String token) throws IOException { |
||||
FineLoggerFactory.getLogger().info("获取用户信息的接口地址为: \"{}\"", apiGetUser); |
||||
Map<String, String> headers = new HashMap<>(); |
||||
headers.put("Cookie", "JSESSIONID=" + token); |
||||
String res = HttpToolbox.get(apiGetUser, Collections.emptyMap(), headers); |
||||
FineLoggerFactory.getLogger().info("获取用户信息接口返回内容为: \"{}\"", res); |
||||
JSONObject body = new JSONObject(res); |
||||
if (body.has("status") && body.getInt("status") == 200) { |
||||
String username = body.getJSONObject("data").getString("psnAccount"); |
||||
if (StringUtils.isNotBlank(username)) { |
||||
FineLoggerFactory.getLogger().info("获取到的用户名为: \"{}\"", username); |
||||
return username; |
||||
} |
||||
throw new RuntimeException("获取到的用户名为空"); |
||||
} |
||||
throw new RuntimeException("获取用户名失败,详见接口返回内容"); |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue