forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加 Spring Security OAuth2 的 authorzation-code
- Loading branch information
YunaiV
committed
Sep 12, 2018
0 parents
commit 6ecc3a6
Showing
14 changed files
with
261 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
### Java template | ||
# Compiled class file | ||
*.class | ||
|
||
# Log file | ||
*.log | ||
|
||
# BlueJ files | ||
*.ctxt | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.nar | ||
*.ear | ||
*.zip | ||
*.tar.gz | ||
*.rar | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
|
||
SpringBoot-Labs.ipr | ||
SpringBoot-Labs.iws |
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 @@ | ||
艿艿 SpringBoot Lab 实验室。 |
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 @@ | ||
jwt 的集成 |
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,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>labs-parent</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-01</artifactId> | ||
|
||
|
||
</project> |
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 @@ | ||
oauth2 的集成 |
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,51 @@ | ||
> 直接饮用 https://github.com/geektime-geekbang/oauth2lab/blob/0e0f380a670718225fcfc86c60ca4cf7cc5c24d4/lab01/authcode-server/README.md | ||
> 作者:杨波 | ||
基于授权码模式+Spring Security OAuth2的最简授权服务器 | ||
====== | ||
|
||
# 操作方式 | ||
|
||
## 1. 获取授权码 | ||
|
||
浏览器请求: | ||
|
||
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=code&scope=read_userinfo | ||
|
||
**注意:state参数暂忽略** | ||
|
||
响应案例: | ||
|
||
http://localhost:9001/callback?code=8uYpdo | ||
|
||
## 2. 获取访问令牌 | ||
|
||
curl -X POST --user clientapp:123456 http://localhost:8080/oauth/token -H | ||
"content-type: application/x-www-form-urlencoded" -d | ||
"code=8uYpdo&grant_type=authorization_code&redirect_uri=http%3A%2F%2Flocalh | ||
ost%3A9001%2Fcallback&scope=read_userinfo" | ||
|
||
案例响应: | ||
|
||
```json | ||
{ | ||
"access_token": "36cded80-b6f5-43b7-bdfc-594788a24530", | ||
"token_type": "bearer", | ||
"expires_in": 43199, | ||
"scope": "read_userinfo" | ||
} | ||
``` | ||
|
||
|
||
## 3. 调用API | ||
|
||
curl -X GET http://localhost:8080/api/userinfo -H "authorization: Bearer 36cded80-b6f5-43b7-bdfc-594788a24530" | ||
|
||
案例响应: | ||
|
||
```json | ||
{ | ||
"name": "bobo", | ||
"email": "[email protected]" | ||
} | ||
``` |
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,44 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.5.16.RELEASE</version> | ||
<relativePath /> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>authorization-code-server</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-security</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- for OAuth 2.0 --> | ||
<dependency> | ||
<groupId>org.springframework.security.oauth</groupId> | ||
<artifactId>spring-security-oauth2</artifactId> | ||
</dependency> | ||
|
||
<!-- for test --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...authorization-code-server/src/main/java/cn/iocoder/springboot/labs/lab01/Application.java
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,13 @@ | ||
package cn.iocoder.springboot.labs.lab01; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...c/main/java/cn/iocoder/springboot/labs/lab01/authorization/OAuth2AuthorizationServer.java
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,24 @@ | ||
package cn.iocoder.springboot.labs.lab01.authorization; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; | ||
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; | ||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; | ||
|
||
// 授权服务器配置 | ||
@Configuration | ||
@EnableAuthorizationServer | ||
public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter { | ||
|
||
@Override | ||
public void configure(ClientDetailsServiceConfigurer clients) throws Exception { | ||
clients.inMemory() | ||
.withClient("clientapp") | ||
// .secret("112233") // 目前非必须,因为开启的是 authorization_code 模式 | ||
.redirectUris("http://localhost:9001/callback") | ||
// 授权码模式 | ||
.authorizedGrantTypes("authorization_code") | ||
.scopes("read_userinfo", "read_contacts"); // TODO 芋艿,后续优化 | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...-server/src/main/java/cn/iocoder/springboot/labs/lab01/resource/OAuth2ResourceServer.java
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,26 @@ | ||
package cn.iocoder.springboot.labs.lab01.resource; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; | ||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; | ||
|
||
// 资源服务配置 | ||
@Configuration | ||
@EnableResourceServer | ||
public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter { | ||
|
||
@Override | ||
public void configure(HttpSecurity http) throws Exception { | ||
http.authorizeRequests() | ||
.anyRequest() | ||
.authenticated() | ||
.and() | ||
.requestMatchers() | ||
.antMatchers("/api/**"); | ||
} | ||
|
||
} | ||
|
||
// 实际,OAuth2ResourceServer 不是和 OAuth2AuthorizationServer 一起。 | ||
// 主要考虑,简化 demo ,所以改成这样。 |
18 changes: 18 additions & 0 deletions
18
...server/src/main/java/cn/iocoder/springboot/labs/lab01/resource/api/ExampleController.java
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,18 @@ | ||
package cn.iocoder.springboot.labs.lab01.resource.api; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* 示例模块 Controller | ||
*/ | ||
@RestController | ||
@RequestMapping("/api/example") | ||
public class ExampleController { | ||
|
||
@RequestMapping("/hello") | ||
public String hello() { | ||
return "world"; | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
lab-02/authorization-code-server/src/main/resources/application.properties
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,3 @@ | ||
# Spring Security Setting | ||
security.user.name=yunai | ||
security.user.password=1024 |
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,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>labs-parent</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-02</artifactId> | ||
<packaging>pom</packaging> | ||
<modules> | ||
<module>authorization-code-server</module> | ||
</modules> | ||
|
||
|
||
</project> |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<artifactId>labs-parent</artifactId> | ||
<packaging>pom</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<modules> | ||
<module>lab-01</module> | ||
<module>lab-02</module> | ||
</modules> | ||
|
||
|
||
</project> |