Skip to content

Commit abfc214

Browse files
author
Dave Syer
committed
Use js.cookie
1 parent 6d976a1 commit abfc214

File tree

4 files changed

+89
-104
lines changed

4 files changed

+89
-104
lines changed

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ self-hosted OAuth2 Authorization Server with a choice of
2020
authentication providers (https://developers.facebook.com[Facebook] or
2121
https://developer.github.com/[Github]). The samples are all
2222
single-page apps using Spring Boot and Spring OAuth on the back
23-
end. They also all use https://angularjs.org/[AngularJS] on the front
23+
end. They also all use plain https://jquery.org/[jQuery] on the front
2424
end, but the changes needed to convert to a different JavaScript
2525
framework or to use server side rendering would be minimal.
2626

logout/README.adoc

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,35 +95,34 @@ protected void configure(HttpSecurity http) throws Exception {
9595
== Adding the CSRF Token in the Client
9696

9797
Since we are not using a higher level framework in this sample, we
98-
need to explicitly add the CSRF token, which is available as a cookie. The code is simple, if a bit long winded:
98+
need to explicitly add the CSRF token, which we made available as a
99+
cookie from the backend. To make the code a bit simpler, we include an
100+
additional library:
101+
102+
.pom.xml
103+
[source,xml]
104+
----
105+
<dependency>
106+
<groupId>org.webjars</groupId>
107+
<artifactId>js-cookie</artifactId>
108+
<version>2.1.0</version>
109+
</dependency>
110+
----
111+
112+
then we can use `Cookies` convenience methods in xhr:
99113

100114
.index.html
115+
[source,html]
101116
----
102117
$.ajaxSetup({
103118
beforeSend : function(xhr, settings) {
104119
if (settings.type == 'POST' || settings.type == 'PUT'
105120
|| settings.type == 'DELETE') {
106-
function getCookie(name) {
107-
var cookieValue = null;
108-
if (document.cookie && document.cookie != '') {
109-
var cookies = document.cookie.split(';');
110-
for (var i = 0; i < cookies.length; i++) {
111-
var cookie = jQuery.trim(cookies[i]);
112-
// Does this cookie string begin with the name we want?
113-
if (cookie.substring(0, name.length + 1) == (name + '=')) {
114-
cookieValue = decodeURIComponent(cookie
115-
.substring(name.length + 1));
116-
break;
117-
}
118-
}
119-
}
120-
return cookieValue;
121-
}
122121
if (!(/^http:.*/.test(settings.url) || /^https:.*/
123122
.test(settings.url))) {
124123
// Only send the token to relative URLs i.e. locally.
125124
xhr.setRequestHeader("X-XSRF-TOKEN",
126-
getCookie('XSRF-TOKEN'));
125+
Cookies.get('XSRF-TOKEN'));
127126
}
128127
}
129128
}

logout/pom.xml

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,79 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>com.example</groupId>
7-
<artifactId>social-logout</artifactId>
8-
<version>0.0.1-SNAPSHOT</version>
9-
<packaging>jar</packaging>
6+
<groupId>com.example</groupId>
7+
<artifactId>social-logout</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
1010

11-
<name>social-logout</name>
12-
<description>Demo project for Spring Boot</description>
11+
<name>social-logout</name>
12+
<description>Demo project for Spring Boot</description>
1313

14-
<parent>
15-
<groupId>org.springframework.boot</groupId>
16-
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>1.5.2.RELEASE</version>
18-
<relativePath /> <!-- lookup parent from repository -->
19-
</parent>
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.2.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
2020

21-
<properties>
22-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23-
<java.version>1.8</java.version>
24-
</properties>
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
2525

26-
<dependencies>
27-
<dependency>
28-
<groupId>org.springframework.boot</groupId>
29-
<artifactId>spring-boot-starter-actuator</artifactId>
30-
</dependency>
31-
<dependency>
32-
<groupId>org.springframework.boot</groupId>
33-
<artifactId>spring-boot-starter-security</artifactId>
34-
</dependency>
35-
<dependency>
36-
<groupId>org.springframework.boot</groupId>
37-
<artifactId>spring-boot-starter-web</artifactId>
38-
</dependency>
39-
<dependency>
40-
<groupId>org.springframework.security.oauth</groupId>
41-
<artifactId>spring-security-oauth2</artifactId>
42-
</dependency>
43-
<dependency>
44-
<groupId>org.webjars</groupId>
45-
<artifactId>angularjs</artifactId>
46-
<version>1.4.3</version>
47-
</dependency>
48-
<dependency>
49-
<groupId>org.webjars</groupId>
50-
<artifactId>jquery</artifactId>
51-
<version>2.1.1</version>
52-
</dependency>
53-
<dependency>
54-
<groupId>org.webjars</groupId>
55-
<artifactId>bootstrap</artifactId>
56-
<version>3.2.0</version>
57-
</dependency>
58-
<dependency>
59-
<groupId>org.webjars</groupId>
60-
<artifactId>webjars-locator</artifactId>
61-
</dependency>
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-actuator</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-security</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-web</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.security.oauth</groupId>
41+
<artifactId>spring-security-oauth2</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.webjars</groupId>
45+
<artifactId>js-cookie</artifactId>
46+
<version>2.1.0</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.webjars</groupId>
50+
<artifactId>jquery</artifactId>
51+
<version>2.1.1</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.webjars</groupId>
55+
<artifactId>bootstrap</artifactId>
56+
<version>3.2.0</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.webjars</groupId>
60+
<artifactId>webjars-locator</artifactId>
61+
</dependency>
6262

63-
<dependency>
64-
<groupId>org.springframework.boot</groupId>
65-
<artifactId>spring-boot-starter-test</artifactId>
66-
<scope>test</scope>
67-
</dependency>
68-
</dependencies>
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-test</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
</dependencies>
6969

70-
<build>
71-
<plugins>
72-
<plugin>
73-
<groupId>org.springframework.boot</groupId>
74-
<artifactId>spring-boot-maven-plugin</artifactId>
75-
</plugin>
76-
</plugins>
77-
</build>
70+
<build>
71+
<plugins>
72+
<plugin>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-maven-plugin</artifactId>
75+
</plugin>
76+
</plugins>
77+
</build>
7878

7979
</project>

logout/src/main/resources/static/index.html

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,19 @@ <h1>Login</h1>
2424
<button onClick="logout()" class="btn btn-primary">Logout</button>
2525
</div>
2626
</div>
27+
<script type="text/javascript"
28+
src="/webjars/js-cookie/js.cookie.js"></script>
2729
<script type="text/javascript">
2830
$
2931
.ajaxSetup({
3032
beforeSend : function(xhr, settings) {
3133
if (settings.type == 'POST' || settings.type == 'PUT'
3234
|| settings.type == 'DELETE') {
33-
function getCookie(name) {
34-
var cookieValue = null;
35-
if (document.cookie && document.cookie != '') {
36-
var cookies = document.cookie.split(';');
37-
for (var i = 0; i < cookies.length; i++) {
38-
var cookie = jQuery.trim(cookies[i]);
39-
// Does this cookie string begin with the name we want?
40-
if (cookie.substring(0, name.length + 1) == (name + '=')) {
41-
cookieValue = decodeURIComponent(cookie
42-
.substring(name.length + 1));
43-
break;
44-
}
45-
}
46-
}
47-
return cookieValue;
48-
}
4935
if (!(/^http:.*/.test(settings.url) || /^https:.*/
5036
.test(settings.url))) {
5137
// Only send the token to relative URLs i.e. locally.
5238
xhr.setRequestHeader("X-XSRF-TOKEN",
53-
getCookie('XSRF-TOKEN'));
39+
Cookies.get('XSRF-TOKEN'));
5440
}
5541
}
5642
}

0 commit comments

Comments
 (0)