mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 OAuth2 数据库存储
This commit is contained in:
@@ -1 +1 @@
|
||||
<http://www.iocoder.cn/Dubbo/Admin/github>
|
||||
<http://www.iocoder.cn/Dubbo/Admin/?github>
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
<?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-demo11-authorization-server-by-jdbc-store</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.boot</groupId>
|
||||
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对数据库连接池的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency> <!-- 本示例,我们使用 MySQL -->
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.48</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springboot.lab68.authorizationserverdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AuthorizationServerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AuthorizationServerApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package cn.iocoder.springboot.lab68.authorizationserverdemo.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
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;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
|
||||
import org.springframework.security.oauth2.provider.ClientDetailsService;
|
||||
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 授权服务器配置
|
||||
*/
|
||||
@Configuration
|
||||
@EnableAuthorizationServer
|
||||
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
|
||||
|
||||
/**
|
||||
* 用户认证 Manager
|
||||
*/
|
||||
@Autowired
|
||||
private AuthenticationManager authenticationManager;
|
||||
|
||||
/**
|
||||
* 数据源 DataSource
|
||||
*/
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Bean
|
||||
public TokenStore jdbcTokenStore() {
|
||||
return new JdbcTokenStore(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
|
||||
endpoints.authenticationManager(authenticationManager)
|
||||
.tokenStore(jdbcTokenStore());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
|
||||
oauthServer.checkTokenAccess("isAuthenticated()");
|
||||
// oauthServer.tokenKeyAccess("isAuthenticated()")
|
||||
// .checkTokenAccess("isAuthenticated()");
|
||||
// oauthServer.tokenKeyAccess("permitAll()")
|
||||
// .checkTokenAccess("permitAll()");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ClientDetailsService jdbcClientDetailsService() {
|
||||
return new JdbcClientDetailsService(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
|
||||
clients.withClientDetails(jdbcClientDetailsService());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package cn.iocoder.springboot.lab68.authorizationserverdemo.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.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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
spring:
|
||||
# datasource 数据源配置内容,对应 DataSourceProperties 配置属性类
|
||||
datasource:
|
||||
url: jdbc:mysql://127.0.0.1:43063/demo-68-authorization-server?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
username: root # 数据库账号
|
||||
password: 123456 # 数据库密码
|
||||
@@ -0,0 +1,7 @@
|
||||
INSERT INTO oauth_client_details
|
||||
(client_id, client_secret, scope, authorized_grant_types,
|
||||
web_server_redirect_uri, authorities, access_token_validity,
|
||||
refresh_token_validity, additional_information, autoapprove)
|
||||
VALUES
|
||||
('clientapp', '112233', 'read_userinfo,read_contacts',
|
||||
'password,refresh_token', null, null, 3600, 864000, null, true);
|
||||
@@ -0,0 +1,52 @@
|
||||
--------------- MySQL ---------------
|
||||
drop table if exists oauth_client_details;
|
||||
create table oauth_client_details (
|
||||
client_id VARCHAR(255) PRIMARY KEY,
|
||||
resource_ids VARCHAR(255),
|
||||
client_secret VARCHAR(255),
|
||||
scope VARCHAR(255),
|
||||
authorized_grant_types VARCHAR(255),
|
||||
web_server_redirect_uri VARCHAR(255),
|
||||
authorities VARCHAR(255),
|
||||
access_token_validity INTEGER,
|
||||
refresh_token_validity INTEGER,
|
||||
additional_information VARCHAR(4096),
|
||||
autoapprove VARCHAR(255)
|
||||
);
|
||||
|
||||
create table if not exists oauth_client_token (
|
||||
token_id VARCHAR(255),
|
||||
token LONG VARBINARY,
|
||||
authentication_id VARCHAR(255) PRIMARY KEY,
|
||||
user_name VARCHAR(255),
|
||||
client_id VARCHAR(255)
|
||||
);
|
||||
|
||||
create table if not exists oauth_access_token (
|
||||
token_id VARCHAR(255),
|
||||
token LONG VARBINARY,
|
||||
authentication_id VARCHAR(255) PRIMARY KEY,
|
||||
user_name VARCHAR(255),
|
||||
client_id VARCHAR(255),
|
||||
authentication LONG VARBINARY,
|
||||
refresh_token VARCHAR(255)
|
||||
);
|
||||
|
||||
create table if not exists oauth_refresh_token (
|
||||
token_id VARCHAR(255),
|
||||
token LONG VARBINARY,
|
||||
authentication LONG VARBINARY
|
||||
);
|
||||
|
||||
create table if not exists oauth_code (
|
||||
code VARCHAR(255), authentication LONG VARBINARY
|
||||
);
|
||||
|
||||
create table if not exists oauth_approvals (
|
||||
userId VARCHAR(255),
|
||||
clientId VARCHAR(255),
|
||||
scope VARCHAR(255),
|
||||
status VARCHAR(10),
|
||||
expiresAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
lastModifiedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
@@ -24,6 +24,7 @@
|
||||
<module>lab-68-demo02-authorization-server-with-client-credentials</module>
|
||||
|
||||
<module>lab-68-demo03-authorization-server-with-resource-owner-password-credentials</module>
|
||||
<module>lab-68-demo11-authorization-server-by-jdbc-store</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user