You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
5.7 KiB
163 lines
5.7 KiB
4 years ago
|
/*
|
||
|
* Copyright (C), 2018-2021
|
||
|
* Project: starter
|
||
|
* FileName: OAuth2Login
|
||
|
* Author: Louis
|
||
|
* Date: 2021/3/30 22:09
|
||
|
*/
|
||
|
package com.fr.plugin.j7944.sso.request;
|
||
|
|
||
|
import com.fanruan.api.decision.login.LoginKit;
|
||
|
import com.fanruan.api.json.JSONKit;
|
||
|
import com.fanruan.api.log.LogKit;
|
||
|
import com.fanruan.api.net.NetworkKit;
|
||
|
import com.fanruan.api.net.http.HttpKit;
|
||
|
import com.fanruan.api.util.StringKit;
|
||
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
|
||
|
import com.fr.decision.webservice.utils.DecisionServiceConstants;
|
||
|
import com.fr.general.ComparatorUtils;
|
||
|
import com.fr.json.JSONException;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.plugin.j7944.sso.config.SsoConfig;
|
||
|
import com.fr.third.org.apache.http.entity.StringEntity;
|
||
|
|
||
|
import javax.servlet.FilterChain;
|
||
|
import javax.servlet.FilterConfig;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.IOException;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
|
||
|
/**
|
||
|
* <Function Description><br>
|
||
|
* <OAuth2Login>
|
||
|
*
|
||
|
* @author Louis
|
||
|
* @since 1.0.0
|
||
|
*/
|
||
|
public class TokenLogin extends AbstractGlobalRequestFilterProvider {
|
||
|
public static final String REMOTE_DESIGN = "/remote/design";
|
||
|
public static final String RESOURCES_PATH = "/resources";
|
||
|
public static final String FILE_PATH = "/file";
|
||
|
public static final String SYSTEM_INFO = "/system/info";
|
||
|
public static final String MATERIALS_MIN_JS_MAP = "/materials.min.js.map";
|
||
|
public static final String LOGIN_PATH = "/login";
|
||
|
public static final String LOGIN_OTHER = "/login/";
|
||
|
public static final String LOGOUT_PATH = "/logout";
|
||
|
public static final String USER_LANGUAGE = "/v10/user/language";
|
||
|
|
||
|
public static final String USERNAME = "fine_username";
|
||
|
public static final String TICKET = "fine_uuid";
|
||
|
public static final String SYSCOD = "syscod";
|
||
|
|
||
|
private SsoConfig config;
|
||
|
|
||
|
/**
|
||
|
* 过滤器名称
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
@Override
|
||
|
public String filterName() {
|
||
|
return "J7944Filter";
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 过滤规则
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
@Override
|
||
|
public String[] urlPatterns() {
|
||
|
return new String[]{"/*"};
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 过滤器初始化
|
||
|
*
|
||
|
* @param filterConfig
|
||
|
*/
|
||
|
@Override
|
||
|
public void init(FilterConfig filterConfig) {
|
||
|
this.config = SsoConfig.getInstance();
|
||
|
super.init(filterConfig);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 过滤器处理
|
||
|
*
|
||
|
* @param request
|
||
|
* @param response
|
||
|
* @param filterChain
|
||
|
*/
|
||
|
@Override
|
||
|
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) {
|
||
|
try {
|
||
|
if (operation(request, response)) {
|
||
|
filterChain.doFilter(request, response);
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
LogKit.error(e.getMessage(), e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 用户验证登陆操作
|
||
|
*
|
||
|
* @param req
|
||
|
* @param res
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
private boolean operation(HttpServletRequest req, HttpServletResponse res) throws Exception {
|
||
|
String pathInfo = (req.getPathInfo() != null) ? req.getPathInfo() : StringKit.EMPTY;
|
||
|
LogKit.info("sso-TokenLogin-operation-pathInfo:{}", pathInfo);
|
||
|
if (pathInfo.startsWith(REMOTE_DESIGN) || pathInfo.startsWith(LOGIN_OTHER)
|
||
|
|| StringKit.equals(LOGIN_PATH, pathInfo)
|
||
|
|| pathInfo.startsWith(RESOURCES_PATH) || pathInfo.startsWith(LOGOUT_PATH)
|
||
|
|| pathInfo.startsWith(SYSTEM_INFO) || pathInfo.startsWith(MATERIALS_MIN_JS_MAP)
|
||
|
|| pathInfo.startsWith(USER_LANGUAGE) || pathInfo.startsWith(FILE_PATH)) {
|
||
|
return true;
|
||
|
}
|
||
|
String username = NetworkKit.getHTTPRequestParameter(req, USERNAME);
|
||
|
String ticket = NetworkKit.getHTTPRequestParameter(req, TICKET);
|
||
|
String syscod = NetworkKit.getHTTPRequestParameter(req, SYSCOD);
|
||
|
LogKit.info("sso-TokenLogin-operation-uuid:{}, username:{}, syscod:{}", ticket, username, syscod);
|
||
|
if (StringKit.isEmpty(ticket) || StringKit.isBlank(username) || StringKit.isBlank(syscod)) {
|
||
|
return true;
|
||
|
}
|
||
|
if (!checkTicket(ticket, username, syscod)){
|
||
|
return true;
|
||
|
}
|
||
|
String tokenFR = LoginKit.login(req, res, username);
|
||
|
req.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, tokenFR);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 验证ticket
|
||
|
*
|
||
|
* @param ticket
|
||
|
* @return
|
||
|
*/
|
||
|
private boolean checkTicket(String ticket, String username, String syscod) throws IOException {
|
||
|
JSONObject userInfoParams = JSONKit.create();
|
||
|
userInfoParams.put("username", username);
|
||
|
userInfoParams.put("sysid", syscod);
|
||
|
userInfoParams.put("uuid", ticket);
|
||
|
LogKit.info("sso-TokenLogin-checkTicket-params:{}", userInfoParams);
|
||
|
Map<String, String> headers = new HashMap<>();
|
||
|
headers.put("Content-Type", "application/json");
|
||
|
StringEntity stringEntity = new StringEntity(userInfoParams.encode(), "UTF-8");
|
||
|
String userRes = HttpKit.executeAndParse(com.fanruan.api.net.http.rs.HttpRequest.custom()
|
||
|
.url(this.config.getAppDomain()).post(stringEntity).headers(headers).build());
|
||
|
LogKit.info("sso-TokenLogin-checkTicket-userRes:{}", userRes);
|
||
|
try {
|
||
|
int sta = new JSONObject(userRes).getJSONObject("data").getInt("sta");
|
||
|
return ComparatorUtils.equals(sta, 1);
|
||
|
} catch (JSONException e) {
|
||
|
LogKit.error(e.getMessage(), e);
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|