README.md 11.8 KB
Newer Older
1 2 3 4 5 6
<p align="center">
  <a href="https://su.usthe.com">
    <img alt="sureness" src="./docs/_media/hat-128.svg">
  </a>
</p>

sinat_25235033's avatar
sinat_25235033 已提交
7
# <font size="14p">sureness</font> <font size="5p">  | [中文文档](README_CN.md)</font>
sinat_25235033's avatar
sinat_25235033 已提交
8

sinat_25235033's avatar
sinat_25235033 已提交
9
> A simple and efficient open-source jvm security framework that focus on the protection of restful api.
sinat_25235033's avatar
sinat_25235033 已提交
10

sinat_25235033's avatar
sinat_25235033 已提交
11
[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) 
12
[![Maven](https://img.shields.io/badge/Maven%20Central-1.0.0-blue.svg)](https://search.maven.org/artifact/com.usthe.sureness/sureness-core) 
sinat_25235033's avatar
sinat_25235033 已提交
13 14
![GitHub pull request check contexts](https://img.shields.io/github/status/contexts/pulls/tomsun28/sureness/8?label=pull%20checks) 
[![Gitter](https://img.shields.io/gitter/room/usthe/sureness?label=sureness&color=orange&logo=gitter&logoColor=red)](https://gitter.im/usthe/sureness) 
sinat_25235033's avatar
sinat_25235033 已提交
15 16 17 18
![GitHub Release Date](https://img.shields.io/github/release-date/tomsun28/sureness?color=blue&logo=figshare&logoColor=red)
[![star](https://gitee.com/tomsun28/sureness/badge/star.svg?theme=gray)](https://gitee.com/tomsun28/sureness/stargazers)
[![star](https://img.shields.io/github/stars/usthe/sureness?style=social)](https://github.com/usthe/sureness)
<a href="https://www.producthunt.com/posts/sureness?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-sureness" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=287707&theme=light" alt="sureness - Jvm security framework that focus on protection of rest api | Product Hunt" width="120" height="23" /></a>
19

sinat_25235033's avatar
sinat_25235033 已提交
20
**Home Page: [usthe.com/sureness](https://usthe.com/sureness) or [su.usthe.com](https://su.usthe.com/)**  
sinat_25235033's avatar
sinat_25235033 已提交
21

22
## 📫 Background   
sinat_25235033's avatar
sinat_25235033 已提交
23 24

In the mainstream web architecture, how to protect the restful api provided by the back-end through effective and fast authentication has become particularly important.     
sinat_25235033's avatar
sinat_25235033 已提交
25
For existing frameworks, whether it is apache shiro which does not natively support rest, or deeply bound spring and steep learning curve of spring security are not our ideal framework.   
sinat_25235033's avatar
sinat_25235033 已提交
26
Ever since sureness was born, we hope to solve these, provide a **restful api**, **no framework dependency**, can **dynamically modify permissions**, **multiple authentication policies**, **faster**, **easy to use and extend** security framework.        
sinat_25235033's avatar
sinat_25235033 已提交
27

28
## 🎡 <font color="green">Introduction</font>
sinat_25235033's avatar
sinat_25235033 已提交
29

sinat_25235033's avatar
sinat_25235033 已提交
30

sinat_25235033's avatar
sinat_25235033 已提交
31 32 33 34
> Sureness is a new, permission project which we learn from apache shiro and add some ideas to create it.  
> Authentication for restful api, based on RBAC, mainly focused on the protection of restful api.  
> No specific framework dependency(support springboot, quarkus, javalin, ktor and more).    
> Support dynamic modification of permissions.   
sinat_25235033's avatar
sinat_25235033 已提交
35
> Support websocket, mainstream http container(servlet and jax-rs).    
sinat_25235033's avatar
sinat_25235033 已提交
36
> Supports JWT, Basic Auth, Digest Auth... Can extend custom supported authentication methods.    
37
> High performance due dictionary matching tree.      
sinat_25235033's avatar
sinat_25235033 已提交
38
> Good extension interface, demo and document.    
sinat_25235033's avatar
sinat_25235033 已提交
39

40
> The low configuration of sureness, easy to expand, and not coupled with other frameworks, enables developers to quickly and safely protect their projects in multiple scenarios.   
sinat_25235033's avatar
sinat_25235033 已提交
41

42
##### 🔍 Compare     
sinat_25235033's avatar
sinat_25235033 已提交
43 44 45 46 47

| ~         | sureness | shiro | spring security |
| ---       | ---      | ---   | --- |
| **multi framework support**  | support      | support need modify   | not support |
| **restful api** | support | support need modify   | support |
48
| **websocket** | support | not support   | not support |
sinat_25235033's avatar
sinat_25235033 已提交
49 50 51 52 53
| **path match**  | dictionary matching tree | ant match | ant match |
| **annotation support**    | support      | support      | support |
| **servlet**    | support      | support      | support |
| **jax-rs**     | support      | not support    | not support |
| **dynamic modification of permissions** | support | support need modify | support need modify |
sinat_25235033's avatar
sinat_25235033 已提交
54
| **performance** | fast | slower | slower|
55
| **learning curve** | simple | simple | steep|
56

57
##### 📈 Benchmark  
58 59 60

![benchmark](docs/_images/benchmark_en.png)  

sinat_25235033's avatar
sinat_25235033 已提交
61 62
**Benchmark test shows sureness to lose 0.026ms performance compared to frameless application, shiro lose 0.088ms, spring security lose 0.116ms.**    
**In contrast, sureness basically does not consume performance, and the performance (TPS loss) is 3 times that of shiro and 4 times that of spring security.**      
sinat_25235033's avatar
sinat_25235033 已提交
63
**The performance gap will be further widened as the api matching chain increases.**      
64

sinat_25235033's avatar
sinat_25235033 已提交
65
Detail see [Benchmark Test](https://github.com/tomsun28/sureness-shiro-spring-security-benchmark)       
sinat_25235033's avatar
sinat_25235033 已提交
66

67
##### ✌ Framework Sample Support  
sinat_25235033's avatar
sinat_25235033 已提交
68

sinat_25235033's avatar
sinat_25235033 已提交
69 70 71 72 73 74
- [x] sureness integration springboot sample(configuration file scheme) [sample-bootstrap](sample-bootstrap)   
- [x] sureness integration springboot sample(database scheme) [sample-tom](sample-tom)  
- [x] sureness integration quarkus sample [sample-quarkus](samples/quarkus-sureness)  
- [x] sureness integration javalin sample [sample-javalin](samples/javalin-sureness)    
- [x] sureness integration ktor sample [sample-ktor](samples/ktor-sureness)   
- [x] sureness integration spring webflux sample [sample-spring-webflux](samples/spring-webflux-sureness)   
75
- [x] more samples todo  
sinat_25235033's avatar
sinat_25235033 已提交
76

77
## 🔨 Quick Start 
sinat_25235033's avatar
sinat_25235033 已提交
78

79
#### 🐕 <font color="red">Some Conventions</font>  
sinat_25235033's avatar
sinat_25235033 已提交
80

sinat_25235033's avatar
sinat_25235033 已提交
81
- Based RBAC, only has role-resource, no permission action.    
82 83 84
- We treat restful requests as a resource, resource format like `requestUri===httpMethod`.   
  That is the request uri + request method(`post,get,put,delete...`) is considered as a resource as a whole.  
  `eg: /api/v2/book===get`    
sinat_25235033's avatar
sinat_25235033 已提交
85
- User belongs some Role -- Role owns Resource -- User can access the resource.  
sinat_25235033's avatar
sinat_25235033 已提交
86

sinat_25235033's avatar
sinat_25235033 已提交
87
Resource path matching see: [URI Match](docs/path-match.md)  
sinat_25235033's avatar
sinat_25235033 已提交
88

89
#### 🐖 Add Sureness In Your Project  
sinat_25235033's avatar
sinat_25235033 已提交
90

sinat_25235033's avatar
sinat_25235033 已提交
91
When use maven or gradle build project, add coordinate  
sinat_25235033's avatar
sinat_25235033 已提交
92 93 94 95
```
<dependency>
    <groupId>com.usthe.sureness</groupId>
    <artifactId>sureness-core</artifactId>
sinat_25235033's avatar
sinat_25235033 已提交
96
    <version>1.0.1</version>
sinat_25235033's avatar
sinat_25235033 已提交
97 98 99
</dependency>
```
```
sinat_25235033's avatar
sinat_25235033 已提交
100
compile group: 'com.usthe.sureness', name: 'sureness-core', version: '1.0.1'
sinat_25235033's avatar
sinat_25235033 已提交
101
```
sinat_25235033's avatar
sinat_25235033 已提交
102

103
#### 🐵 Use the Default Configuration to Configure Sureness  
sinat_25235033's avatar
sinat_25235033 已提交
104

105
The default configuration -`DefaultSurenessConfig` uses the document datasource `sureness.yml` as the auth datasource.  
sinat_25235033's avatar
sinat_25235033 已提交
106
It supports jwt, basic auth, digest auth authentication.  
sinat_25235033's avatar
sinat_25235033 已提交
107
```
sinat_25235033's avatar
sinat_25235033 已提交
108 109 110 111
@Bean
public DefaultSurenessConfig surenessConfig() {
    return new DefaultSurenessConfig();
}
sinat_25235033's avatar
sinat_25235033 已提交
112 113
```

114
#### 🐮 Load Auth Config DataSource   
sinat_25235033's avatar
sinat_25235033 已提交
115

116
Sureness authentication requires us to provide our own account data, role permission data, etc. These data may come from text, relational databases, non-relational databases, annotations, etc.   
sinat_25235033's avatar
sinat_25235033 已提交
117 118
We provide interfaces `SurenessAccountProvider`, `PathTreeProvider` for user implement to load data from the dataSource where they want.  

119 120
- `SurenessAccountProvider` - Account datasource provider interface.    
- `PathTreeProvider` - Resource uri-role datasource provider interface.     
sinat_25235033's avatar
sinat_25235033 已提交
121

122 123
Default Document DataSource Config - `sureness.yml`, see: [Default Document DataSource](docs/default-datasource.md)   
Annotation DataSource Config Detail - `AnnotationLoader`, see: [Annotation DataSource](docs/annotation-datasource.md)  
sinat_25235033's avatar
sinat_25235033 已提交
124

125 126
If the configuration resource data comes from text, please refer to  [Sureness integration springboot sample(configuration file scheme)](https://github.com/tomsun28/sureness/tree/master/sample-bootstrap)   
If the configuration resource data comes from dataBase, please refer to  [Sureness integration springboot sample(database scheme)](https://github.com/tomsun28/sureness/tree/master/sample-tom)   
sinat_25235033's avatar
sinat_25235033 已提交
127 128


129
#### 🐐 Add an Interceptor Intercepting All Requests  
sinat_25235033's avatar
sinat_25235033 已提交
130

131
The essence of sureness is to intercept all rest requests for authenticating and Authorizing.     
sinat_25235033's avatar
sinat_25235033 已提交
132
The interceptor can be a filter or a spring interceptor, it intercepts all request to check them.  
sinat_25235033's avatar
sinat_25235033 已提交
133
```
sinat_25235033's avatar
sinat_25235033 已提交
134
SubjectSum subject = SurenessSecurityManager.getInstance().checkIn(servletRequest)
sinat_25235033's avatar
sinat_25235033 已提交
135 136
```

137
#### 🐰 Implement Auth Exception Handling Process    
sinat_25235033's avatar
sinat_25235033 已提交
138

139
Sureness uses exception handling process:  
sinat_25235033's avatar
sinat_25235033 已提交
140

141 142 143 144 145 146
- If auth success, method - `checkIn` will return a `SubjectSum` object containing user information.    
- If auth failure, method - `checkIn` will throw different types of auth exceptions.   

Users need to continue the subsequent process based on these exceptions.(eg: return the request response)  

Here we need to customize the exceptions thrown by `checkIn`, passed directly when auth success, catch exception when auth failure and do something:    
sinat_25235033's avatar
sinat_25235033 已提交
147 148

```
sinat_25235033's avatar
sinat_25235033 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161
try {
    SubjectSum subject = SurenessSecurityManager.getInstance().checkIn(servletRequest);
} catch (ProcessorNotFoundException | UnknownAccountException | UnsupportedSubjectException e4) {
    // Create subject error related execption 
} catch (DisabledAccountException | ExcessiveAttemptsException e2 ) {
    // Account disable related exception
} catch (IncorrectCredentialsException | ExpiredCredentialsException e3) {
    // Authentication failure related exception
} catch (UnauthorizedException e5) {
    // Authorization failure related exception
} catch (SurenessAuthenticationException | SurenessAuthorizationException e) {
    // other sureness exception
}
sinat_25235033's avatar
sinat_25235033 已提交
162 163
```

sinat_25235033's avatar
sinat_25235033 已提交
164
Detail sureness auth exception see: [Default Sureness Auth Exception](docs/default-exception.md)   
sinat_25235033's avatar
sinat_25235033 已提交
165

166
**Have Fun**      
sinat_25235033's avatar
sinat_25235033 已提交
167

168
## 🥐 Advanced Use
sinat_25235033's avatar
sinat_25235033 已提交
169

170
Sureness supports custom subject, custom subjectCreator, custom processor and more.  
sinat_25235033's avatar
sinat_25235033 已提交
171

172
Before advanced custom extension, let's first understand the general process of sureness:  
sinat_25235033's avatar
sinat_25235033 已提交
173

174
![flow](/docs/_images/flow-en.png)  
sinat_25235033's avatar
sinat_25235033 已提交
175

176
As in the above process, Subject is created by SubjectCreate according to the request body, and different authentication processors process the supported Subjects.  
sinat_25235033's avatar
sinat_25235033 已提交
177

178
Sureness provides the following common interfaces as extension points:  
sinat_25235033's avatar
sinat_25235033 已提交
179

180 181 182 183 184
- `Subject`:  Authenticated authorized  user's account interface, provide the account's username,password, request resources, roles, etc.  
- `SubjectCreate`: Create subject interface, provider create method.   
- `Processor`:  Process subject interface, where happen authentication and authorization. 
- `PathTreeProvider`: Resource data provider, it can load data from txt or database,etc.
- `SurenessAccountProvider`: Account data provider, it can load data from txt or database,etc.   
sinat_25235033's avatar
sinat_25235033 已提交
185

sinat_25235033's avatar
sinat_25235033 已提交
186
Refer to [Extension Point](https://usthe.com/sureness/#/extend-point) for the extended documentation.   
sinat_25235033's avatar
sinat_25235033 已提交
187

188
1. 🥊 **Custom Subject**
sinat_25235033's avatar
sinat_25235033 已提交
189

190 191
`Implment Subject, add custom subject content`  
`Implment SubjectCreate to create custom subject`  
192 193 194
`Implment Processor to support custom subject`

See [Custom Subject](docs/custom-subject.md)  
sinat_25235033's avatar
sinat_25235033 已提交
195

196
2. 🔫 **Custom SubjectCreator**
197 198 199 200 201

`Implment SubjectCreate to create your custom subject`   

See [Custom SubjectCreator](docs/custom-subject-creator.md)  

202
3. 🪓 **Custom Processor**
sinat_25235033's avatar
sinat_25235033 已提交
203

204
`A subject also can support by different processor, so we can custom processor to support custom subject`
205 206 207 208
`Implment Processor, set which subject can support and implment processing details`

See [Custom Processor](docs/custom-processor.md)  

209
4. 🏹 **Custom Datasource**  
210 211 212 213 214

`Implment PathTreeProvider, load in DefaultPathRoleMatcher`   
`Implment SurenessAccountProvide, load in processor`  

See [Custom Datasource](docs/custom-datasource.md)  
sinat_25235033's avatar
sinat_25235033 已提交
215

216
Detail please refer to  [Sureness integration springboot sample(database scheme)](sample-tom)   
sinat_25235033's avatar
sinat_25235033 已提交
217

218
## 🙋 Contributing  
sinat_25235033's avatar
sinat_25235033 已提交
219

sinat_25235033's avatar
sinat_25235033 已提交
220
Very welcome to Contribute this project, go further and better with sureness. If you have any questions or suggestions about the project code, please contact @tomsun28 directly.  
sinat_25235033's avatar
sinat_25235033 已提交
221

222 223
Components of Repository:  
- [sureness's kernel code--sureness-core](core)  
sinat_25235033's avatar
sinat_25235033 已提交
224 225
- [sureness integration springboot sample(configuration file scheme)--sample-bootstrap](sample-bootstrap)  
- [sureness integration springboot sample(database scheme)-sample-tom](sample-tom)  
sinat_25235033's avatar
sinat_25235033 已提交
226
- [sample projects using sureness in each framework(javalin,ktor,quarkus)--samples](samples)  
sinat_25235033's avatar
sinat_25235033 已提交
227 228


229
#### 💪 Why Is High Performance  
sinat_25235033's avatar
sinat_25235033 已提交
230

231
![pathRoleMatcher](docs/_images/PathRoleMatcher.svg)  
sinat_25235033's avatar
sinat_25235033 已提交
232

sinat_25235033's avatar
sinat_25235033 已提交
233

234
## 🛡️ License  
235
[`Apache License, Version 2.0`](https://www.apache.org/licenses/LICENSE-2.0.html)