Browse Source

添加过滤器,禁止访问指定资源

Signed-off-by: Jinbokai <Jinbokai@fanruan.com>
final/10.0
Jinbokai 6 years ago
parent
commit
55d042c790
  1. 22
      designer-base/src/main/java/com/fr/start/server/FineEmbedServerActivator.java
  2. 29
      designer-base/src/main/java/com/fr/start/server/ForbidType.java

22
designer-base/src/main/java/com/fr/start/server/FineEmbedServerActivator.java

@ -14,6 +14,8 @@ import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException; import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper; import org.apache.catalina.Wrapper;
import org.apache.catalina.deploy.FilterDef;
import org.apache.catalina.deploy.FilterMap;
import org.apache.catalina.loader.WebappLoader; import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.Tomcat;
@ -62,6 +64,10 @@ public class FineEmbedServerActivator extends Activator {
String docBase = new File(WorkContext.getCurrent().getPath()).getParent(); String docBase = new File(WorkContext.getCurrent().getPath()).getParent();
String appName = "/" + FRContext.getCommonOperator().getAppName(); String appName = "/" + FRContext.getCommonOperator().getAppName();
Context context = tomcat.addContext(appName, docBase); Context context = tomcat.addContext(appName, docBase);
//添加过滤器
addFilter(context);
addDefaultServlet(context); addDefaultServlet(context);
//覆盖tomcat的WebAppClassLoader //覆盖tomcat的WebAppClassLoader
context.setLoader(new FRTomcatLoader()); context.setLoader(new FRTomcatLoader());
@ -72,6 +78,22 @@ public class FineEmbedServerActivator extends Activator {
classes.add(FineWebApplicationInitializer.class); classes.add(FineWebApplicationInitializer.class);
context.addServletContainerInitializer(initializer, classes); context.addServletContainerInitializer(initializer, classes);
} }
private void addFilter(Context context){
FilterDef filterDef = new FilterDef();
filterDef.setFilterName("Forbidden");
filterDef.setFilterClass("com.fr.start.server.ForbidType");
FilterMap filterMap = new FilterMap();
filterMap.setFilterName("Forbidden");
//指定过滤文件类型
filterMap.addURLPattern("*.txt");
filterMap.addURLPattern("*.xml");
filterMap.addURLPattern("*.jar");
context.addFilterDef(filterDef);
context.addFilterMap(filterMap);
}
private void addDefaultServlet(Context context) { private void addDefaultServlet(Context context) {

29
designer-base/src/main/java/com/fr/start/server/ForbidType.java

@ -0,0 +1,29 @@
package com.fr.start.server;
import javax.servlet.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
public class ForbidType implements Filter {
private String message;
public void init(FilterConfig config) throws ServletException {
message = config.getInitParameter("msg");
System.out.println("Filter is ready.");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println(message);
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.sendRedirect("/webroot/decision");
}
public void destroy() {
}
}
Loading…
Cancel
Save