Skip to content

Commit 86d2551

Browse files
committed
diy csrf error code
1 parent 9bed870 commit 86d2551

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.joychou;
2+
3+
4+
import org.springframework.http.MediaType;
5+
import org.springframework.security.access.AccessDeniedException;
6+
import org.springframework.security.web.access.AccessDeniedHandler;
7+
8+
9+
import javax.servlet.ServletException;
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpServletResponse;
12+
import java.io.IOException;
13+
14+
public class CsrfAccessDeniedHandler implements AccessDeniedHandler {
15+
16+
17+
@Override
18+
public void handle(HttpServletRequest request, HttpServletResponse response,
19+
AccessDeniedException accessDeniedException) throws IOException, ServletException {
20+
response.setContentType(MediaType.TEXT_HTML_VALUE);
21+
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
22+
response.getWriter().write("CSRF check failed by JoyChou.");
23+
}
24+
25+
}
26+

src/main/java/org/joychou/WebSecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
77
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
88
import org.springframework.security.web.util.matcher.RequestMatcher;
9-
109
import javax.servlet.http.HttpServletRequest;
1110
import java.util.Arrays;
1211
import java.util.HashSet;
@@ -38,6 +37,7 @@ protected void configure(HttpSecurity http) throws Exception {
3837
.requireCsrfProtectionMatcher(csrfRequestMatcher)
3938
.ignoringAntMatchers("/xxe/**", "/fastjon/**") // 不进行csrf校验的uri,多个uri使用逗号分隔
4039
.csrfTokenRepository(new CookieCsrfTokenRepository());
40+
http.exceptionHandling().accessDeniedHandler(new CsrfAccessDeniedHandler());
4141
// http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
4242
}
4343
}

src/main/java/org/joychou/controller/URLWhiteList.java

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ public String urlVul(HttpServletRequest request) throws Exception{
115115
}
116116
}
117117

118-
119-
// 安全代码
118+
// 利用InternetDomainName库获取一级域名的安全代码 (一级域名白名单)
120119
@RequestMapping("/seccode")
121120
@ResponseBody
122121
public String seccode(HttpServletRequest request) throws Exception{
@@ -141,4 +140,73 @@ public String seccode(HttpServletRequest request) throws Exception{
141140
return "URL is illegal";
142141
}
143142
}
143+
144+
/**
145+
* @desc 自定义一级域名白名单
146+
* @usage http://localhost:8080/url/seccode1?url=http://aa.taobao.com
147+
* @param request
148+
* @return
149+
* @throws Exception
150+
*/
151+
@RequestMapping("/seccode1")
152+
@ResponseBody
153+
public String seccode1(HttpServletRequest request) throws Exception{
154+
155+
// 定义一级域名白名单list,用endsWith加上.判断
156+
String whiteDomainlists[] = {"taobao.com", "tmall.com"};
157+
158+
String url = request.getParameter("url");
159+
System.out.println("url: " + url);
160+
URI uri = new URI(url);
161+
URL u = new URL(url);
162+
// 判断是否是http(s)协议
163+
if (!u.getProtocol().startsWith("http") && !u.getProtocol().startsWith("https")) {
164+
return "URL is not http or https";
165+
}
166+
// 使用uri获取host
167+
String host = uri.getHost().toLowerCase();
168+
System.out.println("host: " + host);
169+
170+
for (String domain: whiteDomainlists){
171+
if (host.endsWith("." + domain)) {
172+
return "good url";
173+
}
174+
}
175+
176+
return "bad url";
177+
}
178+
179+
/**
180+
* @desc 自定义多级域名白名单
181+
* @usage http://localhost:8080/url/seccode2?url=http://ccc.bbb.taobao.com
182+
* @param request
183+
* @return
184+
* @throws Exception
185+
*/
186+
@RequestMapping("/seccode2")
187+
@ResponseBody
188+
public String seccode2(HttpServletRequest request) throws Exception{
189+
190+
// 定义多级域名白名单,判断使用equals
191+
String whiteDomainlists[] = {"aaa.taobao.com", "ccc.bbb.taobao.com"};
192+
193+
String url = request.getParameter("url");
194+
System.out.println("url: " + url);
195+
URI uri = new URI(url);
196+
URL u = new URL(url);
197+
// 判断是否是http(s)协议
198+
if (!u.getProtocol().startsWith("http") && !u.getProtocol().startsWith("https")) {
199+
return "URL is not http or https";
200+
}
201+
// 使用uri获取host
202+
String host = uri.getHost().toLowerCase();
203+
System.out.println("host: " + host);
204+
205+
for (String domain: whiteDomainlists){
206+
if (host.equals(domain)) {
207+
return "good url";
208+
}
209+
}
210+
return "bad url";
211+
}
144212
}

0 commit comments

Comments
 (0)