-
Notifications
You must be signed in to change notification settings - Fork 649
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
Showing
8 changed files
with
136 additions
and
8 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
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()); | ||
} | ||
} |
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,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."; | ||
} | ||
} |
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 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"; | ||
} | ||
|
||
} |
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,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> |