Skip to content

Commit 1e57856

Browse files
author
Xiong Neng
committed
完成springboot-jwt升级重构
1 parent 7b5e72a commit 1e57856

File tree

25 files changed

+80
-725
lines changed

25 files changed

+80
-725
lines changed

springboot-jwt/README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Table of Contents
2+
3+
* [简介](#简介)
4+
* [测试](#测试)
5+
* [许可证](#许可证)
6+
17

28
## 简介
39

@@ -30,10 +36,19 @@ Content-Type: application/json
3036
``` json
3137
{
3238
"username": "admin",
33-
"password": "12345678",
34-
"appid": "111",
35-
"imei": "imei"
39+
"password": "12345678"
40+
}
41+
```
42+
43+
可使用postman或者curl方式,本人更愿意使用curl方式:
44+
45+
```
46+
curl -X POST http://localhost:9095/login -H 'Content-Type: application/json' -d '
47+
{
48+
"username": "admin",
49+
"password": "12345678"
3650
}
51+
'
3752
```
3853

3954
返回值:
@@ -63,6 +78,12 @@ Content-Type: application/json
6378
Authorization: "上面拿到的token值"
6479
```
6580

81+
curl访问语法:
82+
83+
```
84+
curl -X GET http://localhost:9095/api/v1/join?imei=imei -H 'Content-Type: application/json' -H 'Authorization: 上面拿到的token值'
85+
```
86+
6687
## 许可证
6788

6889
Copyright (c) 2018 Xiong Neng

springboot-jwt/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>com.auth0</groupId>
4444
<artifactId>java-jwt</artifactId>
45-
<version>3.3.0</version>
45+
<version>3.4.0</version>
4646
</dependency>
4747
<dependency>
4848
<groupId>org.springframework.boot</groupId>
@@ -85,6 +85,7 @@
8585
<artifactId>commons-lang3</artifactId>
8686
<version>3.7</version>
8787
</dependency>
88+
8889
</dependencies>
8990

9091
<build>

springboot-jwt/src/main/java/com/xncoding/jwt/api/LoginController.java

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import com.xncoding.jwt.api.model.BaseResponse;
44
import com.xncoding.jwt.api.model.LoginParam;
55
import com.xncoding.jwt.common.util.JWTUtil;
6-
import com.xncoding.jwt.dao.entity.ManagerInfo;
6+
import com.xncoding.jwt.model.ManagerInfo;
77
import com.xncoding.jwt.service.ManagerInfoService;
88
import com.xncoding.jwt.shiro.ShiroKit;
9-
import org.apache.commons.lang3.StringUtils;
109
import org.apache.shiro.authz.UnauthorizedException;
1110
import org.slf4j.Logger;
1211
import org.slf4j.LoggerFactory;
13-
import org.springframework.http.HttpStatus;
1412
import org.springframework.web.bind.annotation.*;
1513

1614
import javax.annotation.Resource;
@@ -32,41 +30,16 @@ public BaseResponse<String> login(@RequestHeader(name="Content-Type", defaultVal
3230
_logger.info("用户请求登录获取Token");
3331
String username = loginParam.getUsername();
3432
String password = loginParam.getPassword();
35-
String appid = loginParam.getAppid();
36-
String imei = loginParam.getImei();
3733
ManagerInfo user = managerInfoService.findByUsername(username);
38-
//盐(用户名+随机数)
34+
//随机数盐
3935
String salt = user.getSalt();
40-
//原密码
36+
//原密码加密(通过username + salt作为盐)
4137
String encodedPassword = ShiroKit.md5(password, username + salt);
4238
if (user.getPassword().equals(encodedPassword)) {
43-
if (StringUtils.isNotEmpty(appid) && StringUtils.isNotEmpty(imei)) {
44-
return new BaseResponse<>(true, "Login success", JWTUtil.signSocket(username, encodedPassword, appid, imei));
45-
}
4639
return new BaseResponse<>(true, "Login success", JWTUtil.sign(username, encodedPassword));
4740
} else {
4841
throw new UnauthorizedException();
4942
}
5043
}
5144

52-
@PostMapping("/notifyLogin")
53-
public BaseResponse<String> notifyLogin(@RequestHeader(name="Content-Type", defaultValue = "application/json") String contentType,
54-
@RequestBody LoginParam loginParam) {
55-
_logger.info("登录用户推送请求登录获取Token");
56-
String username = loginParam.getUsername();
57-
String password = loginParam.getPassword();
58-
ManagerInfo user = managerInfoService.findByUsername(username);
59-
if (user.getPassword().equals(password)) {
60-
return new BaseResponse<>(true, "Login success", JWTUtil.sign(username, password));
61-
} else {
62-
throw new UnauthorizedException();
63-
}
64-
}
65-
66-
@GetMapping(path = "/401")
67-
@ResponseStatus(HttpStatus.UNAUTHORIZED)
68-
public BaseResponse unauthorized() {
69-
return new BaseResponse<>(false, "Unauthorized", null);
70-
}
71-
7245
}

springboot-jwt/src/main/java/com/xncoding/jwt/api/PublicController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public class PublicController {
2222
*/
2323
@RequestMapping(value = "/join", method = RequestMethod.GET)
2424
@RequiresAuthentication
25-
public BaseResponse join(@RequestHeader("Authorization") String token,
26-
@RequestParam("imei") String imei) {
25+
public BaseResponse join(@RequestParam("imei") String imei) {
2726
_logger.info("入网查询接口 start... imei=" + imei);
2827
BaseResponse result = new BaseResponse();
2928
result.setSuccess(true);

springboot-jwt/src/main/java/com/xncoding/jwt/api/model/LoginParam.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ public class LoginParam {
1616
* 密码
1717
*/
1818
private String password;
19-
/**
20-
* Application ID
21-
*/
22-
private String appid;
23-
/**
24-
* IMEI码
25-
*/
26-
private String imei;
2719

2820
public String getUsername() {
2921
return username;
@@ -40,20 +32,4 @@ public String getPassword() {
4032
public void setPassword(String password) {
4133
this.password = password;
4234
}
43-
44-
public String getAppid() {
45-
return appid;
46-
}
47-
48-
public void setAppid(String appid) {
49-
this.appid = appid;
50-
}
51-
52-
public String getImei() {
53-
return imei;
54-
}
55-
56-
public void setImei(String imei) {
57-
this.imei = imei;
58-
}
5935
}

springboot-jwt/src/main/java/com/xncoding/jwt/common/constant/ConstantsList.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

springboot-jwt/src/main/java/com/xncoding/jwt/common/constant/DictMap.java

Lines changed: 0 additions & 80 deletions
This file was deleted.

springboot-jwt/src/main/java/com/xncoding/jwt/common/util/CommonUtil.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)