Skip to content

Commit

Permalink
add csrf
Browse files Browse the repository at this point in the history
  • Loading branch information
JoyChou93 committed May 31, 2019
1 parent 5b60e15 commit 590891b
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [JSONP](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/JSONP.java)
- [SPEL](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/SPEL.java)
- [Actuators to RCE](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/resources/logback.xml)
- [CSRF](https://github.com/JoyChou93/java-sec-code/blob/master/src/main/java/org/joychou/controller/CSRF.java)

## 漏洞说明

Expand All @@ -36,6 +37,7 @@
- [CSRF](https://github.com/JoyChou93/java-sec-code/wiki/CSRF)
- [JSONP](https://github.com/JoyChou93/java-sec-code/wiki/JSONP)
- [Actuators to RCE](https://github.com/JoyChou93/java-sec-code/wiki/Actuators-to-RCE)
- [CSRF](https://github.com/JoyChou93/java-sec-code/wiki/CSRF)
- [Others](https://github.com/JoyChou93/java-sec-code/wiki/others)


Expand Down
18 changes: 11 additions & 7 deletions java-sec-code.iml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand Down Expand Up @@ -41,7 +41,6 @@
<orderEntry type="library" name="Maven: org.slf4j:jcl-over-slf4j:1.7.22" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.22" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:log4j-over-slf4j:1.7.22" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.3.6.RELEASE" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.17" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:1.5.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:8.5.11" level="project" />
Expand All @@ -55,11 +54,7 @@
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-thymeleaf:1.5.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.thymeleaf:thymeleaf-spring4:2.1.5.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.thymeleaf:thymeleaf:2.1.5.RELEASE" level="project" />
Expand Down Expand Up @@ -125,7 +120,6 @@
<orderEntry type="library" scope="RUNTIME" name="Maven: com.sun.jersey.contribs:jersey-apache-client4:1.19.1" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: com.google.inject:guice:4.0" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: javax.inject:javax.inject:1" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: aopalliance:aopalliance:1.0" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: com.netflix.governator:governator-api:1.12.10" level="project" />
<orderEntry type="library" name="Maven: com.netflix.eureka:eureka-core:1.4.11" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: com.netflix.governator:governator:1.12.10" level="project" />
Expand Down Expand Up @@ -165,5 +159,15 @@
<orderEntry type="library" name="Maven: xmlpull:xmlpull:1.1.3.1" level="project" />
<orderEntry type="library" name="Maven: xpp3:xpp3_min:1.1.4c" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.uuid:java-uuid-generator:3.1.4" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-web:4.2.12.RELEASE" level="project" />
<orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-core:4.2.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-config:4.2.12.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-security:2.1.5.RELEASE" level="project" />
</component>
</module>
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@
<version>3.1.4</version>
</dependency>

<!-- 5.x的spring-security版本不适配springboot 1.5,因为1.5的springboot的spring-core版本是4.x,所以spring-security改为4.x即可适配。 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.2.12.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.2.12.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>

</dependencies>

<dependencyManagement>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/joychou/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@SpringBootApplication
@EnableEurekaClient
// @EnableEurekaClient // 测试Eureka请打开注释,防止控制台一直有warning
public class Application extends SpringBootServletInitializer {

@Override
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/joychou/WebSecurityConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.joychou;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
// http.csrf().disable() // 去掉csrf校验
// 默认token存在session里,现在改为token存在cookie里。但存在后端多台服务器情况,session不能同步的问题,所以一般使用cookie模式。
http.csrf().csrfTokenRepository(new CookieCsrfTokenRepository());
// http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
}
}
31 changes: 31 additions & 0 deletions src/main/java/org/joychou/controller/CSRF.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.joychou.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author: JoyChou (joychou@joychou.org)
* @date: 2019.05.31
* @desc: check csrf using spring-security
* @using: access http://localhost:8080/csrf/ -> click submit
*/


@Controller
@RequestMapping("/csrf")
public class CSRF {

@GetMapping("/")
public String index() {
return "csrfTest";
}

@PostMapping("/post")
@ResponseBody
public String post() {
return "CSRF passed.";
}
}
25 changes: 25 additions & 0 deletions src/main/java/org/joychou/controller/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.joychou.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/test")
public class Test {

@RequestMapping(value = "/")
@ResponseBody
private String Index(HttpServletResponse response) {

Cookie cookie = new Cookie("XSRF-TOKEN", "123");
cookie.setDomain("taobao.com");
cookie.setMaxAge(-1); // forever time
response.addCookie(cookie);
return "success";
}

}
27 changes: 27 additions & 0 deletions src/main/resources/templates/csrfTest.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

<html xmlns:th="http://www.thymeleaf.org" lang="en">

<body>


<div>
<form name="f" th:action="@{/csrf/post}" method="post">
<input type="text" name="input" />
<input type="submit" value="Submit" />
</form>
</div>


</body>


<!-- <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" /> -->

<!--<script>-->
<!--window.csrfToken = {-->
<!--tokenName: "${_csrf.parameterName}",-->
<!--tokenValue: "${_csrf.token}"-->
<!--};-->
<!--</script>-->

</html>

0 comments on commit 590891b

Please sign in to comment.