mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
研究下 OAuth2 进行改造
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
<?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>lab-68</artifactId>
|
||||||
|
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>lab-68-demo01-resource-owner-password-credentials-server</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- 依赖相关配置 -->
|
||||||
|
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||||
|
<!-- 插件相关配置 -->
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>${spring.boot.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- 实现对 Spring MVC 的自动配置 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 实现对 Spring Security 的自动配置 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 实现对 Spring Security OAuth2 的自动配置 -->
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>org.springframework.security.oauth</groupId>-->
|
||||||
|
<!-- <artifactId>spring-security-oauth2</artifactId>-->
|
||||||
|
<!-- <version>2.5.0.RELEASE</version>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
|
||||||
|
<!-- 实现对 Spring Security OAuth2 的自动配置 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security.oauth.boot</groupId>
|
||||||
|
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
||||||
|
<version>${spring.boot.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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 ,所以改成这样。
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package cn.iocoder.springboot.lab68.resourceserverdemo;
|
|
||||||
|
|
||||||
public class ResourceServerApplication {
|
|
||||||
}
|
|
||||||
@@ -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/**");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>lab-68-demo01-resource-server</artifactId>
|
<artifactId>lab-68-demo02-resource-server</artifactId>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- 依赖相关配置 -->
|
<!-- 依赖相关配置 -->
|
||||||
@@ -45,12 +45,18 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- 实现对 Spring Security OAuth2 的自动配置 -->
|
<!-- 实现对 Spring Security OAuth2 的自动配置 -->
|
||||||
<dependency>
|
<!-- <dependency>-->
|
||||||
<groupId>org.springframework.security.oauth</groupId>
|
<!-- <groupId>org.springframework.security.oauth</groupId>-->
|
||||||
<artifactId>spring-security-oauth2</artifactId>
|
<!-- <artifactId>spring-security-oauth2</artifactId>-->
|
||||||
<version>2.5.0.RELEASE</version>
|
<!-- <version>2.5.0.RELEASE</version>-->
|
||||||
</dependency>
|
<!-- </dependency>-->
|
||||||
|
|
||||||
|
<!-- 实现对 Spring Security OAuth2 的自动配置 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security.oauth.boot</groupId>
|
||||||
|
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
||||||
|
<version>${spring.boot.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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 ,所以改成这样。
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
server:
|
||||||
|
port: 9090
|
||||||
|
|
||||||
|
security:
|
||||||
|
oauth2:
|
||||||
|
client:
|
||||||
|
client-id: clientapp
|
||||||
|
client-secret: 112233
|
||||||
|
resource:
|
||||||
|
token-info-uri:
|
||||||
@@ -12,7 +12,8 @@
|
|||||||
<artifactId>lab-68</artifactId>
|
<artifactId>lab-68</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<modules>
|
<modules>
|
||||||
<module>lab-68-demo01-resource-server</module>
|
<module>lab-68-demo01-resource-owner-password-credentials-server</module>
|
||||||
|
<module>lab-68-demo02-resource-server</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user