├── .gitattributes
├── .gitignore
├── README.md
├── pom.xml
└── src
└── main
├── java
└── com
│ └── scienjus
│ ├── controller
│ └── TokenController.java
│ ├── domain
│ └── User.java
│ ├── model
│ ├── ResultModel.java
│ └── ResultStatus.java
│ └── repository
│ └── UserRepository.java
├── resources
├── config.properties
└── spring.xml
└── webapp
├── WEB-INF
└── web.xml
└── index.jsp
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 |
7 | # Standard to msysgit
8 | *.doc diff=astextplain
9 | *.DOC diff=astextplain
10 | *.docx diff=astextplain
11 | *.DOCX diff=astextplain
12 | *.dot diff=astextplain
13 | *.DOT diff=astextplain
14 | *.pdf diff=astextplain
15 | *.PDF diff=astextplain
16 | *.rtf diff=astextplain
17 | *.RTF diff=astextplain
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Windows image file caches
2 | Thumbs.db
3 | ehthumbs.db
4 |
5 | # Folder config file
6 | Desktop.ini
7 |
8 | # Recycle Bin used on file shares
9 | $RECYCLE.BIN/
10 |
11 | # Windows Installer files
12 | *.cab
13 | *.msi
14 | *.msm
15 | *.msp
16 |
17 | # Windows shortcuts
18 | *.lnk
19 |
20 | # =========================
21 | # Operating System Files
22 | # =========================
23 |
24 | # OSX
25 | # =========================
26 |
27 | .DS_Store
28 | .AppleDouble
29 | .LSOverride
30 |
31 | # Thumbnails
32 | ._*
33 |
34 | # Files that might appear on external disk
35 | .Spotlight-V100
36 | .Trashes
37 |
38 | # Directories potentially created on remote AFP share
39 | .AppleDB
40 | .AppleDesktop
41 | Network Trash Folder
42 | Temporary Items
43 | .apdisk
44 | SpringAuthorizationManagerDemo.iml
45 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # spring-authorization-manager-demo
2 | https://github.com/ScienJus/spring-authorization-manager 的Demo
3 |
4 | 使用方法:
5 |
6 | 需要首先下载`spring-authorization-manager`,将这个jar添加到你的maven私服或本地仓库。
7 |
8 | 下载该项目,并运行。
9 |
10 | ###演示正常情况
11 |
12 | 输入用户名,点击登录,获取 Token。
13 |
14 | 输入 Token,点击查看登录用户名,会显示刚才输入的用户名。
15 |
16 | 输入 Token,点击退出登录,会显示退出登录成功。
17 |
18 | 输入 Token,点击查看登录用户名,会显示未登录。
19 |
20 | 输入 Token,点击退出登录,会显示未登录。
21 |
22 | ###演示多用户登录同一账号
23 |
24 | 输入用户名,点击登录,获取 Token1。
25 |
26 | 输入同一用户名,点击登录,获取 Token2。
27 |
28 | 输入 Token2,点击查看登录用户名,会显示刚才输入的用户名。
29 |
30 | 输入 Token1,点击查看登录用户名,会显示未登录。
31 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.scienjus
5 | spring-authorization-manager-demo
6 | war
7 | 1.0-SNAPSHOT
8 | spring-authorization-manager-demo
9 | http://maven.apache.org
10 |
11 |
12 |
13 | com.scienjus
14 | spring-authorization-manager
15 | 1.0-SNAPSHOT
16 |
17 |
18 |
19 | org.springframework
20 | spring-webmvc
21 | 4.2.2.RELEASE
22 |
23 |
24 | org.springframework
25 | spring-context
26 | 4.2.2.RELEASE
27 |
28 |
29 | com.fasterxml.jackson.core
30 | jackson-core
31 | 2.6.3
32 |
33 |
34 | com.fasterxml.jackson.core
35 | jackson-databind
36 | 2.6.3
37 |
38 |
39 | javax.servlet
40 | javax.servlet-api
41 | 3.1.0
42 |
43 |
44 |
45 | redis.clients
46 | jedis
47 | 2.7.3
48 |
49 |
50 |
51 |
52 | ${project.artifactId}
53 |
54 |
55 | org.apache.maven.plugins
56 | maven-compiler-plugin
57 |
58 | 1.7
59 | 1.7
60 |
61 |
62 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/src/main/java/com/scienjus/controller/TokenController.java:
--------------------------------------------------------------------------------
1 | package com.scienjus.controller;
2 |
3 | import com.scienjus.authorization.annotation.Authorization;
4 | import com.scienjus.authorization.annotation.CurrentUser;
5 | import com.scienjus.authorization.manager.TokenManager;
6 | import com.scienjus.domain.User;
7 | import com.scienjus.model.ResultModel;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.http.HttpStatus;
10 | import org.springframework.http.ResponseEntity;
11 | import org.springframework.web.bind.annotation.RequestMapping;
12 | import org.springframework.web.bind.annotation.RequestMethod;
13 | import org.springframework.web.bind.annotation.RequestParam;
14 | import org.springframework.web.bind.annotation.RestController;
15 |
16 | import java.util.UUID;
17 |
18 | /**
19 | * 获取和删除token的请求地址,在Restful设计中其实就对应着登录和退出登录的资源映射
20 | * @author ScienJus
21 | * @date 2015/7/30.
22 | */
23 | @RestController
24 | @RequestMapping("/tokens")
25 | public class TokenController {
26 |
27 | @Autowired
28 | private TokenManager tokenManager;
29 |
30 | @RequestMapping(method = RequestMethod.POST)
31 | public ResponseEntity createToken(@RequestParam String username) {
32 | //生成一个token
33 | String token = UUID.randomUUID().toString();
34 | tokenManager.createRelationship(username, token);
35 | return new ResponseEntity<>(ResultModel.ok(token), HttpStatus.OK);
36 | }
37 |
38 | @RequestMapping(method = RequestMethod.GET)
39 | @Authorization
40 | public ResponseEntity getUsername(@CurrentUser User user) {
41 | //生成一个token
42 | return new ResponseEntity<>(ResultModel.ok(user.getUsername()), HttpStatus.OK);
43 | }
44 |
45 | @RequestMapping(method = RequestMethod.DELETE)
46 | @Authorization
47 | public ResponseEntity logout(@CurrentUser User user) {
48 | tokenManager.delRelationshipByKey(user.getUsername());
49 | return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/scienjus/domain/User.java:
--------------------------------------------------------------------------------
1 | package com.scienjus.domain;
2 |
3 | /**
4 | * 用户
5 | * @author ScienJus
6 | * @date 2015/7/31.
7 | */
8 | public class User {
9 | //用户名
10 | private String username;
11 |
12 | public String getUsername() {
13 | return username;
14 | }
15 |
16 | public void setUsername(String username) {
17 | this.username = username;
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/scienjus/model/ResultModel.java:
--------------------------------------------------------------------------------
1 | package com.scienjus.model;
2 |
3 | /**
4 | * 自定义返回结果
5 | * @author XieEnlong
6 | * @date 2015/7/14.
7 | */
8 | public class ResultModel {
9 |
10 | /**
11 | * 返回码
12 | */
13 | private int code;
14 |
15 | /**
16 | * 返回结果描述
17 | */
18 | private String message;
19 |
20 | /**
21 | * 返回内容
22 | */
23 | private Object content;
24 |
25 | public int getCode() {
26 | return code;
27 | }
28 |
29 | public void setCode(int code) {
30 | this.code = code;
31 | }
32 |
33 | public String getMessage() {
34 | return message;
35 | }
36 |
37 | public void setMessage(String message) {
38 | this.message = message;
39 | }
40 |
41 | public Object getContent() {
42 | return content;
43 | }
44 |
45 | public void setContent(Object content) {
46 | this.content = content;
47 | }
48 |
49 | public ResultModel(ResultStatus status) {
50 | this.code = status.getCode();
51 | this.message = status.getMessage();
52 | this.content = "";
53 | }
54 |
55 | public ResultModel(ResultStatus status, Object content) {
56 | this.code = status.getCode();
57 | this.message = status.getMessage();
58 | this.content = content;
59 | }
60 |
61 | public static ResultModel ok(Object content) {
62 | return new ResultModel(ResultStatus.SUCCESS, content);
63 | }
64 |
65 | public static ResultModel ok() {
66 | return new ResultModel(ResultStatus.SUCCESS);
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/com/scienjus/model/ResultStatus.java:
--------------------------------------------------------------------------------
1 | package com.scienjus.model;
2 |
3 | /**
4 | * 自定义请求状态码
5 | * @author ScienJus
6 | * @date 2015/7/15.
7 | */
8 | public enum ResultStatus {
9 | SUCCESS(100, "成功");
10 |
11 | /**
12 | * 返回码
13 | */
14 | private int code;
15 |
16 | /**
17 | * 返回结果描述
18 | */
19 | private String message;
20 |
21 | ResultStatus(int code, String message) {
22 | this.code = code;
23 | this.message = message;
24 | }
25 |
26 | public int getCode() {
27 | return code;
28 | }
29 |
30 | public String getMessage() {
31 | return message;
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/scienjus/repository/UserRepository.java:
--------------------------------------------------------------------------------
1 | package com.scienjus.repository;
2 |
3 | import com.scienjus.authorization.repository.UserModelRepository;
4 | import com.scienjus.domain.User;
5 |
6 | /**
7 | * @author XieEnlong
8 | * @date 2015/10/26.
9 | */
10 | public class UserRepository implements UserModelRepository {
11 | @Override
12 | public Object getCurrentUser(String key) {
13 | User user = new User();
14 | user.setUsername(key);
15 | return user;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/resources/config.properties:
--------------------------------------------------------------------------------
1 | redis.host = 192.168.1.222
2 | redis.port = 6379
3 | redis.timeout = 100000
4 | redis.password = 123
5 |
--------------------------------------------------------------------------------
/src/main/resources/spring.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | classpath:config.properties
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 | Archetype Created Web Application
7 |
8 |
9 | contextConfigLocation
10 | classpath:spring.xml
11 |
12 |
13 |
14 | org.springframework.web.context.ContextLoaderListener
15 |
16 |
17 |
18 | demo
19 | org.springframework.web.servlet.DispatcherServlet
20 |
21 | contextConfigLocation
22 | classpath:spring.xml
23 |
24 |
25 |
26 |
27 | demo
28 | /
29 |
30 |
31 |
32 | index.jsp
33 | index.html
34 |
35 |
36 |
--------------------------------------------------------------------------------
/src/main/webapp/index.jsp:
--------------------------------------------------------------------------------
1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2 |
3 |
4 | Spring Authorization Manager Demo
5 |
6 |
7 |
8 |
9 | 用户名:
10 | 令牌:
11 |
12 |
13 |
14 |
15 | 结果:
16 |
17 |
18 | 演示方式:
19 |
20 | - 点击[查看登录用户名]按钮,提示"未登录"。
21 | - 在[用户名]输入框中填入用户名,点击[登录]按钮(用户名可随意填写),得到令牌。
22 | - 在[令牌]输入框中填入刚才获得的令牌,点击[查看登录用户名]按钮,会出现刚才输入的用户名。
23 | - 在[令牌]输入框中填入刚才获得的令牌,点击[退出登录]按钮,提示"退出登录成功"。
24 | - 在[令牌]输入框中填入刚才获得的令牌,点击[查看登录用户名]按钮,提示"未登录"。
25 |
26 |
27 |
28 |
89 |
90 |
--------------------------------------------------------------------------------