README.md 10.9 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 12 13 14
[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) 
![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) 
![GitHub Release Date](https://img.shields.io/github/release-date/tomsun28/sureness?color=blue&logo=figshare&logoColor=red) 
15

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

sinat_25235033's avatar
sinat_25235033 已提交
18 19 20 21 22
## Background   

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.     
For existing frameworks, whether it is apache shiro which does not natively support rest, or deeply bound spring, the slower performance and steep learning curve of spring security are not our ideal framework.   
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 已提交
23

24
## <font color="green">`Introduction`</font>
sinat_25235033's avatar
sinat_25235033 已提交
25

sinat_25235033's avatar
sinat_25235033 已提交
26

sinat_25235033's avatar
sinat_25235033 已提交
27 28 29 30 31 32
> 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.   
> Support mainstream http container(servlet and jax-rs).    
> Supports JWT, Basic Auth, Digest Auth... Can extend custom supported authentication methods.    
33
> High performance due dictionary matching tree.      
sinat_25235033's avatar
sinat_25235033 已提交
34
> Good extension interface, demo and document.    
sinat_25235033's avatar
sinat_25235033 已提交
35

36
> 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 已提交
37

sinat_25235033's avatar
sinat_25235033 已提交
38 39 40 41 42 43 44 45 46 47 48
##### Compare     

| ~         | sureness | shiro | spring security |
| ---       | ---      | ---   | --- |
| **multi framework support**  | support      | support need modify   | not support |
| **restful api** | support | support need modify   | support |
| **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 已提交
49
| **performance** | fast | slower | slower|
50
| **learning curve** | simple | simple | steep|
51 52 53 54 55

##### Benchmark  

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

sinat_25235033's avatar
sinat_25235033 已提交
56 57
**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 已提交
58
**The performance gap will be further widened as the api matching chain increases.**      
59

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

sinat_25235033's avatar
sinat_25235033 已提交
62 63
##### Framework Sample Support  

sinat_25235033's avatar
sinat_25235033 已提交
64 65 66 67 68 69
- [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)   
70
- [x] more samples todo  
sinat_25235033's avatar
sinat_25235033 已提交
71

72
## Quick Start 
sinat_25235033's avatar
sinat_25235033 已提交
73

sinat_25235033's avatar
sinat_25235033 已提交
74
#### <font color="red">Some Conventions</font>  
sinat_25235033's avatar
sinat_25235033 已提交
75

sinat_25235033's avatar
sinat_25235033 已提交
76
- Based RBAC, only has role-resource, no permission action.    
77 78 79
- 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 已提交
80
- User belongs some Role -- Role owns Resource -- User can access the resource.  
sinat_25235033's avatar
sinat_25235033 已提交
81

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

84
#### Add Sureness In Your Project  
sinat_25235033's avatar
sinat_25235033 已提交
85

sinat_25235033's avatar
sinat_25235033 已提交
86
When use maven or gradle build project, add coordinate  
sinat_25235033's avatar
sinat_25235033 已提交
87 88 89 90
```
<dependency>
    <groupId>com.usthe.sureness</groupId>
    <artifactId>sureness-core</artifactId>
sinat_25235033's avatar
sinat_25235033 已提交
91
    <version>0.4.4</version>
sinat_25235033's avatar
sinat_25235033 已提交
92 93 94
</dependency>
```
```
sinat_25235033's avatar
sinat_25235033 已提交
95
compile group: 'com.usthe.sureness', name: 'sureness-core', version: '0.4.4'
sinat_25235033's avatar
sinat_25235033 已提交
96
```
sinat_25235033's avatar
sinat_25235033 已提交
97

98
#### Use the Default Configuration to Configure Sureness  
sinat_25235033's avatar
sinat_25235033 已提交
99

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

sinat_25235033's avatar
sinat_25235033 已提交
109 110
#### Load Auth Config DataSource   

111
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 已提交
112 113
We provide interfaces `SurenessAccountProvider`, `PathTreeProvider` for user implement to load data from the dataSource where they want.  

114 115
- `SurenessAccountProvider` - Account datasource provider interface.    
- `PathTreeProvider` - Resource uri-role datasource provider interface.     
sinat_25235033's avatar
sinat_25235033 已提交
116

117 118
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 已提交
119

120 121
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 已提交
122 123


sinat_25235033's avatar
sinat_25235033 已提交
124
#### Add an Interceptor Intercepting All Requests  
sinat_25235033's avatar
sinat_25235033 已提交
125

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

sinat_25235033's avatar
sinat_25235033 已提交
132 133
#### Implement Auth Exception Handling Process    

134
Sureness uses exception handling process:  
sinat_25235033's avatar
sinat_25235033 已提交
135

136 137 138 139 140 141
- 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 已提交
142 143

```
sinat_25235033's avatar
sinat_25235033 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156
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 已提交
157 158
```

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

161
**Have Fun**      
sinat_25235033's avatar
sinat_25235033 已提交
162

163
## Advanced Use
sinat_25235033's avatar
sinat_25235033 已提交
164

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

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

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

171
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 已提交
172

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

175 176 177 178 179
- `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 已提交
180

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

183
1. **Custom Subject**
sinat_25235033's avatar
sinat_25235033 已提交
184

185 186
`Implment Subject, add custom subject content`  
`Implment SubjectCreate to create custom subject`  
187 188 189
`Implment Processor to support custom subject`

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

191 192 193 194 195 196 197
2. **Custom SubjectCreator**

`Implment SubjectCreate to create your custom subject`   

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

3. **Custom Processor**
sinat_25235033's avatar
sinat_25235033 已提交
198

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

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

4. **Custom Datasource**  

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

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

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

213
## Contributing  
sinat_25235033's avatar
sinat_25235033 已提交
214

sinat_25235033's avatar
sinat_25235033 已提交
215
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 已提交
216

217 218
Components of Repository:  
- [sureness's kernel code--sureness-core](core)  
sinat_25235033's avatar
sinat_25235033 已提交
219 220
- [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 已提交
221
- [sample projects using sureness in each framework(javalin,ktor,quarkus)--samples](samples)  
sinat_25235033's avatar
sinat_25235033 已提交
222 223


sinat_25235033's avatar
sinat_25235033 已提交
224
#### Why Is High Performance  
sinat_25235033's avatar
sinat_25235033 已提交
225

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

sinat_25235033's avatar
sinat_25235033 已提交
228

sinat_25235033's avatar
sinat_25235033 已提交
229
## License  
230
[`Apache License, Version 2.0`](https://www.apache.org/licenses/LICENSE-2.0.html)