README.md 12.6 KB
Newer Older
1
<p align="center">
sinat_25235033's avatar
sinat_25235033 已提交
2
  <a href="https://github.com/usthe/sureness">
sinat_25235033's avatar
sinat_25235033 已提交
3
    <img alt="sureness" src="./docs/_media/brand128.svg">
4 5 6
  </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 jvm security framework that focus on the protection of REST API.
sinat_25235033's avatar
sinat_25235033 已提交
10

11
[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
sinat_25235033's avatar
sinat_25235033 已提交
12
[![Maven](https://img.shields.io/badge/Maven%20Central-1.0.5-blue.svg)](https://search.maven.org/artifact/com.usthe.sureness/sureness-core)
13 14 15 16 17
![GitHub pull request check contexts](https://img.shields.io/github/status/contexts/pulls/dromara/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/dromara/sureness?color=blue&logo=figshare&logoColor=red)
[![star](https://gitee.com/dromara/sureness/badge/star.svg?theme=gray)](https://gitee.com/dromara/sureness/stargazers)
[![star](https://img.shields.io/github/stars/dromara/sureness?style=social)](https://github.com/dromara/sureness)
sinat_25235033's avatar
sinat_25235033 已提交
18 19

<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"/></a>
20

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

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

25
> [Sureness](https://github.com/dromara/sureness) is a simple and efficient open-source security framework that focus on the protection of REST API.  
26
> Provide authentication and authorization, based on RBAC.   
27
> No specific framework dependency (supports Javalin, Spring Boot, Quarkus, Ktor, Micronaut and more).    
28
> Supports dynamic modification of permissions.   
sinat_25235033's avatar
sinat_25235033 已提交
29 30 31
> Supports WebSockets and HTTP containers (Servlet and JAX-RS).    
> Supports JWT, Basic Auth, Digest Auth, and can custom auth methods.    
> High performance with Dictionary Matching Tree.      
32 33 34
> Good extension interface, demos and documentation.

> Sureness has a sensible default configuration, is easy to customize, and is not couple to any one framework, which enables developers to quickly and safely protect their projects in multiple scenarios.
sinat_25235033's avatar
sinat_25235033 已提交
35

36
##### 🔍 Compare     
sinat_25235033's avatar
sinat_25235033 已提交
37

sinat_25235033's avatar
sinat_25235033 已提交
38
| ~         | Sureness | Shiro | Spring Security |
sinat_25235033's avatar
sinat_25235033 已提交
39
| ---       | ---      | ---   | --- |
40
| **Multi Framework Support**  | support      | support need modify   | not support |
41
| **REST API** | support | support need modify   | support |
42 43 44 45 46
| **Websocket** | support | not support   | not 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 |
sinat_25235033's avatar
sinat_25235033 已提交
47
| **Dynamic Permissions** | support | support need modify | support need modify |
48 49
| **Performance** | fast | slower | slower|
| **Learning Curve** | simple | simple | steep|
50

51
##### 📈 Benchmark  
52 53 54

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

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

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

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

sinat_25235033's avatar
sinat_25235033 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76
- [x] Sureness integration **Spring Boot** sample(configuration file scheme) [sample-bootstrap](sample-bootstrap)   
- [x] Sureness integration **Spring Boot** 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)
- [x] Sureness integration **Micronaut** sample [sample-micronaut](samples/micronaut-sureness)
- [x] Sureness integration **Jfinal** sample [sample-jfinal](samples/jfinal-sureness)
- [x] Sureness integration **Solon** sample [sample-solon](samples/solon-sureness)
- [x] Sureness integration **Spring Gateway** sample [sample-spring-gateway](samples/spring-gateway-sureness)  
- [x] Sureness integration **Zuul** sample [sample-zuul](samples/zuul-sureness)    
- [x] Sureness integration Session sample [sureness-session](samples/sureness-session)    
- [x] Sureness integration Redis Session cache sample [sureness-redis-session](samples/sureness-redis-session)  
- [x] More samples todo  
sinat_25235033's avatar
sinat_25235033 已提交
77

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

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

82 83
- Based RBAC, User-Role-Resource.    
- We treat API requests as a resource, resource format like `requestUri===httpMethod`.   
84 85
  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 已提交
86
- User belongs some Role -- Role owns Resource -- User can access the resource.  
sinat_25235033's avatar
sinat_25235033 已提交
87

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

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

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

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

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

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

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

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

123 124
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 已提交
125

sinat_25235033's avatar
sinat_25235033 已提交
126 127
If the configuration resource data comes from text, please refer to  [Sureness integration Spring Boot 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 Spring Boot sample(database scheme)](https://github.com/tomsun28/sureness/tree/master/sample-tom)   
sinat_25235033's avatar
sinat_25235033 已提交
128 129


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

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

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

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

142 143 144 145 146 147
- 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 已提交
148 149

```
sinat_25235033's avatar
sinat_25235033 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162
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 已提交
163 164
```

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

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

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

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

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

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

177
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 已提交
178

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

181 182 183 184 185
- `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 已提交
186

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

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

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

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

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

`Implment SubjectCreate to create your custom subject`   

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

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

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

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

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

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

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

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

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

sinat_25235033's avatar
sinat_25235033 已提交
221
Very welcome to Contribute this project, go further and better with Sureness. 
sinat_25235033's avatar
sinat_25235033 已提交
222

223
Components of Repository:  
sinat_25235033's avatar
sinat_25235033 已提交
224 225 226 227
- [Sureness's kernel code--Sureness-core](core)  
- [Sureness integration Spring Boot sample(configuration file scheme)--sample-bootstrap](sample-bootstrap)  
- [Sureness integration Spring Boot sample(database scheme)-sample-tom](sample-tom)  
- [Sample projects using Sureness in each framework(Javalin,Ktor,Quarkus)--samples](samples)  
sinat_25235033's avatar
sinat_25235033 已提交
228

sinat_25235033's avatar
sinat_25235033 已提交
229
See [CONTRIBUTING](CONTRIBUTING.md)    
sinat_25235033's avatar
sinat_25235033 已提交
230

231
#### 💪 Why Is High Performance  
sinat_25235033's avatar
sinat_25235033 已提交
232

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

235 236
## 🌞 Friend's Links   

sinat_25235033's avatar
sinat_25235033 已提交
237
* **```HertzBeat```** An open-source, real-time monitoring system with custom-monitor and agentLess: [Github](https://github.com/dromara/hertzbeat)   
238 239 240 241
* **```JustAuth```** A Java library of third-party authorized login: [Github](https://github.com/justauth/JustAuth)    
* **```MaxKey```** Leading-Edge Enterprise-Class open source IAM Identity and Access management product: [Github](https://github.com/dromara/MaxKey)   
* **```PhalApi```** PHP Api Framework: [Website](https://www.phalapi.net/)    

sinat_25235033's avatar
sinat_25235033 已提交
242
## 💬 Join discussion    
243

sinat_25235033's avatar
sinat_25235033 已提交
244 245 246
QQ Group: 390083213   
[Github Discussion](https://github.com/dromara/sureness/discussions)          
[Gitter Channel](https://gitter.im/dromara/sureness)   
sinat_25235033's avatar
sinat_25235033 已提交
247 248 249 250 251 252

<img alt="tan-cloud" src="https://cdn.jsdelivr.net/gh/dromara/hertzbeat/home/static/img/wechat.png" width="400"/>       

<br/>

<img alt="planet" src="https://cdn.jsdelivr.net/gh/dromara/hertzbeat@gh-pages/img/planet.jpg" width="400"/>    
253 254


sinat_25235033's avatar
sinat_25235033 已提交
255

256
## 🛡️ License  
Y
yqxxgh 已提交
257
[`Apache License, Version 2.0`](https://www.apache.org/licenses/LICENSE-2.0.html)