在我看来,拦截器应该是接收到一个请求后才会执行,但是为什么我在项目中自定义的这个拦截器在没有任何接口去请求的时候也一直在执行啊?求大神指教呀~
下面是我的代码和后台打印的日志
package com.test.user.interceptor
import com.test.common.ErrMsg
import com.test.common.Result
import com.test.common.Sessions
import com.test.common.constant.IbErrMsg
import org.slf4j.LoggerFactory
import org.springframework.web.servlet.HandlerInterceptor
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.validation.constraints.NotNull
class LoginInterceptor : HandlerInterceptor {
private val logger = LoggerFactory.getLogger(LoginInterceptor::class.java)
override fun preHandle(@NotNull request: HttpServletRequest,
@NotNull response: HttpServletResponse,
@NotNull handler: Any): Boolean {
val requestUri = request.requestURI
if (requestUri == "/open/user/pwdLogin_v2") {
return true
}
//登录鉴定
val session = request.session
if (session == null) {
logger.warn("session为空...")
err(response, IbErrMsg.SESSION_ID_ERR)
return false
}
val userDetails = Sessions.getUserDetails(session)
if (userDetails == null) {
logger.warn("session中用户信息为空!")
err(response, IbErrMsg.NOT_LOGIN_IN)
return false
}
return true
}
private fun err(response: HttpServletResponse, msg: ErrMsg) {
response.contentType = "application/json;charset=UTF-8"
val err: Result<Unit> = Result.error(msg)
val writer = response.writer
writer.println(err)
}
}