README_EN.md 5.5 KB
Newer Older
sinat_25235033's avatar
sinat_25235033 已提交
1 2 3 4
# `sureness`  

[![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 已提交
5 6
[中文文档](README.md)  

sinat_25235033's avatar
sinat_25235033 已提交
7 8 9 10 11 12
## <font color="green">`Introduction`</font>


> Sureness is a new, permission project which author learn from apache shiro and add some ideas to cretae it  
> Authentication for restful api, based on RABC, Mainly focused on the protection of restful api  
> Native supports  restful api, websocket's protection  
sinat_25235033's avatar
sinat_25235033 已提交
13
> Native supports dynamic permissions  
sinat_25235033's avatar
sinat_25235033 已提交
14
> Native supports JWT, Basic Auth... Can extend custom supported authentication methods  
sinat_25235033's avatar
sinat_25235033 已提交
15
> [High performance due dictionary matching tree](#Why Is High Performance)  
sinat_25235033's avatar
sinat_25235033 已提交
16 17 18 19 20 21 22 23 24 25
> Sorry about google english.   

### Components of Repository:  
- [sureness's kernel code--sureness-core](core)  
- [10 Minute Tutorial's Program--sample-bootstrap](sample-bootstrap)  
- [30 Minute Tutorial's Program--sample-tom](sample-tom)  

### <font color="red">Some Conventions</font>  

- Based RABC, but only has role-resource, no permission action    
sinat_25235033's avatar
sinat_25235033 已提交
26
- We treat restful requests as a resource, resource format like `requestUri===httpMethod`.   
sinat_25235033's avatar
sinat_25235033 已提交
27 28
  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 已提交
29
- User belong some Role -- Role owns Resource -- User can access the resource  
sinat_25235033's avatar
sinat_25235033 已提交
30 31 32 33 34 35 36 37 38

### Use  

`maven`  
```
<!-- https://mvnrepository.com/artifact/com.usthe.sureness/sureness-core -->
<dependency>
    <groupId>com.usthe.sureness</groupId>
    <artifactId>sureness-core</artifactId>
39
    <version>0.0.2.4</version>
sinat_25235033's avatar
sinat_25235033 已提交
40 41 42 43 44
</dependency>
```

`gradle`  
```
45
compile group: 'com.usthe.sureness', name: 'sureness-core', version: '0.0.2.4'
sinat_25235033's avatar
sinat_25235033 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
```

Portal, always is a filter intercepting all requests:  
```
SurenessSecurityManager.getInstance().checkIn(servletRequest)
```

Authentication passed directly, failure throw exception, catch exception and do something:   

```
        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 (RuntimeException e) {
            // other sureness exception
        }
```

sureness exception                              | exception note
---                                                             | ---
SurenessAuthenticationException     |  basic authenticated exception,Authentication related extend it
SurenessAuthorizationException       | basic authorized exception,Authorization related extend it
ProcessorNotFoundException            | authenticated,not found process support this subject
UnknownAccountException                | authenticated,unknown account
UnSupportedSubjectException           | authenticated,unsupport request
DisabledAccountException                  | authenticated,account disable
ExcessiveAttemptsException                | authenticated,excessive attempts
IncorrectCredentialsException             | authenticated, incorrect credential
ExpiredCredentialsException               | authenticated,expired credential
UnauthorizedException                        | authorized,no premission access this resource

custom exception should extend SurenessAuthenticationException or SurenessAuthorizationException  

If the configuration resource data comes from text, please refer to  [10 Minute Tutorial's Program--sample-bootstrap](sample-bootstrap)   
If the configuration resource data comes from database, please refer to  [30 Minute Tutorial's Program--sample-tom](sample-tom)   

Have Fun   


### Advanced Use  

if konw sureness [Process flow](#Process flow), maybe konw the extend point  

sureness support custom subject, custom subjectCreator, custom processor and more.  

sugest look these interface before extending:  

- `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 subejct 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   


1. **custom datasource**  

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

2. **custom subject**  

`implment Subject, add custom subject content`  
`implment SubjectCreate to create custom subject`  
`implment Processor to support custom subject`    

3. **custom processor**  

`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`  

Detail please refer to  [30 Minute Tutorial's Program--sample-tom](sample-tom)   

125 126 127 128
### Why Is High Performance  

![pathRoleMatcher](/img/PathRoleMatcher.svg)  

sinat_25235033's avatar
sinat_25235033 已提交
129 130 131 132 133 134
### Process flow  

![sureness-core](/img/sureness-core.svg)  

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