59 lines
2.1 KiB
Java
59 lines
2.1 KiB
Java
/**
|
||
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
|
||
* <p>
|
||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE;
|
||
* you may not use this file except in compliance with the License.
|
||
* You may obtain a copy of the License at
|
||
* <p>
|
||
* http://www.gnu.org/licenses/lgpl.html
|
||
* <p>
|
||
* Unless required by applicable law or agreed to in writing, software
|
||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
* See the License for the specific language governing permissions and
|
||
* limitations under the License.
|
||
*/
|
||
package org.springblade.core.secure.interceptor;
|
||
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springblade.core.secure.utils.SecureUtil;
|
||
import org.springblade.core.tool.api.R;
|
||
import org.springblade.core.tool.api.ResultCode;
|
||
import org.springblade.core.tool.jackson.JsonUtil;
|
||
import org.springblade.core.tool.utils.StringPool;
|
||
import org.springblade.core.tool.utils.WebUtil;
|
||
import org.springframework.http.MediaType;
|
||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
||
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import javax.servlet.http.HttpServletResponse;
|
||
import java.io.IOException;
|
||
import java.util.Objects;
|
||
|
||
/**
|
||
* jwt拦截器校验
|
||
*/
|
||
@Slf4j
|
||
public class SecureInterceptor extends HandlerInterceptorAdapter {
|
||
|
||
@Override
|
||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||
if (null != SecureUtil.getUser().getUserId()) {
|
||
return true;
|
||
} else {
|
||
log.warn("签名认证失败,请求接口:{},请求IP:{},请求参数:{}", request.getRequestURI(), WebUtil.getIP(request), JsonUtil.toJson(request.getParameterMap()));
|
||
R result = R.failure(ResultCode.UN_AUTHORIZED);
|
||
response.setCharacterEncoding(StringPool.UTF_8);
|
||
response.setHeader("Content-type", MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||
response.setStatus(HttpServletResponse.SC_OK);
|
||
try {
|
||
response.getWriter().write(Objects.requireNonNull(JsonUtil.toJson(result)));
|
||
} catch (IOException ex) {
|
||
log.error(ex.getMessage());
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
|
||
}
|