Skip to content

Commit 4693c56

Browse files
author
Dave Syer
committed
Convert the manual sample to jQuery
1 parent abfc214 commit 4693c56

File tree

4 files changed

+119
-117
lines changed

4 files changed

+119
-117
lines changed

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
tags: [security,angular,rest,oauth]
2+
tags: [security,javascript,rest,oauth]
33
projects: [spring-security,spring-security-oauth,spring-boot]
44
---
55
:toc: left

manual/README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ HTML:
133133
[source,html]
134134
----
135135
<h1>Login</h1>
136-
<div class="container" ng-show="!home.authenticated">
136+
<div class="container unauthenticated">
137137
<div>
138138
With Facebook: <a href="/login/facebook">click here</a>
139139
</div>

manual/pom.xml

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,84 @@
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-manual</artifactId>
8-
<version>0.0.1-SNAPSHOT</version>
9-
<packaging>jar</packaging>
6+
<groupId>com.example</groupId>
7+
<artifactId>social-manual</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
1010

11-
<name>social-manual</name>
12-
<description>Demo project for Spring Boot</description>
11+
<name>social-manual</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-
<dependency>
69-
<groupId>org.springframework.boot</groupId>
70-
<artifactId>spring-boot-configuration-processor</artifactId>
71-
<optional>true</optional>
72-
</dependency>
73-
</dependencies>
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-test</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-configuration-processor</artifactId>
71+
<optional>true</optional>
72+
</dependency>
73+
</dependencies>
7474

75-
<build>
76-
<plugins>
77-
<plugin>
78-
<groupId>org.springframework.boot</groupId>
79-
<artifactId>spring-boot-maven-plugin</artifactId>
80-
</plugin>
81-
</plugins>
82-
</build>
75+
<build>
76+
<plugins>
77+
<plugin>
78+
<groupId>org.springframework.boot</groupId>
79+
<artifactId>spring-boot-maven-plugin</artifactId>
80+
</plugin>
81+
</plugins>
82+
</build>
8383

8484
</project>

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

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,52 @@
88
<meta name="viewport" content="width=device-width" />
99
<base href="/" />
1010
<link rel="stylesheet" type="text/css"
11-
href="/webjars/bootstrap/css/bootstrap.min.css" />
11+
href="/webjars/bootstrap/css/bootstrap.min.css" />
1212
<script type="text/javascript" src="/webjars/jquery/jquery.min.js"></script>
1313
<script type="text/javascript"
14-
src="/webjars/bootstrap/js/bootstrap.min.js"></script>
14+
src="/webjars/bootstrap/js/bootstrap.min.js"></script>
1515
</head>
16-
<body ng-app="app" ng-controller="home as home">
17-
<h1>Login</h1>
18-
<div class="container" ng-show="!home.authenticated">
19-
<div>
20-
With Facebook: <a href="/login/facebook">click here</a>
21-
</div>
22-
</div>
23-
<div class="container" ng-show="home.authenticated">
24-
Logged in as: <span ng-bind="home.user"></span>
25-
<div>
26-
<button ng-click="home.logout()" class="btn btn-primary">Logout</button>
27-
</div>
28-
</div>
29-
<script type="text/javascript" src="/webjars/angularjs/angular.min.js"></script>
30-
<script type="text/javascript">
31-
angular
32-
.module("app", [])
33-
.config(
34-
function($httpProvider) {
35-
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
36-
}).controller("home", function($http, $location) {
37-
var self = this;
38-
$http.get("/user").success(function(data) {
39-
self.user = data.userAuthentication.details.name;
40-
self.authenticated = true;
41-
}).error(function() {
42-
self.user = "N/A";
43-
self.authenticated = false;
44-
});
45-
self.logout = function() {
46-
$http.post('logout', {}).success(function() {
47-
self.authenticated = false;
48-
$location.path("/");
49-
}).error(function(data) {
50-
console.log("Logout failed")
51-
self.authenticated = false;
52-
});
53-
};
54-
});
55-
</script>
16+
<body>
17+
<h1>Login</h1>
18+
<div class="container unauthenticated">
19+
With Facebook: <a href="/login/facebook">click here</a>
20+
</div>
21+
<div class="container authenticated" style="display: none">
22+
Logged in as: <span id="user"></span>
23+
<div>
24+
<button onClick="logout()" class="btn btn-primary">Logout</button>
25+
</div>
26+
</div>
27+
<script type="text/javascript"
28+
src="/webjars/js-cookie/js.cookie.js"></script>
29+
<script type="text/javascript">
30+
$
31+
.ajaxSetup({
32+
beforeSend : function(xhr, settings) {
33+
if (settings.type == 'POST' || settings.type == 'PUT'
34+
|| settings.type == 'DELETE') {
35+
if (!(/^http:.*/.test(settings.url) || /^https:.*/
36+
.test(settings.url))) {
37+
// Only send the token to relative URLs i.e. locally.
38+
xhr.setRequestHeader("X-XSRF-TOKEN",
39+
Cookies.get('XSRF-TOKEN'));
40+
}
41+
}
42+
}
43+
});
44+
$.get("/user", function(data) {
45+
$("#user").html(data.userAuthentication.details.name);
46+
$(".unauthenticated").hide();
47+
$(".authenticated").show();
48+
});
49+
var logout = function() {
50+
$.post("/logout", function() {
51+
$("#user").html('');
52+
$(".unauthenticated").show();
53+
$(".authenticated").hide();
54+
})
55+
return true;
56+
}
57+
</script>
5658
</body>
5759
</html>

0 commit comments

Comments
 (0)