Commit 7f1a115d zshaohui

1.登录功能提交

1 个父辈 612eb27a
...@@ -89,7 +89,7 @@ public class DataInitManager { ...@@ -89,7 +89,7 @@ public class DataInitManager {
operator = roleManager.save(operator); operator = roleManager.save(operator);
log.info("创建默认角色:" + operator.toString()); log.info("创建默认角色:" + operator.toString());
admin = new User(userName, "admin@neotel.tech", "zh-CN", role.getId(), "$2a$10$Egp1/gvFlt7zhlXVfEFw4OfWQCGPw0ClmMcc6FjTnvXNRVf9zdMRa", true, true, new Date(), groupIds, ""); admin = new User(userName, "admin@neotel.tech", "zh-CN", role.getId(), "$2a$10$Egp1/gvFlt7zhlXVfEFw4OfWQCGPw0ClmMcc6FjTnvXNRVf9zdMRa", true, true, new Date(), groupIds, "","");
admin = userManager.save(admin); admin = userManager.save(admin);
log.info("创建默认用户:" + admin.toString()); log.info("创建默认用户:" + admin.toString());
......
...@@ -107,7 +107,7 @@ public class UserCodeUtil { ...@@ -107,7 +107,7 @@ public class UserCodeUtil {
String langu=""; String langu="";
String roleId=""; String roleId="";
User user=new User(username,email,langu,roleId,"",true User user=new User(username,email,langu,roleId,"",true
,false,new Date(),new HashSet<>(),""); ,false,new Date(),new HashSet<>(),"","");
user.setId(id); user.setId(id);
user.setCreateDate(createData); user.setCreateDate(createData);
list.add(user); list.add(user);
......
package com.neotel.smfcore.custom.toyota1541.controller;
import com.neotel.smfcore.common.bean.ResultBean;
import com.neotel.smfcore.common.exception.ValidateException;
import com.neotel.smfcore.common.utils.*;
import com.neotel.smfcore.core.api.SmfApi;
import com.neotel.smfcore.security.TokenProvider;
import com.neotel.smfcore.security.annotation.AnonymousPostMapping;
import com.neotel.smfcore.security.bean.LoginProperties;
import com.neotel.smfcore.security.bean.RsaProperties;
import com.neotel.smfcore.security.bean.SecurityProperties;
import com.neotel.smfcore.security.rest.bean.dto.AuthUserDto;
import com.neotel.smfcore.security.rest.bean.dto.JwtUserDto;
import com.neotel.smfcore.security.rest.bean.dto.OnlineUserDto;
import com.neotel.smfcore.security.service.OnlineUserService;
import com.neotel.smfcore.security.service.manager.IUserManager;
import com.neotel.smfcore.security.service.po.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Api(tags = "登录")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/toyotaLogin")
public class ToyotaLoginController {
private final SecurityProperties properties;
@Autowired
private OnlineUserService onlineUserService;
@Autowired
private TokenProvider tokenProvider;
private final AuthenticationManagerBuilder authenticationManagerBuilder;
@Resource
private LoginProperties loginProperties;
@Autowired
private IUserManager userManager;
@Autowired
private UserDetailsService userDetailsService;
@ApiOperation("登录授权")
@AnonymousPostMapping(value = "/login")
public ResponseEntity<Object> login(@RequestBody AuthUserDto authUser, HttpServletRequest request) throws Exception {
String code = authUser.getCode();
if (StringUtils.isEmpty(code)) {
throw new ValidateException("smfcore.valueCanotNull", "{0}不能为空", new String[]{"code"});
}
//取第一位是id信息
String id = code.substring(0, code.indexOf("-"));
User user = userManager.get(id);
if (user == null) {
throw new ValidateException("smfcore.valueNotExist", "{0}[{1}]不存在", new String[]{"userId", id});
}
//判断与登录是否一致
String loginCode = user.getLoginCode();
if (StringUtils.isEmpty(loginCode)){
loginCode = id +"-"+ Md5Utls.getMd5(id, user.getCreateDate());
}
if (StringUtils.isEmpty(user.getLoginCode())){
user.setLoginCode(loginCode);
userManager.save(user);
}
if (!loginCode.equals(code)){
throw new ValidateException("smfcore.login.codeError", "登录码[{0}]错误", new String[]{code});
}
/* UsernamePasswordAuthenticationToken authenticationToken =
new UsernamePasswordAuthenticationToken(user.getUsername(), password);
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authentication);*/
// 生成令牌与第三方系统获取令牌方式
UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUsername());
Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
String token = tokenProvider.createToken(authentication);
final JwtUserDto jwtUserDto = (JwtUserDto) authentication.getPrincipal();
// 保存在线信息
String ip = StringUtils.getIp(request);
String browser = StringUtils.getBrowser(request);
String address = StringUtils.getCityInfo(ip);
OnlineUserDto onlineUserDto = null;
try {
long seconds = onlineUserService.properties.getTokenValidityInSeconds() / 1000;
Date exTime = DateUtil.addSeconds(new Date(), new Long(seconds).intValue());
onlineUserDto = new OnlineUserDto(jwtUserDto.getUsername(), browser, ip, address, EncryptUtils.desEncrypt(token), new Date(), exTime);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
OnlineUserService.onlineUserMap.put(onlineUserService.properties.getOnlineKey() + token, onlineUserDto);
// 返回 token 与 用户信息
Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
put("token", properties.getTokenStartWith() + token);
put("user", jwtUserDto);
}};
if (loginProperties.isSingleLogin()) {
//踢掉之前已经登录的token
onlineUserService.checkLoginOnUser(user.getUsername(), token);
}
//重新登陆时清理调试模式状态
SecurityUtils.updateToDebugModel(user.getUsername(), false);
return ResponseEntity.ok(authInfo);
}
}
...@@ -15,11 +15,16 @@ ...@@ -15,11 +15,16 @@
*/ */
package com.neotel.smfcore.security.rest; package com.neotel.smfcore.security.rest;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.multi.qrcode.QRCodeMultiReader;
import com.neotel.smfcore.common.bean.PageData; import com.neotel.smfcore.common.bean.PageData;
import com.neotel.smfcore.common.bean.ResultBean; import com.neotel.smfcore.common.bean.ResultBean;
import com.neotel.smfcore.common.csv.CsvReader; import com.neotel.smfcore.common.csv.CsvReader;
import com.neotel.smfcore.common.exception.ValidateException; import com.neotel.smfcore.common.exception.ValidateException;
import com.neotel.smfcore.common.utils.*; import com.neotel.smfcore.common.utils.*;
import com.neotel.smfcore.core.barcode.utils.QrcodeUtils;
import com.neotel.smfcore.security.annotation.AnonymousAccess;
import com.neotel.smfcore.security.bean.FileProperties; import com.neotel.smfcore.security.bean.FileProperties;
import com.neotel.smfcore.security.bean.RsaProperties; import com.neotel.smfcore.security.bean.RsaProperties;
import com.neotel.smfcore.security.rest.bean.dto.UserDto; import com.neotel.smfcore.security.rest.bean.dto.UserDto;
...@@ -69,6 +74,9 @@ public class UserController { ...@@ -69,6 +74,9 @@ public class UserController {
@Autowired @Autowired
private final FileProperties properties; private final FileProperties properties;
@Autowired
private QrcodeUtils qrcodeUtils;
@ApiOperation("导出用户数据") @ApiOperation("导出用户数据")
@GetMapping(value = "/download") @GetMapping(value = "/download")
@PreAuthorize("@el.check('user:list')") @PreAuthorize("@el.check('user:list')")
...@@ -261,6 +269,31 @@ public class UserController { ...@@ -261,6 +269,31 @@ public class UserController {
return ResultBean.newOkResult(resultMsg); return ResultBean.newOkResult(resultMsg);
} }
@ApiOperation("生成用户授权的二维码")
@PostMapping(value = "/userQrcode")
//@AnonymousAccess
public ResultBean userQrcode(@RequestBody Map<String, String> paramMap) {
String id = paramMap.get("id");
if (StringUtils.isEmpty(id)) {
return ResultBean.newErrorResult(-1, "smfcore.valueCanotNull", "{0}不能为空", new String[]{"id"});
}
User user = userManager.get(id);
//用户登录的授权code
String loginCode = user.getLoginCode();
if (StringUtils.isEmpty(loginCode)) {
loginCode = id + "-" + Md5Utls.getMd5(id, user.getCreateDate());
}
if (StringUtils.isEmpty(user.getLoginCode())) {
user.setLoginCode(loginCode);
userManager.save(user);
}
QrConfig config = new QrConfig();
byte[] codeBytes = QrCodeUtil.generatePng(loginCode, config);
return ResultBean.newOkResult(codeBytes);
}
protected String handleUserUpload(String fileURL ) throws Exception protected String handleUserUpload(String fileURL ) throws Exception
{ {
......
...@@ -58,6 +58,8 @@ public class User extends BasePo implements Serializable { ...@@ -58,6 +58,8 @@ public class User extends BasePo implements Serializable {
private String checkCode; private String checkCode;
private String loginCode;
public boolean hasGroup(String groupId) { public boolean hasGroup(String groupId) {
if (groupId == null || groupId.equals("") || groupId.equals("-1")) { if (groupId == null || groupId.equals("") || groupId.equals("-1")) {
......
...@@ -2,7 +2,7 @@ server: ...@@ -2,7 +2,7 @@ server:
port: 8800 port: 8800
api: api:
name: name: 1541
inCheckUrl: inCheckUrl:
outNotifyUrl: outNotifyUrl:
inNotifyUrl: inNotifyUrl:
......
...@@ -401,4 +401,5 @@ smfcore.storagePos.available=\u662F\u5426\u53EF\u7528 ...@@ -401,4 +401,5 @@ smfcore.storagePos.available=\u662F\u5426\u53EF\u7528
smfcore.storagePos.yes=\u662F smfcore.storagePos.yes=\u662F
smfcore.storagePos.no=\u5426 smfcore.storagePos.no=\u5426
smfcore.dashBoard=\u4EEA\u8868\u76D8 smfcore.dashBoard=\u4EEA\u8868\u76D8
smfcore.spHumiture=\u6E29\u6E7F\u5EA6
\ No newline at end of file \ No newline at end of file
smfcore.spHumiture=\u6E29\u6E7F\u5EA6
smfcore.login.codeError=\u767B\u5F55\u7801[{0}]\u9519\u8BEF
\ No newline at end of file \ No newline at end of file
...@@ -392,4 +392,5 @@ smfcore.storagePos.available=Available ...@@ -392,4 +392,5 @@ smfcore.storagePos.available=Available
smfcore.storagePos.yes=Yes smfcore.storagePos.yes=Yes
smfcore.storagePos.no=No smfcore.storagePos.no=No
smfcore.dashBoard=Dashboard smfcore.dashBoard=Dashboard
smfcore.spHumiture=Temperature & Humidity
\ No newline at end of file \ No newline at end of file
smfcore.spHumiture=Temperature & Humidity
smfcore.login.codeError=Login code [{0}] error
\ No newline at end of file \ No newline at end of file
...@@ -388,4 +388,5 @@ smfcore.storagePos.available=\u5229\u7528\u53EF\u5426 ...@@ -388,4 +388,5 @@ smfcore.storagePos.available=\u5229\u7528\u53EF\u5426
smfcore.storagePos.yes=\u662F smfcore.storagePos.yes=\u662F
smfcore.storagePos.no=\u5426 smfcore.storagePos.no=\u5426
smfcore.dashBoard=\u30C0\u30C3\u30B7\u30E5\u30DC\u30FC\u30C9 smfcore.dashBoard=\u30C0\u30C3\u30B7\u30E5\u30DC\u30FC\u30C9
smfcore.spHumiture=\u6E29\u6E7F\u5EA6
\ No newline at end of file \ No newline at end of file
smfcore.spHumiture=\u6E29\u6E7F\u5EA6
smfcore.login.codeError=\u30ED\u30B0\u30A4\u30F3\u30B3\u30FC\u30C9[{0}]\u30A8\u30E9\u30FC
\ No newline at end of file \ No newline at end of file
...@@ -388,4 +388,5 @@ smfcore.storagePos.available=\u662F\u5426\u53EF\u7528 ...@@ -388,4 +388,5 @@ smfcore.storagePos.available=\u662F\u5426\u53EF\u7528
smfcore.storagePos.yes=\u662F smfcore.storagePos.yes=\u662F
smfcore.storagePos.no=\u5426 smfcore.storagePos.no=\u5426
smfcore.dashBoard=\u4EEA\u8868\u76D8 smfcore.dashBoard=\u4EEA\u8868\u76D8
smfcore.spHumiture=\u6E29\u6E7F\u5EA6
\ No newline at end of file \ No newline at end of file
smfcore.spHumiture=\u6E29\u6E7F\u5EA6
smfcore.login.codeError=\u767B\u5F55\u7801[{0}]\u9519\u8BEF
\ No newline at end of file \ No newline at end of file
...@@ -388,4 +388,5 @@ smfcore.storagePos.available=\u662F\u5426\u53EF\u7528 ...@@ -388,4 +388,5 @@ smfcore.storagePos.available=\u662F\u5426\u53EF\u7528
smfcore.storagePos.yes=\u662F smfcore.storagePos.yes=\u662F
smfcore.storagePos.no=\u5426 smfcore.storagePos.no=\u5426
smfcore.dashBoard=SP\u5100\u9336\u76E4 smfcore.dashBoard=SP\u5100\u9336\u76E4
smfcore.spHumiture=\u6E29\u6E7F\u5EA6
\ No newline at end of file \ No newline at end of file
smfcore.spHumiture=\u6E29\u6E7F\u5EA6
smfcore.login.codeError=\u767B\u9304\u78BC[{0}]\u932F\u8AA4
\ No newline at end of file \ No newline at end of file
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!