forked from zhouit/Zblog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhou
committed
Jun 14, 2015
1 parent
a285596
commit d05690c
Showing
9 changed files
with
157 additions
and
67 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
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
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
25 changes: 25 additions & 0 deletions
25
src/main/java/com/zblog/web/resovler/ServletRequestReaderArgumentsResolver.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,25 @@ | ||
package com.zblog.web.resovler; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.springframework.core.MethodParameter; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
import com.zblog.core.util.web.ServletRequestReader; | ||
|
||
public class ServletRequestReaderArgumentsResolver implements HandlerMethodArgumentResolver{ | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter){ | ||
return parameter.getParameterType() == ServletRequestReader.class; | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception{ | ||
return new ServletRequestReader(webRequest.getNativeRequest(HttpServletRequest.class)); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/zblog/web/resovler/XssHandlerMappingPostProcessor.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,76 @@ | ||
package com.zblog.web.resovler; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.servlet.handler.AbstractHandlerMapping; | ||
import org.springframework.web.util.HtmlUtils; | ||
import org.springframework.web.util.UrlPathHelper; | ||
|
||
import com.zblog.core.util.CollectionUtils; | ||
|
||
/** | ||
* 解决@PathVariable注解造成的xss攻击问题,注意:此类必需由WebApplicationContext初始化 | ||
* | ||
* @author zhou | ||
* | ||
*/ | ||
public class XssHandlerMappingPostProcessor implements BeanPostProcessor{ | ||
|
||
@Override | ||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException{ | ||
return bean; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException{ | ||
if(bean instanceof AbstractHandlerMapping){ | ||
AbstractHandlerMapping ahm = (AbstractHandlerMapping) bean; | ||
ahm.setUrlPathHelper(new XssUrlPathHelper()); | ||
} | ||
|
||
return bean; | ||
} | ||
|
||
static class XssUrlPathHelper extends UrlPathHelper{ | ||
|
||
@Override | ||
public Map<String, String> decodePathVariables(HttpServletRequest request, Map<String, String> vars){ | ||
Map<String, String> result = super.decodePathVariables(request, vars); | ||
if(!CollectionUtils.isEmpty(result)){ | ||
for(String key : result.keySet()){ | ||
result.put(key, cleanXSS(result.get(key))); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
public MultiValueMap<String, String> decodeMatrixVariables(HttpServletRequest request, | ||
MultiValueMap<String, String> vars){ | ||
MultiValueMap<String, String> mvm = super.decodeMatrixVariables(request, vars); | ||
if(!CollectionUtils.isEmpty(mvm)){ | ||
for(String key : mvm.keySet()){ | ||
List<String> value = mvm.get(key); | ||
for(int i = 0; i < value.size(); i++){ | ||
value.set(i, cleanXSS(value.get(i))); | ||
} | ||
} | ||
} | ||
|
||
return mvm; | ||
} | ||
|
||
private String cleanXSS(String value){ | ||
return HtmlUtils.htmlEscape(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