Last active
November 18, 2016 18:59
-
-
Save geapi/a0b1e81339667e4be7be11256ecdcaf4 to your computer and use it in GitHub Desktop.
added csrf tokens to the TestAuthentication
This file contains hidden or 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
package tips.cloudnative.testauthentication; | |
import org.apache.commons.codec.binary.Base64; | |
import org.springframework.http.HttpHeaders; | |
public class TestAuthentication { | |
@Autowired | |
private CsrfTokenRepository csrfTokenRepository; | |
public HttpHeaders basicAuthHeaders() { | |
String plainCreds = "user:password"; | |
byte[] plainCredsBytes = plainCreds.getBytes(); | |
byte[] base64CredsBytes = Base64.getEncoder().encode(plainCredsBytes); | |
String base64Creds = new String(base64CredsBytes); | |
HttpHeaders headers = new HttpHeaders(); | |
headers.add("Authorization", "Basic " + base64Creds); | |
return headers; | |
} | |
public HttpHeaders csrfHeaders() { | |
CsrfToken csrfToken = csrfTokenRepository.generateToken(null); | |
HttpHeaders headers = basicAuthHeaders(); | |
headers.add(csrfToken.getHeaderName(), csrfToken.getToken()); | |
headers.add("Cookie", "XSRF-TOKEN=" + csrfToken.getToken()); | |
return headers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment