Skip to content

Commit 7f10bb1

Browse files
author
YunaiV
committed
增加 OAuth2 客户端模式
1 parent a892944 commit 7f10bb1

File tree

20 files changed

+151
-50
lines changed

20 files changed

+151
-50
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-68</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-68-demo01-client-credentials-server</artifactId>
13+
14+
<properties>
15+
<!-- 依赖相关配置 -->
16+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
17+
<!-- 插件相关配置 -->
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
</properties>
21+
22+
<dependencyManagement>
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-parent</artifactId>
27+
<version>${spring.boot.version}</version>
28+
<type>pom</type>
29+
<scope>import</scope>
30+
</dependency>
31+
</dependencies>
32+
</dependencyManagement>
33+
34+
<dependencies>
35+
<!-- 实现对 Spring MVC 的自动配置 -->
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
41+
<!-- 实现对 Spring Security 的自动配置 -->
42+
<!-- <dependency>-->
43+
<!-- <groupId>org.springframework.boot</groupId>-->
44+
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
45+
<!-- </dependency>-->
46+
47+
<!-- 实现对 Spring Security OAuth2 的自动配置 -->
48+
<dependency>
49+
<groupId>org.springframework.security.oauth.boot</groupId>
50+
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
51+
<version>${spring.boot.version}</version>
52+
</dependency>
53+
</dependencies>
54+
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cn.iocoder.springboot.lab68.resourceserverdemo.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
6+
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
7+
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
8+
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
9+
10+
/**
11+
* 授权服务器配置
12+
*/
13+
@Configuration
14+
@EnableAuthorizationServer
15+
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
16+
17+
@Bean
18+
public static NoOpPasswordEncoder passwordEncoder() {
19+
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
20+
}
21+
22+
@Override
23+
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
24+
clients.inMemory()
25+
.withClient("clientapp").secret("112233") // Client 账号、密码。
26+
.authorizedGrantTypes("client_credentials") // 客户端模式
27+
.scopes("read_userinfo", "read_contacts") // 可授权的 Scope
28+
// .and().withClient() // 可以继续配置新的 Client
29+
;
30+
}
31+
32+
}

0 commit comments

Comments
 (0)