Browse Source

session过期之后不能登出

tags/v1.1.0
qiujinyang 2 years ago
parent
commit
44b410c752
2 changed files with 15 additions and 3 deletions
  1. +2
    -1
      tuoheng-system/src/main/java/com/tuoheng/system/filter/ShiroLoginFilter.java
  2. +13
    -2
      tuoheng-system/src/main/java/com/tuoheng/system/utils/ShiroUtils.java

+ 2
- 1
tuoheng-system/src/main/java/com/tuoheng/system/filter/ShiroLoginFilter.java View File

@@ -3,6 +3,7 @@ package com.tuoheng.system.filter;
import com.alibaba.fastjson.JSONObject;
import com.tuoheng.common.utils.JsonResult;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.http.HttpStatus;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
@@ -48,7 +49,7 @@ public class ShiroLoginFilter extends FormAuthenticationFilter {
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("application/json");
JsonResult jsonResult = new JsonResult();
httpServletResponse.getWriter().write(JSONObject.toJSON(jsonResult.error(401, "请先登录")).toString());
httpServletResponse.getWriter().write(JSONObject.toJSON(jsonResult.error(HttpStatus.UNAUTHORIZED.value(), "请先登录")).toString());
return false;
}
}

+ 13
- 2
tuoheng-system/src/main/java/com/tuoheng/system/utils/ShiroUtils.java View File

@@ -1,5 +1,6 @@
package com.tuoheng.system.utils;

import cn.hutool.core.util.ObjectUtil;
import com.tuoheng.common.exception.ServiceException;
import com.tuoheng.common.utils.SpringUtils;
import com.tuoheng.system.entity.User;
@@ -45,7 +46,11 @@ public class ShiroUtils {
* @return
*/
public static User getUserInfo() {
return (User) SecurityUtils.getSubject().getPrincipal();
User user = (User) SecurityUtils.getSubject().getPrincipal();
if(ObjectUtil.isEmpty(user)){
throw new ServiceException(HttpStatus.UNAUTHORIZED.value(), "请先登录");
}
return user;
}

/**
@@ -53,8 +58,11 @@ public class ShiroUtils {
*
* @return
*/
public static Integer getUserId() {
public static Integer getUserId() throws ServiceException {
User user = getUserInfo();
if(ObjectUtil.isEmpty(user)){
throw new ServiceException(HttpStatus.UNAUTHORIZED.value(), "请先登录");
}
return Optional.ofNullable(user.getId()).orElseThrow(() ->
new ServiceException(HttpStatus.BAD_REQUEST.value(), "未获取到当前用户信息!"));
}
@@ -66,6 +74,9 @@ public class ShiroUtils {
*/
public static Integer getTenantId() {
User user = getUserInfo();
if(ObjectUtil.isEmpty(user)){
throw new ServiceException(HttpStatus.UNAUTHORIZED.value(), "请先登录");
}
return Optional.ofNullable(user.getTenantId()).orElseThrow(() ->
new ServiceException(HttpStatus.BAD_REQUEST.value(), "未获取到当前租户信息!"));
}

Loading…
Cancel
Save