Skip to content

Commit a166f82

Browse files
author
YunaiV
committed
增加 OAuth2 简化模式
1 parent 605cde5 commit a166f82

File tree

7 files changed

+207
-0
lines changed

7 files changed

+207
-0
lines changed

lab-68/lab-68-demo01-implicit/pom.xml

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-implicit</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,13 @@
1+
package cn.iocoder.springboot.lab68.resourceserverdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ResourceServerApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ResourceServerApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.iocoder.springboot.lab68.resourceserverdemo.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.authentication.AuthenticationManager;
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+
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
10+
11+
/**
12+
* 授权服务器配置
13+
*/
14+
@Configuration
15+
@EnableAuthorizationServer
16+
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
17+
18+
// 用户认证
19+
@Autowired
20+
private AuthenticationManager authenticationManager;
21+
22+
@Override
23+
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
24+
endpoints.authenticationManager(authenticationManager);
25+
}
26+
27+
@Override
28+
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
29+
clients.inMemory()
30+
.withClient("clientapp").secret("112233") // Client 账号、密码。
31+
.authorizedGrantTypes("implicit") // 授权码模式
32+
.redirectUris("http://127.0.0.1:9090/callback02") // 配置回调地址,选填。
33+
.scopes("read_userinfo", "read_contacts") // 可授权的 Scope
34+
// .and().withClient() // 可以继续配置新的 Client
35+
;
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.iocoder.springboot.lab68.resourceserverdemo.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5+
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
6+
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
7+
8+
/**
9+
* 资源服务器配置
10+
*/
11+
@Configuration
12+
@EnableResourceServer
13+
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {
14+
15+
@Override
16+
public void configure(HttpSecurity http) throws Exception {
17+
http.authorizeRequests()
18+
.anyRequest().authenticated()
19+
// 设置 /api/ 开头的 URL 需要保护
20+
.and().requestMatchers().antMatchers("/api/**");
21+
}
22+
23+
}
24+
25+
// 实际,OAuth2ResourceServer 不是和 OAuth2AuthorizationServer 一起。
26+
// 主要考虑,简化 demo ,所以改成这样。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.authentication.AuthenticationManager;
6+
import org.springframework.security.config.BeanIds;
7+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
8+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
10+
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
11+
12+
@Configuration
13+
@EnableWebSecurity
14+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
15+
16+
@Override
17+
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
18+
public AuthenticationManager authenticationManagerBean() throws Exception {
19+
return super.authenticationManagerBean();
20+
}
21+
22+
@Bean
23+
public static NoOpPasswordEncoder passwordEncoder() {
24+
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
25+
}
26+
27+
@Override
28+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
29+
auth.
30+
// 使用内存中的 InMemoryUserDetailsManager
31+
inMemoryAuthentication()
32+
// 不使用 PasswordEncoder 密码编码器
33+
.passwordEncoder(passwordEncoder())
34+
// 配置 yunai 用户
35+
.withUser("yunai").password("1024").roles("USER");
36+
}
37+
38+
// @Override
39+
// protected void configure(HttpSecurity http) throws Exception {
40+
// http.authorizeRequests()
41+
// // 对所有 URL 都进行认证
42+
// .anyRequest()
43+
// .authenticated();
44+
// }
45+
46+
// @Override
47+
// public void configure(HttpSecurity http) throws Exception {
48+
// http.csrf()
49+
// .disable()
50+
// .authorizeRequests()
51+
// .antMatchers("/oauth/**", "/login/**", "/logout/**").permitAll()
52+
// .anyRequest().authenticated()
53+
// .and().formLogin().permitAll();
54+
// }
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.iocoder.springboot.lab68.resourceserverdemo.controller;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
/**
7+
* 示例模块 Controller
8+
*/
9+
@RestController
10+
@RequestMapping("/api/example")
11+
public class ExampleController {
12+
13+
@RequestMapping("/hello")
14+
public String hello() {
15+
return "world";
16+
}
17+
18+
}

lab-68/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<modules>
1515
<module>lab-68-demo01-resource-owner-password-credentials-server</module>
1616
<module>lab-68-demo01-authorization-code</module>
17+
<module>lab-68-demo01-implicit</module>
1718

1819
<module>lab-68-demo02-resource-server</module>
1920
<module>lab-68-demo02-authorization-server-with-resource-owner-password-credentials</module>

0 commit comments

Comments
 (0)