|
@@ -26,6 +26,7 @@ import me.zhengjie.annotation.rest.AnonymousGetMapping;
|
|
|
import me.zhengjie.annotation.rest.AnonymousPostMapping;
|
|
|
import me.zhengjie.config.RsaProperties;
|
|
|
import me.zhengjie.exception.BadRequestException;
|
|
|
+import me.zhengjie.modules.security.config.bean.CheckLoginNumber;
|
|
|
import me.zhengjie.modules.security.config.bean.LoginCodeEnum;
|
|
|
import me.zhengjie.modules.security.config.bean.LoginProperties;
|
|
|
import me.zhengjie.modules.security.config.bean.SecurityProperties;
|
|
@@ -33,6 +34,8 @@ import me.zhengjie.modules.security.security.TokenProvider;
|
|
|
import me.zhengjie.modules.security.service.dto.AuthUserDto;
|
|
|
import me.zhengjie.modules.security.service.dto.JwtUserDto;
|
|
|
import me.zhengjie.modules.security.service.OnlineUserService;
|
|
|
+import me.zhengjie.modules.system.service.UserService;
|
|
|
+import me.zhengjie.modules.system.service.dto.UserDto;
|
|
|
import me.zhengjie.service.LocalStorageService;
|
|
|
import me.zhengjie.utils.RsaUtils;
|
|
|
import me.zhengjie.utils.RedisUtils;
|
|
@@ -43,7 +46,9 @@ import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.AuthenticationException;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import javax.annotation.Resource;
|
|
@@ -72,41 +77,68 @@ public class AuthorizationController {
|
|
|
@Resource
|
|
|
private LoginProperties loginProperties;
|
|
|
|
|
|
+ private final UserService userService;
|
|
|
+
|
|
|
+ private final CheckLoginNumber checkLoginNumber;
|
|
|
+
|
|
|
@ApiOperation("登录授权")
|
|
|
@AnonymousPostMapping(value = "/login")
|
|
|
public ResponseEntity<Object> login(@Validated @RequestBody AuthUserDto authUser, HttpServletRequest request) throws Exception {
|
|
|
- // 密码解密
|
|
|
- String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword());
|
|
|
- System.out.println("password:"+password);
|
|
|
- // 查询验证码
|
|
|
- String code = (String) redisUtils.get(authUser.getUuid());
|
|
|
- // 清除验证码
|
|
|
- redisUtils.del(authUser.getUuid());
|
|
|
- if (StringUtils.isBlank(code)) {
|
|
|
- throw new BadRequestException("验证码不存在或已过期");
|
|
|
- }
|
|
|
- if (StringUtils.isBlank(authUser.getCode()) || !authUser.getCode().equalsIgnoreCase(code)) {
|
|
|
- throw new BadRequestException("验证码错误");
|
|
|
- }
|
|
|
- UsernamePasswordAuthenticationToken authenticationToken =
|
|
|
- new UsernamePasswordAuthenticationToken(authUser.getUsername(), password);
|
|
|
- Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
|
|
|
- SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
- // 生成令牌
|
|
|
- String token = tokenProvider.createToken(authentication);
|
|
|
- final JwtUserDto jwtUserDto = (JwtUserDto) authentication.getPrincipal();
|
|
|
- // 保存在线信息
|
|
|
- onlineUserService.save(jwtUserDto, token, request);
|
|
|
- // 返回 token 与 用户信息
|
|
|
- Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
|
|
|
- put("token", properties.getTokenStartWith() + token);
|
|
|
- put("user", jwtUserDto);
|
|
|
- }};
|
|
|
- if (loginProperties.isSingleLogin()) {
|
|
|
- //踢掉之前已经登录的token
|
|
|
- onlineUserService.checkLoginOnUser(authUser.getUsername(), token);
|
|
|
+ //根据账号查询用户
|
|
|
+ UserDto userDto = userService.findByUsername(authUser.getUsername());
|
|
|
+ //如果账号不等于空,则验证登录次数限制
|
|
|
+ if(!ObjectUtils.isEmpty(userDto)){
|
|
|
+ //redis限制次数的key
|
|
|
+ String key = "SIGN_UP_LOGIN_COUNT_" + authUser.getUsername();
|
|
|
+ //获取当前半个小时内登录的次数
|
|
|
+ Integer number = (Integer) redisUtils.get(key);
|
|
|
+ //如果超过限制次数则返回账号锁定
|
|
|
+ if(!ObjectUtils.isEmpty(number) && number >= userDto.getErrLimit()){
|
|
|
+ throw new BadRequestException("账号已被锁定!");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 密码解密
|
|
|
+ String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword());
|
|
|
+ System.out.println("password:"+password);
|
|
|
+ // 查询验证码
|
|
|
+ String code = (String) redisUtils.get(authUser.getUuid());
|
|
|
+ // 清除验证码
|
|
|
+ redisUtils.del(authUser.getUuid());
|
|
|
+ if (StringUtils.isBlank(code)) {
|
|
|
+ throw new BadRequestException("验证码不存在或已过期");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(authUser.getCode()) || !authUser.getCode().equalsIgnoreCase(code)) {
|
|
|
+ checkLoginNumber.checkLoginTimes(key,userDto.getErrLimit());
|
|
|
+ throw new BadRequestException("验证码错误");
|
|
|
+ }
|
|
|
+ Authentication authentication = null;
|
|
|
+ try {
|
|
|
+ UsernamePasswordAuthenticationToken authenticationToken =
|
|
|
+ new UsernamePasswordAuthenticationToken(authUser.getUsername(), password);
|
|
|
+ authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
+ } catch (AuthenticationException e) {
|
|
|
+ checkLoginNumber.checkLoginTimes(key,userDto.getErrLimit());
|
|
|
+ throw new BadRequestException("密码错误");
|
|
|
+ }
|
|
|
+ // 生成令牌
|
|
|
+ String token = tokenProvider.createToken(authentication);
|
|
|
+ final JwtUserDto jwtUserDto = (JwtUserDto) authentication.getPrincipal();
|
|
|
+ // 保存在线信息
|
|
|
+ onlineUserService.save(jwtUserDto, token, request);
|
|
|
+ // 返回 token 与 用户信息
|
|
|
+ Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
|
|
|
+ put("token", properties.getTokenStartWith() + token);
|
|
|
+ put("user", jwtUserDto);
|
|
|
+ }};
|
|
|
+ if (loginProperties.isSingleLogin()) {
|
|
|
+ //踢掉之前已经登录的token
|
|
|
+ onlineUserService.checkLoginOnUser(authUser.getUsername(), token);
|
|
|
+ }
|
|
|
+ return ResponseEntity.ok(authInfo);
|
|
|
+ } else {
|
|
|
+ throw new BadRequestException("账号不正确");
|
|
|
}
|
|
|
- return ResponseEntity.ok(authInfo);
|
|
|
}
|
|
|
|
|
|
@ApiOperation("单点登录授权")
|