diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/LogoutApi.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/LogoutApi.java index c8f99ec..15de22b 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/LogoutApi.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/LogoutApi.java @@ -2,6 +2,7 @@ package com.ruoyi.web.controller.api; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.domain.entity.SysUser; +import com.ruoyi.common.enums.OnlineStatus; import com.ruoyi.common.utils.CookieUtils; import com.ruoyi.common.utils.MessageUtils; import com.ruoyi.common.utils.ShiroUtils; @@ -9,11 +10,16 @@ import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.spring.SpringUtils; import com.ruoyi.framework.manager.AsyncManager; import com.ruoyi.framework.manager.factory.AsyncFactory; +import com.ruoyi.framework.shiro.session.OnlineSession; +import com.ruoyi.framework.shiro.session.OnlineSessionDAO; import com.ruoyi.framework.shiro.web.filter.LogoutFilter; +import com.ruoyi.system.domain.SysUserOnline; import com.ruoyi.system.service.ISysUserOnlineService; +import org.apache.commons.collections.CollectionUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.subject.Subject; import org.apache.shiro.web.servlet.SimpleCookie; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -23,6 +29,8 @@ import javax.servlet.ServletResponse; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.List; +import java.util.stream.Collectors; /** * 工业互联网退出接口 @@ -32,15 +40,42 @@ import javax.servlet.http.HttpServletResponse; @Controller @RequestMapping("/api/logout") public class LogoutApi { - + @Autowired + private ISysUserOnlineService userOnlineService; + @Autowired + private OnlineSessionDAO onlineSessionDAO; @GetMapping() public void logout(HttpServletRequest request, HttpServletResponse response,String userName){ + //CookieUtil.remove(request, response, "JSESSIONID"); + //START + System.out.println("》》》获取接口用户名:"+ userName); + SysUserOnline userOnline = new SysUserOnline(); + userOnline.setStatus(OnlineStatus.on_line); + List list = userOnlineService.selectUserOnlineList(userOnline); + + List result = null; + + result = list.stream() + .filter((SysUserOnline b) -> b.getLoginName().contains(userName)) + .collect(Collectors.toList()); + for (SysUserOnline SysUserOnline:result + ) { + SysUserOnline online = userOnlineService.selectOnlineById(SysUserOnline.getSessionId()); + OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getSessionId()); + onlineSessionDAO.delete(onlineSession); + online.setStatus(OnlineStatus.off_line); + userOnlineService.saveOnline(online); + userOnlineService.removeUserCache(online.getLoginName(), SysUserOnline.getSessionId()); + } + + + //END Subject subject = SecurityUtils.getSubject(); SysUser user = new SysUser(); user.setUserName(userName); if (StringUtils.isNotNull(user)) { - String loginName = user.getLoginName(); + String loginName = ShiroUtils.getLoginName(); // 记录用户退出日志 AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGOUT, MessageUtils.message("user.logout.success"))); // 清理缓存 diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java index 4388deb..37e63c4 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java @@ -7,6 +7,14 @@ import cn.hutool.http.HttpRequest; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSON; +import com.ruoyi.common.constant.UserConstants; +import com.ruoyi.common.core.domain.entity.SysUser; +import com.ruoyi.common.utils.ShiroUtils; +import com.ruoyi.common.utils.uuid.IdUtils; +import com.ruoyi.common.utils.uuid.UUID; +import com.ruoyi.framework.shiro.service.SysPasswordService; +import com.ruoyi.system.service.ISysRoleService; +import com.ruoyi.system.service.ISysUserService; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UsernamePasswordToken; @@ -53,6 +61,12 @@ public class SysLoginController extends BaseController @Autowired private ConfigService configService; + @Autowired + private ISysUserService userService; + @Autowired + private SysPasswordService passwordService; + @Autowired + private ISysRoleService roleService; @GetMapping("/login") public String login(HttpServletRequest request, HttpServletResponse response, ModelMap mmap) @@ -83,11 +97,31 @@ public class SysLoginController extends BaseController if(StringUtils.isNotNull(result)){ com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(result); String username = jsonObject.get("username").toString(); - String password = jsonObject.get("password").toString(); + + //判断数据库里是否存在该用户,如果不存在则添加,如果存在则跳过; + if (!UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(username))) + { + SysUser user = new SysUser(); + user.setDeptId(100L); + user.setLoginName(username); + user.setUserName(username); + user.setRoleIds(new Long[]{2L}); + user.setRoleId(2L); + user.setSalt(ShiroUtils.randomSalt()); + user.setPassword(passwordService.encryptPassword(user.getLoginName(), "123456", user.getSalt())); + user.setCreateBy(username); + user.setStatus("0"); + userService.insertUser(user); + } mmap.put("requestURL", requestURL); mmap.put("username", username); - mmap.put("password", password); +// mmap.put("password", password); + if (StringUtils.isNotNull(jsonObject.get("password"))) { + mmap.put("password", jsonObject.get("password").toString()); + } else { + mmap.put("password", "123456"); + } return "ssologin"; } } diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 3f564ef..2149de4 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -89,7 +89,9 @@ pagehelper: shiro: user: # 登录地址 +# loginUrl: http://localhost:5050/login loginUrl: http://10.100.71.111/sso/login?redirect_url=http://10.100.71.119:5050/login +# loginUrl: http://10.100.71.111/sso/login?redirect_url=http://localhost:5050/login # 权限认证失败地址 unauthorizedUrl: /unauth # 首页地址 diff --git a/ruoyi-admin/src/main/resources/templates/index.html b/ruoyi-admin/src/main/resources/templates/index.html index d095208..cd526f9 100644 --- a/ruoyi-admin/src/main/resources/templates/index.html +++ b/ruoyi-admin/src/main/resources/templates/index.html @@ -86,6 +86,17 @@