README.md 9.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 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
## <font color="green">`Introduction`</font>
sinat_25235033's avatar
sinat_25235033 已提交
20

sinat_25235033's avatar
sinat_25235033 已提交
21

sinat_25235033's avatar
sinat_25235033 已提交
22 23 24 25 26 27 28 29
> 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.    
> [High performance due dictionary matching tree](#Why Is High Performance ).      
> Good extension interface, demo and document.    
sinat_25235033's avatar
sinat_25235033 已提交
30

31
> 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 已提交
32

sinat_25235033's avatar
sinat_25235033 已提交
33 34 35 36 37 38 39 40 41 42 43
##### 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 已提交
44 45
| **Performance** | extremely fast | faster | slower|
| **Learning curve** | simple | simple | steep|
46 47 48 49 50 51 52 53 54

##### Benchmark  

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

**Benchmark tests show that sureness basically does not consume performance compared with frameless applications, 
and the performance (TPS) is twice that of shiro and 170 times that of spring security.**    

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

sinat_25235033's avatar
sinat_25235033 已提交
56 57
##### Framework Sample Support  

sinat_25235033's avatar
sinat_25235033 已提交
58 59 60 61 62 63
- [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)   
64
- [x] more samples todo  
sinat_25235033's avatar
sinat_25235033 已提交
65

66
## Quick Start 
sinat_25235033's avatar
sinat_25235033 已提交
67

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

sinat_25235033's avatar
sinat_25235033 已提交
70
- Based RBAC, only has role-resource, no permission action.    
71 72 73
- 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 已提交
74
- User belongs some Role -- Role owns Resource -- User can access the resource.  
sinat_25235033's avatar
sinat_25235033 已提交
75

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

sinat_25235033's avatar
sinat_25235033 已提交
78
#### Add sureness In Your Project  
sinat_25235033's avatar
sinat_25235033 已提交
79

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

#### Use the default configuration to configure sureness  

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

sinat_25235033's avatar
sinat_25235033 已提交
103 104 105 106 107
#### Load Auth Config DataSource   

Sureness need dataSource to authenticate and authorize, eg: role data, user data etc.  
The dataSource can load from txt, dataBase, no dataBase or annotation etc.  
We provide interfaces `SurenessAccountProvider`, `PathTreeProvider` for user implement to load data from the dataSource where they want.  
sinat_25235033's avatar
sinat_25235033 已提交
108 109
`SurenessAccountProvider` - Account datasource provider interface.    
`PathTreeProvider` - Resource uri-role datasource provider interface.     
sinat_25235033's avatar
sinat_25235033 已提交
110 111 112 113 114 115 116

We provide default dataSource implement which load dataSource from txt(sureness.yml), user can defined their data in sureness.yml.   
We also provider dataSource implement which load dataSource form annotation - `AnnotationLoader`.   

Default Document DataSource Config - sureness.yml, see: [Default Document DataSource](docs/default-datasource.md)   
Annotation DataSource Config Detail, see: [Annotation DataSource](docs/annotation-datasource.md)  

sinat_25235033's avatar
sinat_25235033 已提交
117 118
If the configuration resource data comes from text, please refer to  [sureness integration springboot sample(configuration file scheme)--sample-bootstrap](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)-sample-tom](https://github.com/tomsun28/sureness/tree/master/sample-tom)   
sinat_25235033's avatar
sinat_25235033 已提交
119 120


sinat_25235033's avatar
sinat_25235033 已提交
121
#### Add an Interceptor Intercepting All Requests  
sinat_25235033's avatar
sinat_25235033 已提交
122

sinat_25235033's avatar
sinat_25235033 已提交
123 124
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 已提交
125
```
sinat_25235033's avatar
sinat_25235033 已提交
126
SubjectSum subject = SurenessSecurityManager.getInstance().checkIn(servletRequest)
sinat_25235033's avatar
sinat_25235033 已提交
127 128
```

sinat_25235033's avatar
sinat_25235033 已提交
129 130 131
#### Implement Auth Exception Handling Process    

`sureness` uses exception handling process:  
sinat_25235033's avatar
sinat_25235033 已提交
132 133 134
1. If auth success, method - `checkIn` will return a `SubjectSum` object containing user information.    
2. If auth failure, method - `checkIn` will throw different types of auth exceptions.   
Users need to continue the subsequent process based on these exceptions.(like return the request response)  
sinat_25235033's avatar
sinat_25235033 已提交
135 136 137

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 已提交
138 139

```
sinat_25235033's avatar
sinat_25235033 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152
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 已提交
153 154
```

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

157
**Have Fun**      
sinat_25235033's avatar
sinat_25235033 已提交
158

159
## Advanced Use  
sinat_25235033's avatar
sinat_25235033 已提交
160

sinat_25235033's avatar
sinat_25235033 已提交
161
If know sureness Process flow, maybe know these extend points.      
sinat_25235033's avatar
sinat_25235033 已提交
162

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

165
Suggest look these interface before extending:  
sinat_25235033's avatar
sinat_25235033 已提交
166

167
- `Subject`:  Authenticated authorized  user's account interface, provide the account's username,password, request resources, roles, etc.  
sinat_25235033's avatar
sinat_25235033 已提交
168 169 170 171
- `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 已提交
172

173
Sureness Process Flow: 
sinat_25235033's avatar
sinat_25235033 已提交
174

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

177
1. **Custom Datasource**  
sinat_25235033's avatar
sinat_25235033 已提交
178

179 180
`Implment PathTreeProvider, load in DefaultPathRoleMatcher`   
`Implment SurenessAccountProvide, load in processor`  
sinat_25235033's avatar
sinat_25235033 已提交
181

182
2. **Custom Subject**  
sinat_25235033's avatar
sinat_25235033 已提交
183

184 185 186
`Implment Subject, add custom subject content`  
`Implment SubjectCreate to create custom subject`  
`Implment Processor to support custom subject`    
sinat_25235033's avatar
sinat_25235033 已提交
187

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

190 191
`A subject also can support by different processor, so we can custom processor to support custom subject`
`Implment Processor, set which subject can support and implment processing details`  
sinat_25235033's avatar
sinat_25235033 已提交
192

sinat_25235033's avatar
sinat_25235033 已提交
193
Detail please refer to  [sureness integration springboot sample(database scheme)-sample-tom](sample-tom)   
sinat_25235033's avatar
sinat_25235033 已提交
194

195
## Contributing  
sinat_25235033's avatar
sinat_25235033 已提交
196

sinat_25235033's avatar
sinat_25235033 已提交
197
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 已提交
198

199 200
Components of Repository:  
- [sureness's kernel code--sureness-core](core)  
sinat_25235033's avatar
sinat_25235033 已提交
201 202
- [sureness integration springboot sample(configuration file scheme)--sample-bootstrap](sample-bootstrap)  
- [sureness integration springboot sample(database scheme)-sample-tom](sample-tom)  
203
- [Sample projects using sureness in each framework(javalin,ktor,quarkus)--samples](samples)  
sinat_25235033's avatar
sinat_25235033 已提交
204 205


sinat_25235033's avatar
sinat_25235033 已提交
206
#### Why Is High Performance  
sinat_25235033's avatar
sinat_25235033 已提交
207

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

sinat_25235033's avatar
sinat_25235033 已提交
210

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