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

import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.tuoheng.common.utils.JsonResult; import com.tuoheng.common.utils.JsonResult;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter; import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.http.HttpStatus;


import javax.servlet.ServletRequest; import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
httpServletResponse.setCharacterEncoding("UTF-8"); httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("application/json"); httpServletResponse.setContentType("application/json");
JsonResult jsonResult = new JsonResult(); 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; return false;
} }
} }

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

package com.tuoheng.system.utils; package com.tuoheng.system.utils;


import cn.hutool.core.util.ObjectUtil;
import com.tuoheng.common.exception.ServiceException; import com.tuoheng.common.exception.ServiceException;
import com.tuoheng.common.utils.SpringUtils; import com.tuoheng.common.utils.SpringUtils;
import com.tuoheng.system.entity.User; import com.tuoheng.system.entity.User;
* @return * @return
*/ */
public static User getUserInfo() { 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;
} }


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

Loading…
Cancel
Save