Skip to content

Commit b0ade85

Browse files
committed
ApiBoot Security Oauth自定义用户表示例.
1 parent 854a7d6 commit b0ade85

File tree

3 files changed

+322
-3
lines changed

3 files changed

+322
-3
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
-- MySQL dump 10.16 Distrib 10.2.13-MariaDB, for osx10.13 (x86_64)
2+
--
3+
-- Host: 127.0.0.1 Database: test
4+
-- ------------------------------------------------------
5+
-- Server version 10.2.13-MariaDB
6+
7+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
8+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
9+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
10+
/*!40101 SET NAMES utf8 */;
11+
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
12+
/*!40103 SET TIME_ZONE='+00:00' */;
13+
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
14+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
15+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
16+
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
17+
18+
--
19+
-- Table structure for table `clientdetails`
20+
--
21+
22+
DROP TABLE IF EXISTS `clientdetails`;
23+
/*!40101 SET @saved_cs_client = @@character_set_client */;
24+
/*!40101 SET character_set_client = utf8 */;
25+
CREATE TABLE `clientdetails` (
26+
`appId` varchar(128) NOT NULL,
27+
`resourceIds` varchar(256) DEFAULT NULL,
28+
`appSecret` varchar(256) DEFAULT NULL,
29+
`scope` varchar(256) DEFAULT NULL,
30+
`grantTypes` varchar(256) DEFAULT NULL,
31+
`redirectUrl` varchar(256) DEFAULT NULL,
32+
`authorities` varchar(256) DEFAULT NULL,
33+
`access_token_validity` int(11) DEFAULT NULL,
34+
`refresh_token_validity` int(11) DEFAULT NULL,
35+
`additionalInformation` varchar(4096) DEFAULT NULL,
36+
`autoApproveScopes` varchar(256) DEFAULT NULL,
37+
PRIMARY KEY (`appId`)
38+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
39+
/*!40101 SET character_set_client = @saved_cs_client */;
40+
41+
--
42+
-- Dumping data for table `clientdetails`
43+
--
44+
45+
LOCK TABLES `clientdetails` WRITE;
46+
/*!40000 ALTER TABLE `clientdetails` DISABLE KEYS */;
47+
/*!40000 ALTER TABLE `clientdetails` ENABLE KEYS */;
48+
UNLOCK TABLES;
49+
50+
--
51+
-- Table structure for table `oauth_client_details`
52+
--
53+
54+
DROP TABLE IF EXISTS `oauth_client_details`;
55+
/*!40101 SET @saved_cs_client = @@character_set_client */;
56+
/*!40101 SET character_set_client = utf8 */;
57+
CREATE TABLE `oauth_client_details` (
58+
`client_id` varchar(128) NOT NULL,
59+
`resource_ids` varchar(256) DEFAULT NULL,
60+
`client_secret` varchar(256) DEFAULT NULL,
61+
`scope` varchar(256) DEFAULT NULL,
62+
`authorized_grant_types` varchar(256) DEFAULT NULL,
63+
`web_server_redirect_uri` varchar(256) DEFAULT NULL,
64+
`authorities` varchar(256) DEFAULT NULL,
65+
`access_token_validity` int(11) DEFAULT NULL,
66+
`refresh_token_validity` int(11) DEFAULT NULL,
67+
`additional_information` varchar(4096) DEFAULT NULL,
68+
`autoapprove` varchar(256) DEFAULT NULL,
69+
PRIMARY KEY (`client_id`)
70+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
71+
/*!40101 SET character_set_client = @saved_cs_client */;
72+
73+
74+
--
75+
-- Table structure for table `oauth_access_token`
76+
--
77+
78+
DROP TABLE IF EXISTS `oauth_access_token`;
79+
/*!40101 SET @saved_cs_client = @@character_set_client */;
80+
/*!40101 SET character_set_client = utf8 */;
81+
CREATE TABLE `oauth_access_token` (
82+
`token_id` varchar(256) DEFAULT NULL,
83+
`token` blob DEFAULT NULL,
84+
`authentication_id` varchar(128) NOT NULL,
85+
`user_name` varchar(256) DEFAULT NULL,
86+
`client_id` varchar(256) DEFAULT NULL,
87+
`authentication` blob DEFAULT NULL,
88+
`refresh_token` varchar(256) DEFAULT NULL,
89+
PRIMARY KEY (`authentication_id`)
90+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
91+
/*!40101 SET character_set_client = @saved_cs_client */;
92+
93+
94+
--
95+
-- Table structure for table `oauth_approvals`
96+
--
97+
98+
DROP TABLE IF EXISTS `oauth_approvals`;
99+
/*!40101 SET @saved_cs_client = @@character_set_client */;
100+
/*!40101 SET character_set_client = utf8 */;
101+
CREATE TABLE `oauth_approvals` (
102+
`userId` varchar(256) DEFAULT NULL,
103+
`clientId` varchar(256) DEFAULT NULL,
104+
`scope` varchar(256) DEFAULT NULL,
105+
`status` varchar(10) DEFAULT NULL,
106+
`expiresAt` datetime DEFAULT NULL,
107+
`lastModifiedAt` datetime DEFAULT NULL
108+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
109+
/*!40101 SET character_set_client = @saved_cs_client */;
110+
111+
--
112+
-- Dumping data for table `oauth_approvals`
113+
--
114+
115+
LOCK TABLES `oauth_approvals` WRITE;
116+
/*!40000 ALTER TABLE `oauth_approvals` DISABLE KEYS */;
117+
/*!40000 ALTER TABLE `oauth_approvals` ENABLE KEYS */;
118+
UNLOCK TABLES;
119+
120+
--
121+
-- Table structure for table `oauth_client_token`
122+
--
123+
124+
DROP TABLE IF EXISTS `oauth_client_token`;
125+
/*!40101 SET @saved_cs_client = @@character_set_client */;
126+
/*!40101 SET character_set_client = utf8 */;
127+
CREATE TABLE `oauth_client_token` (
128+
`token_id` varchar(256) DEFAULT NULL,
129+
`token` blob DEFAULT NULL,
130+
`authentication_id` varchar(128) NOT NULL,
131+
`user_name` varchar(256) DEFAULT NULL,
132+
`client_id` varchar(256) DEFAULT NULL,
133+
PRIMARY KEY (`authentication_id`)
134+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
135+
/*!40101 SET character_set_client = @saved_cs_client */;
136+
137+
--
138+
-- Dumping data for table `oauth_client_token`
139+
--
140+
141+
LOCK TABLES `oauth_client_token` WRITE;
142+
/*!40000 ALTER TABLE `oauth_client_token` DISABLE KEYS */;
143+
/*!40000 ALTER TABLE `oauth_client_token` ENABLE KEYS */;
144+
UNLOCK TABLES;
145+
146+
--
147+
-- Table structure for table `oauth_refresh_token`
148+
--
149+
150+
DROP TABLE IF EXISTS `oauth_refresh_token`;
151+
/*!40101 SET @saved_cs_client = @@character_set_client */;
152+
/*!40101 SET character_set_client = utf8 */;
153+
CREATE TABLE `oauth_refresh_token` (
154+
`token_id` varchar(256) DEFAULT NULL,
155+
`token` blob DEFAULT NULL,
156+
`authentication` blob DEFAULT NULL
157+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
158+
/*!40101 SET character_set_client = @saved_cs_client */;
159+
160+
--
161+
-- Dumping data for table `oauth_refresh_token`
162+
--
163+
164+
LOCK TABLES `oauth_refresh_token` WRITE;
165+
/*!40000 ALTER TABLE `oauth_refresh_token` DISABLE KEYS */;
166+
/*!40000 ALTER TABLE `oauth_refresh_token` ENABLE KEYS */;
167+
UNLOCK TABLES;
168+
169+
--
170+
-- Table structure for table `oauth_code`
171+
--
172+
173+
DROP TABLE IF EXISTS `oauth_code`;
174+
/*!40101 SET @saved_cs_client = @@character_set_client */;
175+
/*!40101 SET character_set_client = utf8 */;
176+
CREATE TABLE `oauth_code` (
177+
`code` varchar(256) DEFAULT NULL,
178+
`authentication` blob DEFAULT NULL
179+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
180+
/*!40101 SET character_set_client = @saved_cs_client */;
181+
182+
--
183+
-- Dumping data for table `oauth_code`
184+
--
185+
186+
LOCK TABLES `oauth_code` WRITE;
187+
/*!40000 ALTER TABLE `oauth_code` DISABLE KEYS */;
188+
/*!40000 ALTER TABLE `oauth_code` ENABLE KEYS */;
189+
UNLOCK TABLES;
190+
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
191+
192+
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
193+
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
194+
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
195+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
196+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
197+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
198+
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
199+
200+
-- Dump completed on 2019-03-20 15:34:12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.minbox.framework.api.boot.sample;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import org.minbox.framework.api.boot.autoconfigure.security.web.delegate.ApiBootStoreDelegate;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.security.core.GrantedAuthority;
9+
import org.springframework.security.core.userdetails.UserDetails;
10+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
11+
import org.springframework.security.crypto.password.PasswordEncoder;
12+
import org.springframework.stereotype.Component;
13+
14+
import java.util.ArrayList;
15+
import java.util.Collection;
16+
import java.util.List;
17+
18+
/**
19+
* 禁用默认的用户表结构
20+
* 使用自定义数据源读取用户信息
21+
*
22+
* @author:恒宇少年 - 于起宇
23+
* <p>
24+
* DateTime:2019-03-20 15:12
25+
* Blog:http://blog.yuqiyu.com
26+
* WebSite:http://www.jianshu.com/u/092df3f77bca
27+
* Gitee:https://gitee.com/hengboy
28+
* GitHub:https://github.com/hengyuboy
29+
*/
30+
@Component
31+
public class DisableDefaultUserTableStoreDelegate implements ApiBootStoreDelegate {
32+
33+
@Autowired
34+
private PasswordEncoder passwordEncoder;
35+
36+
/**
37+
* 用户列表示例
38+
* 从该集合内读取用户信息
39+
* 可以使用集合内的用户获取access_token
40+
*/
41+
static List<String> users = new ArrayList() {
42+
{
43+
add("api-boot");
44+
add("hengboy");
45+
add("yuqiyu");
46+
}
47+
};
48+
49+
/**
50+
* 根据用户名查询用户信息
51+
*
52+
* @param username 用户名
53+
* @return
54+
* @throws UsernameNotFoundException
55+
*/
56+
@Override
57+
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
58+
if (!users.contains(username)) {
59+
throw new UsernameNotFoundException("用户:" + username + "不存在");
60+
}
61+
return new DisableDefaultUserDetails(username);
62+
}
63+
64+
@Data
65+
@AllArgsConstructor
66+
@NoArgsConstructor
67+
class DisableDefaultUserDetails implements UserDetails {
68+
private String username;
69+
70+
@Override
71+
public Collection<? extends GrantedAuthority> getAuthorities() {
72+
return new ArrayList() {
73+
{
74+
add((GrantedAuthority) () -> "ROLE_USER");
75+
}
76+
};
77+
}
78+
79+
/**
80+
* 示例密码使用123456
81+
*
82+
* @return
83+
*/
84+
@Override
85+
public String getPassword() {
86+
return passwordEncoder.encode("123456");
87+
}
88+
89+
@Override
90+
public String getUsername() {
91+
return username;
92+
}
93+
94+
@Override
95+
public boolean isAccountNonExpired() {
96+
return true;
97+
}
98+
99+
@Override
100+
public boolean isAccountNonLocked() {
101+
return true;
102+
}
103+
104+
@Override
105+
public boolean isCredentialsNonExpired() {
106+
return true;
107+
}
108+
109+
@Override
110+
public boolean isEnabled() {
111+
return true;
112+
}
113+
}
114+
}

api-boot-samples/api-boot-sample-security-oauth-jwt/src/main/resources/application.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ api:
55
boot:
66
security:
77
# Spring Security 内存方式用户列表示例
8-
users:
9-
- username: hengboy
10-
password: 123456
8+
#users:
9+
#- username: hengboy
10+
# password: 123456
11+
#- username: apiboot
12+
# password: abc321
13+
enable-default-store-delegate: false
14+
away: jdbc
1115
oauth:
1216
jwt:
17+
away: jdbc
1318
# 开启Jwt转换AccessToken
1419
enable: true
1520
# 转换Jwt时所需加密key,默认为ApiBoot

0 commit comments

Comments
 (0)