文档目录
应用集成提供一种简单的集成到现有系统的方案,一般情况下如非特殊需求不建议采用应用集成的方式,最好独立部署。独立部署不但拥有更好的灵活性而且对现有系统零侵入。
1. Servlet集成
请参考web.xml注册finderweb提供的DispatchServlet
2. Spring-MVC集成
/**
* springmvc 集成
* Controller方式
*/
@Controller
public class FinderController implements InitializingBean {
@Autowired
private ServletContext servletContext;
private ActionDispatcher dispatcher;
private static final String LOGIN_URL = "/finder?action=finder.login";
@Override
public void afterPropertiesSet() throws Exception {
/**
* 使用 Servlet3.0 的文件上传
*/
this.servletContext.setAttribute("javax.servlet.multipart.support", true);
this.dispatcher = ActionDispatcherFactory.create(this.servletContext);
}
/**
* 如果不使用Filter方式, 可以将Filter注掉, 并将该类的 Controller 注解放开
* 但是如果使用Controller方式, Finder的集群功能将失效
*
* 其他集成方式请在web.xml中配置 com.finder.web.servlet.DispatchServlet 的方式。
* 并且要将servlet配置到最前面,确保在其他mvc框架之前执行。
*/
@RequestMapping(value = "/finder")
public void execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
if(com.skin.finder.filter.SessionFilter.check(request, response, LOGIN_URL)) {
this.dispatcher.service(request, response);
}
}
}
在web.xml中注册com.skin.finder.filter.HtmlSupportFilter
<filter>
<filter-name>HtmlSupportFilter</filter-name>
<filter-class>com.skin.finder.filter.HtmlSupportFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HtmlSupportFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3. SpringBoot集成
1. 将finder-web-x.x.x.jar放到磁盘的任意目录。
2. 将demo.zip中的代码复制到你的项目中,新建一个Controller, 在Controller中调用FinderSupport.service(request, response)。
3. 重写一个filter并继承HtmlSupportFilter。
// springboot 集成必须将finder-web-2.5.0.jar 作为一个独立的jar文件加载, 而不能打包在YourProject.jar中。
// java -Dloader.path=webapp/WEB-INF/lib,webapp/WEB-INF/classes -jar bin\YourProject.jar
/**
* springboot springmvc 集成
* Filter方式
*/
public class FinderFilter extends com.skin.finder.filter.DispatchFilter {
/**
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
/**
* 使用servlet3.0的multipart上传
* 如果 springboot 配置项 spring.http.multipart.enabled=true, 并且容器支持servlet3.0, springboot就会配置MultipartConfig
* 这将导致容器会解析 http body, finder将无法再读取 http body, 从而使finder的文件上传无法获取到上传文件。
* 通过此配置项指定finder使用容器提供的上传支持。
*
* 如果要使用finder自己的上传组件:
* 1. spring.http.multipart.enabled=false
* 2. javax.servlet.multipart.support=false 或者不设置此属性
*/
ServletContext servletContext = filterConfig.getServletContext();
servletContext.setAttribute("javax.servlet.multipart.support", true);
super.init(filterConfig);
}
}
/**
* springboot 注册 Filter
*/
@Configuration
public class FilterRegister {
@Bean
public FilterRegistrationBean registrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new FinderFilter());
filterRegistrationBean.addUrlPatterns("/finder");
filterRegistrationBean.setOrder(Integer.MIN_VALUE);
filterRegistrationBean.setEnabled(true);
return filterRegistrationBean;
}
}
注:如果不需要html预览功能,HtmlSupportFilter可以不注册。
登录接口
## HTTP 接口调用
FinderWeb目前没有提供专门对外的HTTP接口,所有的功能均可通过一般的HTTP方式调用,并且自动支持集群,调用任意一台主机均可。
## 调用方式
1. 先调用登录接口:http://ip:port/finder?action=finder.login, 用户名,密码等数据RSA加密之后post发送。登录之后从Http Header中获取Cookie并保存到本地。
2. 调用其他任意 HTTP 接口均需要带上登录时服务端返回的Cookie。
# 用户名密码加密过程
timestamp = 时间戳,精确到毫秒,与finderweb服务器误差必须在5分钟之内
userName = urlEncode(rsaEncrypt(userName + "|" + timestamp))
password = urlEncode(rsaEncrypt(password + "|" + timestamp))
token = 从finderweb服务端获取, http://ip:port/finder?action=api.status
RSA加密的publicKey从finderweb服务端获取
以上数据准备好之后,拼接为字符串:
userName=${userName}&password=${password}&token=${token}×tamp=${timestamp}
Http.post("http://ip:port/finder?action=finder.login")
## PublicKey
## 仅最新版本支持该接口, 旧版需要解析登录页面获取
http://ip:port/finder?action=api.login.key
## java, python2.7, python3等客户端源码可在QQ群下载, 其中python2.7版本为其他用户开发。
SSO集成
// 单点登录集成请实现 UserService 和 SessionService 接口;
// 接口声明:
public interface UserManager {
/**
* @param userName
* @return User
*/
User getByName(String userName);
/**
* @param user
* @param password
* @return boolean
*/
boolean auth(User user, String password);
/**
* @param pageNum
* @param pageSize
* @return List
*/
List getUserList(int pageNum, int pageSize);
/**
* @return int
*/
int getUserCount();
/**
* @param user
* @return int
*/
int create(User user);
/**
* @param user
* @return int
*/
int update(User user);
/**
* @param userName
* @return int
*/
int delete(String userName);
}
public interface SessionManager {
/**
* @param request
* @return UserSession
*/
UserSession getSession(HttpServletRequest request);
/**
* @param request
* @param response
* @param userSession
* @param expiry
*/
void registe(HttpServletRequest request, HttpServletResponse response, UserSession userSession, int expiry);
/**
* @param response
*/
void logout(HttpServletResponse response);
}
# 服务注册
# service.conf, 修改为应用方的实现类
com.skin.finder.user.UserManager = com.skin.finder.user.SimpleUserManager
com.skin.finder.user.SessionManager = com.skin.finder.user.DefaultSessionManager