Skip to content

Commit 0efb170

Browse files
author
Dave Syer
committed
Split out indiviual stages into projects
1 parent e8d4acc commit 0efb170

File tree

36 files changed

+1650
-124
lines changed

36 files changed

+1650
-124
lines changed

README.adoc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
This project is a sample app showing how to do various things with
2+
https://tools.ietf.org/html/rfc6749[OAuth2] and
3+
http://projects.spring.io/spring-boot/[Spring Boot], starting with a
4+
simple, single-provider single-sign on, and working up to a
5+
self-hosted OAuth2 Authorization Server with a choice of social
6+
authentication providers (https://developers.facebook.com[Facebook] or
7+
https://developer.github.com/[Github]). The samples are all
8+
single-page apps using Spring Boot and Spring OAuth on the back
9+
end. They also all use https://angularjs.org/[AngularJS] on the front
10+
end, but the changes needed to convert to a different JavaScript
11+
framework or to use server side rendering would be minimal.
12+
13+
There are several samples building on each other adding new features:
14+
15+
* **simple**: a very basic static app with just a home page and
16+
unconditional login through via Spring Boot's `@EnableOAuth2Sso` (if
17+
you visit the home page you will be automatically redirected to
18+
Facebook).
19+
20+
* **click**: adds an explicit link that the user has to click to
21+
login.
22+
23+
* **logout**: adds a logout link as well for authenticated users.
24+
25+
* **manual**: shows how the `@EnableOAuth2Sso` works by unpicking it
26+
and configuring all its pieces manually.
27+
28+
* **gitub**: adds a second login provider in Github, so the user can
29+
choose on the home page which one to use.
30+
31+
* **auth-server**: turns the app into a fully-fledged OAuth2
32+
Authorization Server, able to issue its own tokens, but still using
33+
the external OAuth2 providers for authentication.
34+
35+
Each of them can be imported into an IDE and there is a main class
36+
`SocialApplication` that you can run there to start the apps. They all
37+
come up with a home page on http://localhost:8080 (and all require
38+
that you have at least a Facebook account if you want to log in and
39+
see the content). You can also run all the apps on the command line
40+
using `mvn spring-boot:run` or by building the jar file and running it
41+
with `mvn package` and `java -jar ...`. There is no need to install
42+
Maven if you use the https://github.com/takari/maven-wrapper[wrapper]
43+
at the top level, e.g.
44+
45+
```
46+
$ cd simple
47+
$ ../mvnw package
48+
$ java -jar target/*.jar
49+
```
50+
51+
NOTE: The apps all work on `localhost:8080` because they use OAuth2
52+
clients registered with Facebook and Github for that address. To run
53+
them on a different host or port, you need to register your own apps
54+
and put the credentials in the config files. There is no danger of
55+
leaking your Facebook or Github credentials beyond localhost if you
56+
use the default values, but be careful what you expose on the
57+
internet, and don't put your own app registrations in public source
58+
control.

auth-server/pom.xml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>social-auth-server</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>social-auth-server</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.0.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
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>
62+
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-test</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
</dependencies>
69+
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>
78+
79+
</project>
File renamed without changes.

click/pom.xml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>social-click</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>social-click</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.0.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
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>
62+
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-test</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
</dependencies>
69+
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>
78+
79+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example;
17+
18+
import java.security.Principal;
19+
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
23+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
24+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
25+
import org.springframework.web.bind.annotation.RequestMapping;
26+
import org.springframework.web.bind.annotation.RestController;
27+
28+
@SpringBootApplication
29+
@EnableOAuth2Sso
30+
@RestController
31+
public class SocialApplication extends WebSecurityConfigurerAdapter {
32+
33+
@RequestMapping("/user")
34+
public Principal user(Principal principal) {
35+
return principal;
36+
}
37+
38+
@Override
39+
protected void configure(HttpSecurity http) throws Exception {
40+
http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**", "/webjars/**").permitAll().anyRequest()
41+
.authenticated();
42+
}
43+
44+
public static void main(String[] args) {
45+
SpringApplication.run(SocialApplication.class, args);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)