From 4d8a5b254c33c6565eaddc3f5f90a4e6d4c4aaeb Mon Sep 17 00:00:00 2001
From: YunaiV <>
Date: Wed, 24 Jun 2020 12:13:05 +0800
Subject: [PATCH] =?UTF-8?q?=E7=A0=94=E7=A9=B6=E4=B8=8B=20OAuth2=20?=
=?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=94=B9=E9=80=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../pom.xml | 62 +++++++++++++++++++
.../ResourceServerApplication.java | 13 ++++
.../OAuth2AuthorizationServerConfig.java | 37 +++++++++++
.../config/OAuth2ResourceServerConfig.java | 16 +++++
.../config/SecurityConfig.java | 48 ++++++++++++++
.../controller/ExampleController.java | 0
.../ResourceServerApplication.java | 4 --
.../config/OAuth2ResourceServer.java | 26 --------
.../pom.xml | 18 ++++--
.../ResourceServerApplication.java | 13 ++++
.../config/OAuth2ResourceServerConfig.java | 28 +++++++++
.../config/SecurityConfig.java | 18 ++++++
.../controller/ExampleController.java | 18 ++++++
.../src/main/resources/application.yml | 10 +++
lab-68/pom.xml | 3 +-
15 files changed, 277 insertions(+), 37 deletions(-)
create mode 100644 lab-68/lab-68-demo01-resource-owner-password-credentials-server/pom.xml
create mode 100644 lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java
create mode 100644 lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2AuthorizationServerConfig.java
create mode 100644 lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServerConfig.java
create mode 100644 lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/SecurityConfig.java
rename lab-68/{lab-68-demo01-resource-server => lab-68-demo01-resource-owner-password-credentials-server}/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/controller/ExampleController.java (100%)
delete mode 100644 lab-68/lab-68-demo01-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java
delete mode 100644 lab-68/lab-68-demo01-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServer.java
rename lab-68/{lab-68-demo01-resource-server => lab-68-demo02-resource-server}/pom.xml (75%)
create mode 100644 lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java
create mode 100644 lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServerConfig.java
create mode 100644 lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/SecurityConfig.java
create mode 100644 lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/controller/ExampleController.java
create mode 100644 lab-68/lab-68-demo02-resource-server/src/main/resources/application.yml
diff --git a/lab-68/lab-68-demo01-resource-owner-password-credentials-server/pom.xml b/lab-68/lab-68-demo01-resource-owner-password-credentials-server/pom.xml
new file mode 100644
index 00000000..7342978f
--- /dev/null
+++ b/lab-68/lab-68-demo01-resource-owner-password-credentials-server/pom.xml
@@ -0,0 +1,62 @@
+
+
+
+ lab-68
+ cn.iocoder.springboot.labs
+ 1.0-SNAPSHOT
+
+ 4.0.0
+
+ lab-68-demo01-resource-owner-password-credentials-server
+
+
+
+ 2.2.4.RELEASE
+
+ 1.8
+ 1.8
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ ${spring.boot.version}
+ pom
+ import
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-security
+
+
+
+
+
+
+
+
+
+
+
+ org.springframework.security.oauth.boot
+ spring-security-oauth2-autoconfigure
+ ${spring.boot.version}
+
+
+
+
diff --git a/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java b/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java
new file mode 100644
index 00000000..630affa7
--- /dev/null
+++ b/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java
@@ -0,0 +1,13 @@
+package cn.iocoder.springboot.lab68.resourceserverdemo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class ResourceServerApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(ResourceServerApplication.class, args);
+ }
+
+}
diff --git a/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2AuthorizationServerConfig.java b/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2AuthorizationServerConfig.java
new file mode 100644
index 00000000..cfe85fc6
--- /dev/null
+++ b/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2AuthorizationServerConfig.java
@@ -0,0 +1,37 @@
+package cn.iocoder.springboot.lab68.resourceserverdemo.config;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.authentication.AuthenticationManager;
+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;
+import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
+
+/**
+ * 授权服务器配置
+ */
+@Configuration
+@EnableAuthorizationServer
+public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
+
+ // 用户认证
+ @Autowired
+ private AuthenticationManager authenticationManager;
+
+ @Override
+ public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
+ endpoints.authenticationManager(authenticationManager);
+ }
+
+ @Override
+ public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
+ clients.inMemory()
+ .withClient("clientapp").secret("112233") // Client 账号、密码。
+ .authorizedGrantTypes("password") // 密码模式
+ .scopes("read_userinfo", "read_contacts") // 可授权的 Scope
+// .and().withClient() // 可以继续配置新的 Client
+ ;
+ }
+
+}
diff --git a/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServerConfig.java b/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServerConfig.java
new file mode 100644
index 00000000..349400c1
--- /dev/null
+++ b/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServerConfig.java
@@ -0,0 +1,16 @@
+package cn.iocoder.springboot.lab68.resourceserverdemo.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
+
+/**
+ * 资源服务器配置
+ */
+@Configuration
+@EnableResourceServer
+public class OAuth2ResourceServerConfig {
+
+}
+
+// 实际,OAuth2ResourceServer 不是和 OAuth2AuthorizationServer 一起。
+// 主要考虑,简化 demo ,所以改成这样。
diff --git a/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/SecurityConfig.java b/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/SecurityConfig.java
new file mode 100644
index 00000000..6cfbe591
--- /dev/null
+++ b/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/SecurityConfig.java
@@ -0,0 +1,48 @@
+package cn.iocoder.springboot.lab68.resourceserverdemo.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.config.BeanIds;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.crypto.password.NoOpPasswordEncoder;
+
+@Configuration
+@EnableWebSecurity
+public class SecurityConfig extends WebSecurityConfigurerAdapter {
+
+ @Override
+ @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
+ public AuthenticationManager authenticationManagerBean() throws Exception {
+ return super.authenticationManagerBean();
+ }
+
+ @Bean
+ public static NoOpPasswordEncoder passwordEncoder() {
+ return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
+ }
+
+ @Override
+ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
+ auth.
+ // 使用内存中的 InMemoryUserDetailsManager
+ inMemoryAuthentication()
+ // 不使用 PasswordEncoder 密码编码器
+ .passwordEncoder(passwordEncoder())
+ // 配置 yunai 用户
+ .withUser("yunai").password("1024").roles("USER");
+ }
+
+ @Override
+ protected void configure(HttpSecurity http) throws Exception {
+ http.authorizeRequests()
+ .antMatchers("/oauth2/keys").permitAll()
+ // 对所有 URL 都进行认证
+ .anyRequest()
+ .authenticated();
+ }
+
+}
diff --git a/lab-68/lab-68-demo01-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/controller/ExampleController.java b/lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/controller/ExampleController.java
similarity index 100%
rename from lab-68/lab-68-demo01-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/controller/ExampleController.java
rename to lab-68/lab-68-demo01-resource-owner-password-credentials-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/controller/ExampleController.java
diff --git a/lab-68/lab-68-demo01-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java b/lab-68/lab-68-demo01-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java
deleted file mode 100644
index cdf4a6f2..00000000
--- a/lab-68/lab-68-demo01-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java
+++ /dev/null
@@ -1,4 +0,0 @@
-package cn.iocoder.springboot.lab68.resourceserverdemo;
-
-public class ResourceServerApplication {
-}
diff --git a/lab-68/lab-68-demo01-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServer.java b/lab-68/lab-68-demo01-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServer.java
deleted file mode 100644
index 4b4d15ec..00000000
--- a/lab-68/lab-68-demo01-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServer.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package cn.iocoder.springboot.lab68.resourceserverdemo.config;
-
-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()
- // 对 "/api/**" 开启认证
- .anyRequest()
- .authenticated()
- .and()
- .requestMatchers()
- .antMatchers("/api/**");
- }
-
-}
diff --git a/lab-68/lab-68-demo01-resource-server/pom.xml b/lab-68/lab-68-demo02-resource-server/pom.xml
similarity index 75%
rename from lab-68/lab-68-demo01-resource-server/pom.xml
rename to lab-68/lab-68-demo02-resource-server/pom.xml
index caa0680d..97319742 100644
--- a/lab-68/lab-68-demo01-resource-server/pom.xml
+++ b/lab-68/lab-68-demo02-resource-server/pom.xml
@@ -9,7 +9,7 @@
4.0.0
- lab-68-demo01-resource-server
+ lab-68-demo02-resource-server
@@ -45,12 +45,18 @@
-
- org.springframework.security.oauth
- spring-security-oauth2
- 2.5.0.RELEASE
-
+
+
+
+
+
+
+
+ org.springframework.security.oauth.boot
+ spring-security-oauth2-autoconfigure
+ ${spring.boot.version}
+
diff --git a/lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java b/lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java
new file mode 100644
index 00000000..630affa7
--- /dev/null
+++ b/lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/ResourceServerApplication.java
@@ -0,0 +1,13 @@
+package cn.iocoder.springboot.lab68.resourceserverdemo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class ResourceServerApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(ResourceServerApplication.class, args);
+ }
+
+}
diff --git a/lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServerConfig.java b/lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServerConfig.java
new file mode 100644
index 00000000..19ec8709
--- /dev/null
+++ b/lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/OAuth2ResourceServerConfig.java
@@ -0,0 +1,28 @@
+package cn.iocoder.springboot.lab68.resourceserverdemo.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
+import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
+
+/**
+ * 资源服务器配置
+ */
+@Configuration
+@EnableResourceServer
+public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {
+
+// @Override
+// public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
+// resources.
+// super.configure(resources);
+// }
+//
+// @Override
+// public void configure(HttpSecurity http) throws Exception {
+// super.configure(http);
+// }
+
+}
+
+// 实际,OAuth2ResourceServer 不是和 OAuth2AuthorizationServer 一起。
+// 主要考虑,简化 demo ,所以改成这样。
diff --git a/lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/SecurityConfig.java b/lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/SecurityConfig.java
new file mode 100644
index 00000000..655acb24
--- /dev/null
+++ b/lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/config/SecurityConfig.java
@@ -0,0 +1,18 @@
+package cn.iocoder.springboot.lab68.resourceserverdemo.config;
+
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+
+//@Configuration
+//@EnableWebSecurity
+public class SecurityConfig extends WebSecurityConfigurerAdapter {
+
+ @Override
+ protected void configure(HttpSecurity http) throws Exception {
+ http.authorizeRequests()
+ // 对所有 URL 都进行认证
+ .anyRequest()
+ .authenticated();
+ }
+
+}
diff --git a/lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/controller/ExampleController.java b/lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/controller/ExampleController.java
new file mode 100644
index 00000000..2114a466
--- /dev/null
+++ b/lab-68/lab-68-demo02-resource-server/src/main/java/cn/iocoder/springboot/lab68/resourceserverdemo/controller/ExampleController.java
@@ -0,0 +1,18 @@
+package cn.iocoder.springboot.lab68.resourceserverdemo.controller;
+
+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";
+ }
+
+}
diff --git a/lab-68/lab-68-demo02-resource-server/src/main/resources/application.yml b/lab-68/lab-68-demo02-resource-server/src/main/resources/application.yml
new file mode 100644
index 00000000..c8226475
--- /dev/null
+++ b/lab-68/lab-68-demo02-resource-server/src/main/resources/application.yml
@@ -0,0 +1,10 @@
+server:
+ port: 9090
+
+security:
+ oauth2:
+ client:
+ client-id: clientapp
+ client-secret: 112233
+ resource:
+ token-info-uri:
diff --git a/lab-68/pom.xml b/lab-68/pom.xml
index 3b86700d..c8a699e2 100644
--- a/lab-68/pom.xml
+++ b/lab-68/pom.xml
@@ -12,7 +12,8 @@
lab-68
pom
- lab-68-demo01-resource-server
+ lab-68-demo01-resource-owner-password-credentials-server
+ lab-68-demo02-resource-server