diff --git a/src/main/java/com/sztzjy/fund_investment/controller/UserController.java b/src/main/java/com/sztzjy/fund_investment/controller/UserController.java index dd2765f..63f4859 100644 --- a/src/main/java/com/sztzjy/fund_investment/controller/UserController.java +++ b/src/main/java/com/sztzjy/fund_investment/controller/UserController.java @@ -3,7 +3,9 @@ package com.sztzjy.fund_investment.controller; import cn.hutool.core.util.IdUtil; import com.sztzjy.fund_investment.annotation.AnonymousAccess; import com.sztzjy.fund_investment.annotation.OperateLog; +import com.sztzjy.fund_investment.config.exception.UnAuthorizedException; import com.sztzjy.fund_investment.config.security.JwtUser; +import com.sztzjy.fund_investment.config.security.LoginResult; import com.sztzjy.fund_investment.config.security.TokenProvider; import com.sztzjy.fund_investment.entity.Flow; import com.sztzjy.fund_investment.entity.PerformanceScore; @@ -20,8 +22,10 @@ import com.sztzjy.fund_investment.service.PerformanceScoreService; import com.sztzjy.fund_investment.service.tea.ClassScoreService; import com.sztzjy.fund_investment.util.ResultEntity; import com.sztzjy.fund_investment.util.RsaUtil; +import com.sztzjy.fund_investment.util.TzApi; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -59,15 +63,100 @@ public class UserController { @Autowired private ClassScoreService classScoreService; +// @ApiOperation(value = "用户登录", httpMethod = "POST") +// @OperateLog(recordParameters = false) +// @PostMapping("login") +// @AnonymousAccess +// public ResultEntity login(@RequestParam(required = false) String username, +// @RequestParam(required = false) String passwordEncode, +// @RequestParam(required = false) String ticket) { +// +// if (ticket == null) { +// String password; +// try { +// password = RsaUtil.decryptByPrivateKey(passwordEncode); +// } catch (Exception e) { +// return new ResultEntity(HttpStatus.BAD_REQUEST, "密码错误"); +// } +// UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); +// Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken); +// SecurityContextHolder.getContext().setAuthentication(authentication); +// JwtUser jwtUser = (JwtUser) authentication.getPrincipal(); +// String token = TokenProvider.createToken(jwtUser); +// Map map = new HashMap<>(); +// map.put("token", token); +// map.put("name", jwtUser.getName()); +// User user = userService.findById(jwtUser.getUserId()); +// map.put("username", user.getUsername()); +// map.put("roleId", user.getRoleId()); +// map.put("phone", user.getPhone()); +// map.put("schoolId", "999999999"); +// map.put("userId", user.getUserid()); +// +// Flow flow = flowMapper.selectByPrimaryKey(jwtUser.getUserId()); +// if (flow == null) { +// String uuid = String.valueOf(UUID.randomUUID()); +// Flow flow1 = new Flow(); +// flow1.setUserid(jwtUser.getUserId()); +// flow1.setFlowId(uuid); +// flowMapper.insert(flow1); +// map.put("flowId", uuid); +// } else { +// map.put("flowId", flow.getFlowId()); +// } +// return new ResultEntity(HttpStatus.OK, map); +// } else { //平台跳转登录 +// //判断用户是否存在 +// Map accessToken = iIlabService.getAccessToken(ticket); +// String access_token = accessToken.get("access_token"); +// if (access_token == null) { +// return new ResultEntity(HttpStatus.BAD_REQUEST, "登录过期,请重试!"); +// } +// String un = accessToken.get("un"); +// String dis = accessToken.get("dis"); +// User user = userMapper.selectByUn(un); +// String newUUID = IdUtil.simpleUUID(); +// if (user != null) { +// Flow flow = flowMapper.selectByPrimaryKey(user.getUserid()); +// Map map = getStringObjectMap(access_token, un, dis, flow.getUserid(), flow.getFlowId()); +// user.setAccessToken(access_token); +// userMapper.updateByPrimaryKey(user); +// return new ResultEntity(HttpStatus.OK, map); +// } +// User newUser = new User(); +// String flowId = IdUtil.simpleUUID(); +// newUser.setUserid(newUUID); +// newUser.setRoleId(4); +// newUser.setLoginType(0); +// newUser.setAccessToken(access_token); +// newUser.setDis(dis); +// newUser.setName(dis); +// newUser.setUn(un); +// newUser.setUsername(un); +// newUser.setCreateTime(new Date()); +// newUser.setSchoolId("999999999"); +// newUser.setClassId("999999999"); +// newUser.setClassName("国家虚拟仿真训练班"); +// Map map = getStringObjectMap(access_token, un, dis, newUUID, flowId); +// userMapper.insert(newUser); +// Flow flow = new Flow(); +// flow.setUserid(newUUID); +// flow.setFlowId(flowId); +// flowMapper.insert(flow); +// return new ResultEntity(HttpStatus.OK, map); +// } +// } + + @ApiOperation(value = "用户登录", httpMethod = "POST") @OperateLog(recordParameters = false) @PostMapping("login") @AnonymousAccess public ResultEntity login(@RequestParam(required = false) String username, @RequestParam(required = false) String passwordEncode, - @RequestParam(required = false) String ticket) { + @RequestParam(required = false) String TOKEN) { - if (ticket == null) { + if (TOKEN == null) { String password; try { password = RsaUtil.decryptByPrivateKey(passwordEncode); @@ -86,8 +175,9 @@ public class UserController { map.put("username", user.getUsername()); map.put("roleId", user.getRoleId()); map.put("phone", user.getPhone()); - map.put("schoolId", "999999999"); + map.put("schoolId", user.getSchoolId()); map.put("userId", user.getUserid()); + map.put("classId", user.getClassId()); Flow flow = flowMapper.selectByPrimaryKey(jwtUser.getUserId()); if (flow == null) { @@ -101,48 +191,35 @@ public class UserController { map.put("flowId", flow.getFlowId()); } return new ResultEntity(HttpStatus.OK, map); - } else { //平台跳转登录 - //判断用户是否存在 - Map accessToken = iIlabService.getAccessToken(ticket); - String access_token = accessToken.get("access_token"); - if (access_token == null) { - return new ResultEntity(HttpStatus.BAD_REQUEST, "登录过期,请重试!"); + } else { // 2、智云单点登录 + JwtUser jwtUser; + jwtUser = TokenProvider.getJWTUserByZhiYun(TOKEN); + + jwtUser = TzApi.foreignExchangeTradingLogin(jwtUser.getUsername(), jwtUser.getPassword()); + if (jwtUser == null) { + throw new UnAuthorizedException("用户名或密码错误"); } - String un = accessToken.get("un"); - String dis = accessToken.get("dis"); - User user = userMapper.selectByUn(un); - String newUUID = IdUtil.simpleUUID(); - if (user != null) { - Flow flow = flowMapper.selectByPrimaryKey(user.getUserid()); - Map map = getStringObjectMap(access_token, un, dis, flow.getUserid(), flow.getFlowId()); - user.setAccessToken(access_token); - userMapper.updateByPrimaryKey(user); - return new ResultEntity(HttpStatus.OK, map); + Authentication authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities()); + SecurityContextHolder.getContext().setAuthentication(authentication); + String token = TokenProvider.createToken(jwtUser); + LoginResult loginResult = LoginResult.create(jwtUser, token); + Map map = new HashMap<>(); + map.put("token", token); + map.put("name", loginResult.getName()); + User user = userService.findById(loginResult.getUserId()); + map.put("username", loginResult.getUsername()); + map.put("roleId", loginResult.getRoleId()); + map.put("schoolId", loginResult.getSchoolId()); + if (StringUtils.isNotBlank(loginResult.getUserId())) { + map.put("classId", loginResult.getClassId()); } - User newUser = new User(); - String flowId = IdUtil.simpleUUID(); - newUser.setUserid(newUUID); - newUser.setRoleId(4); - newUser.setLoginType(0); - newUser.setAccessToken(access_token); - newUser.setDis(dis); - newUser.setName(dis); - newUser.setUn(un); - newUser.setUsername(un); - newUser.setCreateTime(new Date()); - newUser.setSchoolId("999999999"); - newUser.setClassId("999999999"); - newUser.setClassName("国家虚拟仿真训练班"); - Map map = getStringObjectMap(access_token, un, dis, newUUID, flowId); - userMapper.insert(newUser); - Flow flow = new Flow(); - flow.setUserid(newUUID); - flow.setFlowId(flowId); - flowMapper.insert(flow); + map.put("token", token); + map.put("userId", loginResult.getUserId()); return new ResultEntity(HttpStatus.OK, map); } } + /* 封装MAP并生成flow表数据 * @author xcj * @Date 2024/1/3