-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from qq254963746/develop
add mapdb FailStore and other upgrade
- Loading branch information
Showing
34 changed files
with
760 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
lts-admin/src/main/java/com/lts/web/support/csrf/CSRFHandlerInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.lts.web.support.csrf; | ||
|
||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* A Spring MVC <code>HandlerInterceptor</code> which is responsible to enforce CSRF token validity on incoming posts | ||
* requests. The interceptor should be registered with Spring MVC servlet using the following syntax: | ||
* <p/> | ||
* <mvc:interceptors> | ||
* <bean class="com.lts.web.support.csrf.CSRFHandlerInterceptor"/> | ||
* </mvc:interceptors> | ||
* | ||
* @author Robert HG ([email protected]) on 11/10/15. | ||
*/ | ||
public class CSRFHandlerInterceptor extends HandlerInterceptorAdapter { | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, | ||
HttpServletResponse response, Object handler) throws Exception { | ||
|
||
if (handler instanceof DefaultServletHttpRequestHandler) { | ||
return true; | ||
} | ||
|
||
if (request.getMethod().equalsIgnoreCase("GET")) { | ||
return true; | ||
} else { | ||
String sessionToken = CSRFTokenManager.getToken(request.getSession()); | ||
String requestToken = CSRFTokenManager.getToken(request); | ||
// 检查 csrf token是否正确 | ||
if (sessionToken.equals(requestToken)) { | ||
return true; | ||
} else { | ||
response.sendError(HttpServletResponse.SC_FORBIDDEN, "Bad or missing CSRF value"); | ||
return false; | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
lts-admin/src/main/java/com/lts/web/support/csrf/CSRFTokenManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.lts.web.support.csrf; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpSession; | ||
import java.util.UUID; | ||
|
||
/** | ||
* @author Robert HG ([email protected]) on 11/10/15. | ||
*/ | ||
public final class CSRFTokenManager { | ||
|
||
static final String CSRF_PARAM_NAME = "csrfToken"; | ||
|
||
public final static String CSRF_TOKEN_FOR_SESSION_ATTR_NAME = CSRFTokenManager.class.getSimpleName() + ".token"; | ||
|
||
private CSRFTokenManager() { | ||
} | ||
|
||
public static String getToken(HttpSession session) { | ||
String token = null; | ||
|
||
synchronized (session) { | ||
token = (String) session.getAttribute(CSRF_TOKEN_FOR_SESSION_ATTR_NAME); | ||
if (null == token) { | ||
token = UUID.randomUUID().toString(); | ||
session.setAttribute(CSRF_TOKEN_FOR_SESSION_ATTR_NAME, token); | ||
} | ||
} | ||
return token; | ||
} | ||
|
||
public static String getToken(HttpServletRequest request) { | ||
String token = request.getParameter(CSRF_PARAM_NAME); | ||
if (token == null || "".equals(token)) { | ||
token = request.getHeader(CSRF_PARAM_NAME); | ||
} | ||
return token; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
lts-admin/src/main/java/com/lts/web/support/csrf/CSRFTool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.lts.web.support.csrf; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* 配置在 velocity tools 中 | ||
* | ||
* <input type="hidden" name="csrfToken" value="$csrfTool.getToken($request)"/> | ||
* | ||
* @author Robert HG ([email protected]) on 11/10/15. | ||
*/ | ||
public class CSRFTool { | ||
public static String getToken(HttpServletRequest request) { | ||
return CSRFTokenManager.getToken(request.getSession()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
lts-admin/src/main/java/com/lts/web/support/xss/XssFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.lts.web.support.xss; | ||
|
||
import javax.servlet.*; | ||
import javax.servlet.http.HttpServletRequest; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @author Robert HG ([email protected]) on 11/10/15. | ||
*/ | ||
public class XssFilter implements Filter { | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
|
||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
|
||
chain.doFilter(new XssHttpServletRequestWrapper((HttpServletRequest) request), response); | ||
|
||
} | ||
|
||
@Override | ||
public void destroy() { | ||
|
||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
lts-admin/src/main/java/com/lts/web/support/xss/XssHttpServletRequestWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.lts.web.support.xss; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletRequestWrapper; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @author Robert HG ([email protected]) on 11/10/15. | ||
*/ | ||
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper { | ||
|
||
public XssHttpServletRequestWrapper(HttpServletRequest servletRequest) { | ||
super(servletRequest); | ||
} | ||
|
||
public String[] getParameterValues(String parameter) { | ||
String[] values = super.getParameterValues(parameter); | ||
if (values == null) { | ||
return null; | ||
} | ||
int count = values.length; | ||
String[] encodedValues = new String[count]; | ||
for (int i = 0; i < count; i++) { | ||
encodedValues[i] = cleanXSS(values[i]); | ||
} | ||
return encodedValues; | ||
} | ||
|
||
public String getParameter(String parameter) { | ||
String value = super.getParameter(parameter); | ||
if (value == null) { | ||
return null; | ||
} | ||
return cleanXSS(value); | ||
} | ||
|
||
public String getHeader(String name) { | ||
String value = super.getHeader(name); | ||
if (value == null) | ||
return null; | ||
return cleanXSS(value); | ||
} | ||
|
||
private static final List<Pattern> PATTERNS = new CopyOnWriteArrayList<Pattern>(); | ||
|
||
static { | ||
PATTERNS.add(Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE)); | ||
PATTERNS.add(Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL)); | ||
PATTERNS.add(Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL)); | ||
PATTERNS.add(Pattern.compile("</script>", Pattern.CASE_INSENSITIVE)); | ||
PATTERNS.add(Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL)); | ||
PATTERNS.add(Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL)); | ||
PATTERNS.add(Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL)); | ||
PATTERNS.add(Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE)); | ||
PATTERNS.add(Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE)); | ||
PATTERNS.add(Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL)); | ||
} | ||
|
||
private String cleanXSS(String value) { | ||
if (value != null) { | ||
for (Pattern pattern : PATTERNS) { | ||
value = pattern.matcher(value).replaceAll(""); | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
lts-core/src/main/java/com/lts/core/commons/io/UnsafeByteArrayInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.lts.core.commons.io; | ||
|
||
import java.io.InputStream; | ||
|
||
/** | ||
* @author Robert HG ([email protected]) | ||
*/ | ||
public class UnsafeByteArrayInputStream extends InputStream { | ||
|
||
protected byte buf[]; | ||
|
||
protected int pos; | ||
|
||
protected int mark = 0; | ||
|
||
protected int count; | ||
|
||
public UnsafeByteArrayInputStream(byte buf[]) { | ||
this.buf = buf; | ||
this.pos = 0; | ||
this.count = buf.length; | ||
} | ||
|
||
public UnsafeByteArrayInputStream(byte buf[], int offset, int length) { | ||
this.buf = buf; | ||
this.pos = offset; | ||
this.count = Math.min(offset + length, buf.length); | ||
this.mark = offset; | ||
} | ||
|
||
public int read() { | ||
return (pos < count) ? (buf[pos++] & 0xff) : -1; | ||
} | ||
|
||
public int read(byte b[], int off, int len) { | ||
if (b == null) { | ||
throw new NullPointerException(); | ||
} else if (off < 0 || len < 0 || len > b.length - off) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
if (pos >= count) { | ||
return -1; | ||
} | ||
|
||
int avail = count - pos; | ||
if (len > avail) { | ||
len = avail; | ||
} | ||
if (len <= 0) { | ||
return 0; | ||
} | ||
System.arraycopy(buf, pos, b, off, len); | ||
pos += len; | ||
return len; | ||
} | ||
|
||
public long skip(long n) { | ||
long k = count - pos; | ||
if (n < k) { | ||
k = n < 0 ? 0 : n; | ||
} | ||
|
||
pos += k; | ||
return k; | ||
} | ||
|
||
public int available() { | ||
return count - pos; | ||
} | ||
|
||
public boolean markSupported() { | ||
return true; | ||
} | ||
|
||
public void mark(int readAheadLimit) { | ||
mark = pos; | ||
} | ||
|
||
public void reset() { | ||
pos = mark; | ||
} | ||
|
||
} |
Oops, something went wrong.