servletResponse instanceof HttpServletResponse
时间: 2023-11-09 18:17:55 浏览: 111
是的,servletResponse instanceof HttpServletResponse 是用来检查 servletResponse 对象是否是 HttpServletResponse 类型的实例。这个语句将返回一个布尔值,如果 servletResponse 是 HttpServletResponse 类的实例,则返回 true,否则返回 false。
相关问题
下面这段代码是什么意思@Pointcut("@annotation(com.tplinknbu.paymentcenter.deprecated.annotation.LogRequired)") public void logPointcut() { // 切点定义 } @Around("logPointcut()") public Object handlerStat(ProceedingJoinPoint pjp) throws Throwable { MethodSignature signature = (MethodSignature) pjp.getSignature(); Method method = signature.getMethod(); ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); Set<Entry<String, String[]>> entries = getHttpRequestPrameterSet(requestAttributes); Map<Object, Object> parameterMap = new HashMap<>(); for (final Entry<String, String[]> entry : entries) { parameterMap.put(entry.getKey(), Arrays.toString(entry.getValue())); } Object response = null; long startAtNs = systemTime.getMilliseconds(); try { response = pjp.proceed(); } finally { List<Object> requestArgs = new ArrayList<>(); Arrays.asList(pjp.getArgs()).forEach(obj -> { if (obj instanceof MultipartFile) { // } else if (obj instanceof HttpServletRequest || obj instanceof HttpServletResponse || obj instanceof ServletRequest || obj instanceof ServletResponse) { // mapper.valueToTree 无法转化 HttpServletRequest } else { requestArgs.add(obj); } }); long durationInMills = systemTime.getMilliseconds() - startAtNs; toJsonPoint(method.getName(), requestArgs, parameterMap, response, durationInMills); } return response; }
这段代码是一个基于 Spring AOP 的切面,用于记录某些方法的运行时间、参数、返回结果等信息。首先,通过 @Pointcut 注解定义一个切点,该切点的条件是被 @LogRequired 注解标注的方法。然后,使用 @Around 注解定义一个环绕通知,该通知会在切点处切入,执行被切入的方法。在通知中,首先记录请求参数,然后执行被切入的方法,并记录执行时间和返回结果,最后将这些信息序列化成 JSON 格式,输出到日志中。需要注意的是,该切面对 HttpServletRequest、HttpServletResponse 等对象不做处理。
package com.interceptor; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSONObject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import com.annotation.IgnoreAuth; import com.entity.EIException; import com.entity.TokenEntity; import com.service.TokenService; import com.utils.R; /** * 权限(Token)验证 */ @Component public class AuthorizationInterceptor implements HandlerInterceptor { public static final String LOGIN_TOKEN_KEY = "Token"; @Autowired private TokenService tokenService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //支持跨域请求 response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Headers", "x-requested-with,request-source,Token, Origin,imgType, Content-Type, cache-control,postman-token,Cookie, Accept,authorization"); response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); IgnoreAuth annotation; if (handler instanceof HandlerMethod) { annotation = ((HandlerMethod) handler).getMethodAnnotation(IgnoreAuth.class); } else { return true; } //从header中获取token String token = request.getHeader(LOGIN_TOKEN_KEY); /** * 不需要验证权限的方法直接放过 */ if(annotation!=null) { return true; } TokenEntity tokenEntity = null; if(StringUtils.isNotBlank(token)) { tokenEntity = tokenService.getTokenEntity(token); } if(tokenEntity != null) { request.getSession().setAttribute("userId", tokenEntity.getUserid()); request.getSession().setAttribute("role", tokenEntity.getRole()); request.getSession().setAttribute("tableName", tokenEntity.getTablename()); request.getSession().setAttribute("username", tokenEntity.getUsername()); return true; } PrintWriter writer = null; response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); try { writer = response.getWriter(); writer.print(JSONObject.toJSONString(R.error(401, "请先登录"))); } finally { if(writer != null){ writer.close(); } } // throw new EIException("请先登录", 401); return false; } }
这段代码是一个Spring Boot应用程序中的拦截器,用于验证用户的Token令牌是否有效。它会在每个请求之前拦截并验证Token,如果Token有效则允许请求继续进行,否则返回一个401错误并提示用户先登录。其中,@IgnoreAuth注解表示该请求不需要验证Token。这段代码还支持跨域请求,设置了一些CORS相关的header信息。
阅读全文