sa-token/sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/spring/SpringMVCUtil.java

44 lines
1.2 KiB
Java
Raw Normal View History

2020-12-22 01:11:28 +08:00
package cn.dev33.satoken.spring;
2020-02-05 00:31:51 +08:00
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* SpringMVC相关操作
* @author kong
*
*/
2020-12-22 01:11:28 +08:00
public class SpringMVCUtil {
2020-02-05 00:31:51 +08:00
2020-02-08 00:54:38 +08:00
/**
* 获取当前会话的 request
2020-12-22 01:11:28 +08:00
* @return .
2020-02-08 00:54:38 +08:00
*/
2020-02-05 00:31:51 +08:00
public static HttpServletRequest getRequest() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();// 大善人SpringMVC提供的封装
if(servletRequestAttributes == null) {
throw new RuntimeException("当前环境非JavaWeb");
}
return servletRequestAttributes.getRequest();
}
2020-02-08 00:54:38 +08:00
/**
* 获取当前会话的 response
2020-12-22 01:11:28 +08:00
* @return .
2020-02-08 00:54:38 +08:00
*/
2020-02-05 00:31:51 +08:00
public static HttpServletResponse getResponse() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();// 大善人SpringMVC提供的封装
if(servletRequestAttributes == null) {
throw new RuntimeException("当前环境非JavaWeb");
}
return servletRequestAttributes.getResponse();
}
2020-09-07 02:21:35 +08:00
2020-02-05 00:31:51 +08:00
}