Skip to content

Commit ccee016

Browse files
committed
fix: 前台登录接口增加登录限制
1 parent 5a9315b commit ccee016

File tree

9 files changed

+167
-97
lines changed

9 files changed

+167
-97
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.moxi.mougblog.base.enums;
2+
3+
/**
4+
* 网站登录方式枚举类
5+
*
6+
* @Author: 陌溪
7+
* @Date: 2020年10月14日11:10:04
8+
*/
9+
public enum ELoginType {
10+
11+
/**
12+
* 账号密码
13+
*/
14+
PASSWORD("1", "PASSWORD"),
15+
16+
/**
17+
* 码云
18+
*/
19+
GITEE("2", "GITEE"),
20+
21+
/**
22+
* GITHUB
23+
*/
24+
GITHUB("3", "GITHUB"),
25+
26+
/**
27+
* QQ
28+
*/
29+
QQ("4", "QQ"),
30+
31+
/**
32+
* Gitee
33+
*/
34+
WECHAT("5", "WECHAT");
35+
36+
37+
private final String code;
38+
private final String name;
39+
40+
ELoginType(String code, String name) {
41+
this.code = code;
42+
this.name = name;
43+
}
44+
45+
public String getCode() {
46+
return code;
47+
}
48+
49+
public String getName() {
50+
return name;
51+
}
52+
}

mogu_base/src/main/java/com/moxi/mougblog/base/global/BaseRedisConf.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,15 @@ public class BaseRedisConf {
163163
*/
164164
public final static String BLOG_CONTRIBUTE_COUNT = "BLOG_CONTRIBUTE_COUNT";
165165

166+
/**
167+
* 登录方式
168+
*/
169+
public final static String LOGIN_TYPE = "LOGIN_TYPE";
170+
public final static String GITEE = "GITEE";
171+
public final static String GITHUB = "GITHUB";
172+
public final static String QQ = "QQ";
173+
public final static String PASSWORD = "PASSWORD";
174+
public final static String WECHAT = "WECHAT";
175+
166176

167177
}

mogu_web/src/main/java/com/moxi/mogublog/web/restapi/AuthRestApi.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,12 @@
1616
import com.moxi.mogublog.web.global.SQLConf;
1717
import com.moxi.mogublog.web.global.SysConf;
1818
import com.moxi.mogublog.web.utils.RabbitMqUtil;
19-
import com.moxi.mogublog.xo.service.FeedbackService;
20-
import com.moxi.mogublog.xo.service.LinkService;
21-
import com.moxi.mogublog.xo.service.SystemConfigService;
22-
import com.moxi.mogublog.xo.service.UserService;
19+
import com.moxi.mogublog.xo.service.*;
2320
import com.moxi.mogublog.xo.utils.WebUtil;
2421
import com.moxi.mogublog.xo.vo.FeedbackVO;
2522
import com.moxi.mogublog.xo.vo.LinkVO;
2623
import com.moxi.mogublog.xo.vo.UserVO;
27-
import com.moxi.mougblog.base.enums.EGender;
28-
import com.moxi.mougblog.base.enums.ELinkStatus;
29-
import com.moxi.mougblog.base.enums.EOpenStatus;
30-
import com.moxi.mougblog.base.enums.EStatus;
24+
import com.moxi.mougblog.base.enums.*;
3125
import com.moxi.mougblog.base.exception.ThrowableUtils;
3226
import com.moxi.mougblog.base.global.Constants;
3327
import com.moxi.mougblog.base.validator.group.Insert;
@@ -52,6 +46,7 @@
5246
import org.springframework.validation.annotation.Validated;
5347
import org.springframework.web.bind.annotation.*;
5448

49+
import javax.annotation.Resource;
5550
import javax.servlet.http.HttpServletRequest;
5651
import javax.servlet.http.HttpServletResponse;
5752
import java.io.IOException;
@@ -74,6 +69,8 @@ public class AuthRestApi {
7469
@Autowired
7570
private SystemConfigService systemConfigService;
7671
@Autowired
72+
private WebConfigService webConfigService;
73+
@Autowired
7774
private FeedbackService feedbackService;
7875
@Autowired
7976
private LinkService linkService;
@@ -112,12 +109,17 @@ public class AuthRestApi {
112109

113110
@Autowired
114111
private StringRedisTemplate stringRedisTemplate;
115-
@Autowired
112+
@Resource
116113
private PictureFeignClient pictureFeignClient;
117114

118115
@ApiOperation(value = "获取认证", notes = "获取认证")
119116
@RequestMapping("/render")
120-
public String renderAuth(String source, HttpServletResponse response) throws IOException {
117+
public String renderAuth(String source) {
118+
// 将传递过来的转换成大写
119+
Boolean isOpenLoginType = webConfigService.isOpenLoginType(source.toUpperCase());
120+
if (!isOpenLoginType){
121+
return ResultUtil.result(SysConf.ERROR, "后台未开启该登录方式!");
122+
}
121123
log.info("进入render:" + source);
122124
AuthRequest authRequest = getAuthRequest(source);
123125
String token = AuthStateUtils.createState();
@@ -132,7 +134,7 @@ public String renderAuth(String source, HttpServletResponse response) throws IOE
132134
* oauth平台中配置的授权回调地址,以本项目为例,在创建gitee授权应用时的回调地址应为:http://127.0.0.1:8603/oauth/callback/gitee
133135
*/
134136
@RequestMapping("/callback/{source}")
135-
public void login(@PathVariable("source") String source, AuthCallback callback, HttpServletRequest request, HttpServletResponse httpServletResponse) throws IOException {
137+
public void login(@PathVariable("source") String source, AuthCallback callback, HttpServletResponse httpServletResponse) throws IOException {
136138
log.info("进入callback:" + source + " callback params:" + JSONObject.toJSONString(callback));
137139
AuthRequest authRequest = getAuthRequest(source);
138140
AuthResponse response = authRequest.login(callback);
@@ -218,36 +220,27 @@ public void login(@PathVariable("source") String source, AuthCallback callback,
218220
} else {
219221
user.setLoginCount(user.getLoginCount() + 1);
220222
}
221-
222223
// 获取浏览器,IP来源,以及操作系统
223224
user = userService.serRequestInfo(user);
224-
225225
// 暂时将token也存入到user表中,为了以后方便更新redis中的内容
226226
user.setValidCode(accessToken);
227-
228227
if (exist) {
229228
user.updateById();
230229
} else {
231-
232230
user.setUuid(data.get(SysConf.UUID).toString());
233231
user.setSource(data.get(SysConf.SOURCE).toString());
234-
235232
String userName = PROJECT_NAME_EN.concat("_").concat(user.getSource()).concat("_").concat(user.getUuid());
236233
user.setUserName(userName);
237-
238234
// 如果昵称为空,那么直接设置用户名
239235
if (StringUtils.isEmpty(user.getNickName())) {
240236
user.setNickName(userName);
241237
}
242-
243238
// 默认密码
244239
user.setPassWord(MD5Utils.string2MD5(DEFAULE_PWD));
245240
user.insert();
246241
}
247-
248242
// 过滤密码
249243
user.setPassWord("");
250-
251244
if (user != null) {
252245
//将从数据库查询的数据缓存到redis中
253246
stringRedisTemplate.opsForValue().set(RedisConf.USER_TOKEN + Constants.SYMBOL_COLON + accessToken, JsonUtils.objectToJson(user), userTokenSurvivalTime, TimeUnit.HOURS);

mogu_web/src/main/java/com/moxi/mogublog/web/restapi/CommentRestApi.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,6 @@ public String getListByUser(HttpServletRequest request, @Validated({GetList.clas
433433
Map<String, Object> resultMap = new HashMap<>();
434434
resultMap.put(SysConf.COMMENT_LIST, commentList);
435435
resultMap.put(SysConf.REPLY_LIST, replyList);
436-
437436
return ResultUtil.result(SysConf.SUCCESS, resultMap);
438437
}
439438

@@ -451,7 +450,6 @@ public String getPraiseListByUser(@ApiParam(name = "currentPage", value = "当
451450
return ResultUtil.result(SysConf.ERROR, MessageConf.INVALID_TOKEN);
452451
}
453452
String userUid = request.getAttribute(SysConf.USER_UID).toString();
454-
455453
QueryWrapper<Comment> queryWrappe = new QueryWrapper<>();
456454
queryWrappe.eq(SQLConf.USER_UID, userUid);
457455
queryWrappe.eq(SQLConf.TYPE, ECommentType.PRAISE);
@@ -462,7 +460,6 @@ public String getPraiseListByUser(@ApiParam(name = "currentPage", value = "当
462460
page.setSize(pageSize);
463461
IPage<Comment> pageList = commentService.page(page, queryWrappe);
464462
List<Comment> praiseList = pageList.getRecords();
465-
466463
List<String> blogUids = new ArrayList<>();
467464
praiseList.forEach(item -> {
468465
blogUids.add(item.getBlogUid());
@@ -482,9 +479,7 @@ public String getPraiseListByUser(@ApiParam(name = "currentPage", value = "当
482479
item.setBlog(blogMap.get(item.getBlogUid()));
483480
}
484481
});
485-
486482
pageList.setRecords(praiseList);
487-
488483
return ResultUtil.result(SysConf.SUCCESS, pageList);
489484
}
490485

@@ -494,11 +489,9 @@ public String getPraiseListByUser(@ApiParam(name = "currentPage", value = "当
494489
public String add(@Validated({Insert.class}) @RequestBody CommentVO commentVO, BindingResult result) {
495490
ThrowableUtils.checkParamArgument(result);
496491
HttpServletRequest request = RequestHolder.getRequest();
497-
498492
if (request.getAttribute(SysConf.USER_UID) == null) {
499493
return ResultUtil.result(SysConf.ERROR, MessageConf.INVALID_TOKEN);
500494
}
501-
502495
QueryWrapper<WebConfig> queryWrapper = new QueryWrapper<>();
503496
queryWrapper.eq(SysConf.STATUS, EStatus.ENABLE);
504497
WebConfig webConfig = webConfigService.getOne(queryWrapper);
@@ -514,11 +507,8 @@ public String add(@Validated({Insert.class}) @RequestBody CommentVO commentVO, B
514507
return ResultUtil.result(SysConf.ERROR, MessageConf.BLOG_NO_OPEN_COMMENTS);
515508
}
516509
}
517-
518510
String userUid = request.getAttribute(SysConf.USER_UID).toString();
519-
520511
User user = userService.getById(userUid);
521-
522512
// 判断字数是否超过限制
523513
if (commentVO.getContent().length() > SysConf.ONE_ZERO_TWO_FOUR) {
524514
return ResultUtil.result(SysConf.ERROR, MessageConf.COMMENT_CAN_NOT_MORE_THAN_1024);

mogu_web/src/main/java/com/moxi/mogublog/web/restapi/LoginRestApi.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.moxi.mogublog.web.global.SysConf;
1212
import com.moxi.mogublog.web.utils.RabbitMqUtil;
1313
import com.moxi.mogublog.xo.service.UserService;
14+
import com.moxi.mogublog.xo.service.WebConfigService;
1415
import com.moxi.mogublog.xo.utils.WebUtil;
1516
import com.moxi.mogublog.xo.vo.UserVO;
1617
import com.moxi.mougblog.base.enums.EStatus;
@@ -29,6 +30,7 @@
2930
import org.springframework.validation.annotation.Validated;
3031
import org.springframework.web.bind.annotation.*;
3132

33+
import javax.annotation.Resource;
3234
import javax.servlet.http.HttpServletRequest;
3335
import java.util.Date;
3436
import java.util.List;
@@ -48,13 +50,14 @@
4850
@Slf4j
4951
public class LoginRestApi {
5052

51-
5253
@Autowired
53-
RabbitMqUtil rabbitMqUtil;
54+
private RabbitMqUtil rabbitMqUtil;
5455
@Autowired
55-
PictureFeignClient pictureFeignClient;
56+
private WebConfigService webConfigService;
57+
@Resource
58+
private PictureFeignClient pictureFeignClient;
5659
@Autowired
57-
WebUtil webUtil;
60+
private WebUtil webUtil;
5861
@Autowired
5962
private UserService userService;
6063
@Autowired
@@ -66,6 +69,11 @@ public class LoginRestApi {
6669
@PostMapping("/login")
6770
public String login(@Validated({GetOne.class}) @RequestBody UserVO userVO, BindingResult result) {
6871
ThrowableUtils.checkParamArgument(result);
72+
Boolean isOpenLoginType = webConfigService.isOpenLoginType(RedisConf.PASSWORD);
73+
if (!isOpenLoginType){
74+
return ResultUtil.result(SysConf.ERROR, "后台未开启该登录方式!");
75+
}
76+
6977
String userName = userVO.getUserName();
7078
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
7179
queryWrapper.and(wrapper -> wrapper.eq(SQLConf.USER_NAME, userName).or().eq(SQLConf.EMAIL, userName));

mogu_xo/src/main/java/com/moxi/mogublog/xo/global/SysConf.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public final class SysConf extends BaseSysConf {
6262
*/
6363
public static final String GITHUB = "github";
6464
public static final String GITEE = "gitee";
65+
public static final String QQ_NUMBER = "qqNumber";
66+
6567
public static final String PIC_URL = "picUrl";
6668
public static final String QI_NIU_URL = "qiNiuUrl";
6769
public static final String NICKNAME = "nickname";

mogu_xo/src/main/java/com/moxi/mogublog/xo/service/WebConfigService.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,27 @@ public interface WebConfigService extends SuperService<WebConfig> {
1717
*
1818
* @return
1919
*/
20-
public WebConfig getWebConfig();
20+
WebConfig getWebConfig();
2121

2222
/**
2323
* 通过显示列表获取配置
2424
*
2525
* @return
2626
*/
27-
public WebConfig getWebConfigByShowList();
27+
WebConfig getWebConfigByShowList();
2828

2929
/**
3030
* 修改网站配置
3131
*
3232
* @param webConfigVO
3333
* @return
3434
*/
35-
public String editWebConfig(WebConfigVO webConfigVO);
35+
String editWebConfig(WebConfigVO webConfigVO);
36+
37+
/**
38+
* 是否开启该登录方式【账号密码、码云、Github、QQ、微信】
39+
* @param loginType
40+
* @return
41+
*/
42+
Boolean isOpenLoginType(String loginType);
3643
}

0 commit comments

Comments
 (0)