时间:2023-02-09 13:14:23 | 栏目:JAVA代码 | 点击:次
近日,客户针对我司系统做一些列漏洞扫描,最后总结通用漏洞有如下:
针对以上漏洞,分三篇文章分别记录解决方案,供后续回忆学习,本文先处理用户重复登录漏洞
系统采用spring boot搭建,spring security+ jwt 作为安全框架
RepeatLoginFilter.java
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String jwt = resolveToken(request); //重复登录只校验带有合法jwt的 放过验证码以及免鉴权的那些请求 if (null == jwt || "null".equalsIgnoreCase(jwt) || !this.tokenProvider.validateToken(jwt)) { filterChain.doFilter(request, response); return; } if (isAccessAllowed(jwt)) { filterChain.doFilter(request, response); return; } // 账号重复登录,跳转登录页面 logout logout(response); } /** * 重复登录核心校验 * @param token 当前token * @return true通过放行 */ private boolean isAccessAllowed(String token) { Authentication authentication = this.tokenProvider.getAuthentication(token); String redisToken = redisTemplate.opsForValue().get(Constants.PREFIX_LOGIN_USER+authentication.getName()); //redisToken为空 说明第一次登陆 放过并加入redis if(!StringUtils.hasLength(redisToken)){ redisTemplate.opsForValue().set(Constants.PREFIX_LOGIN_USER+authentication.getName(),token, 86400, TimeUnit.SECONDS); return true; } //redis token和当前token一致 说明是当前登陆用户访问 放过 if(token.equals(redisToken)){ return true; } //redis token和当前token不一致,比较两个token的创建时间,如果当前token大于redistoken 就说当前是最新的,放入redis并放过 //否则就说明redis里已有最新的token,当前token应该过期,不放行 Date date = this.tokenProvider.getIssueAt(token); Date redisDate = this.tokenProvider.getIssueAt(redisToken); if(date.after(redisDate)){ redisTemplate.opsForValue().set(Constants.PREFIX_LOGIN_USER+authentication.getName(),token, 86400, TimeUnit.SECONDS); return true; }else{ return false; } } /** * 获取jwt / private String resolveToken(HttpServletRequest request) { String bearerToken = request.getHeader("Authorization"); if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) { return bearerToken.substring(7); } return null; } /** * 返回退出信息到前台 * @param response */ private void logout(HttpServletResponse response){ response.setStatus(HttpStatus.FORBIDDEN.value()); response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); try { JSONObject resultObj = new JSONObject(); resultObj.putOnce("data","账号已在别的地方登录,请重新登录"); response.getWriter().print(resultObj.toString()); } catch (IOException e) { e.printStackTrace(); } }