|
|
|
@ -1,10 +1,12 @@ |
|
|
|
|
package com.daqing.financial.hrauth.filter; |
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
import com.daqing.financial.hrauth.enums.Constants; |
|
|
|
|
import com.daqing.financial.hrauth.handle.SecurityUser; |
|
|
|
|
import com.daqing.financial.hrauth.service.impl.UserDetailsServiceImpl; |
|
|
|
|
import com.daqing.financial.hrauth.util.MultiReadHttpServletRequest; |
|
|
|
|
import com.daqing.financial.hrauth.util.MultiReadHttpServletResponse; |
|
|
|
|
import com.daqing.framework.model.response.ResponseResult; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
|
import org.springframework.security.access.AccessDeniedException; |
|
|
|
@ -19,6 +21,7 @@ import javax.servlet.ServletException; |
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.PrintWriter; |
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -38,6 +41,25 @@ public class MyAuthenticationFilter extends OncePerRequestFilter { |
|
|
|
|
this.userDetailsService = userDetailsService; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void printJson(HttpServletResponse response) { |
|
|
|
|
ResponseResult responseResult = new ResponseResult(false,401,"TOKEN已过期,请重新登录","TOKEN已过期,请重新登录"); |
|
|
|
|
String content = JSON.toJSONString(responseResult); |
|
|
|
|
printContent(response, content); |
|
|
|
|
} |
|
|
|
|
private static void printContent(HttpServletResponse response, String content) { |
|
|
|
|
try { |
|
|
|
|
response.reset(); |
|
|
|
|
response.setContentType("application/json"); |
|
|
|
|
response.setHeader("Cache-Control", "no-store"); |
|
|
|
|
response.setCharacterEncoding("UTF-8"); |
|
|
|
|
PrintWriter pw = response.getWriter(); |
|
|
|
|
pw.write(content); |
|
|
|
|
pw.flush(); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { |
|
|
|
|
System.out.println("请求头类型: " + request.getContentType()); |
|
|
|
@ -70,15 +92,17 @@ public class MyAuthenticationFilter extends OncePerRequestFilter { |
|
|
|
|
// 检查token
|
|
|
|
|
SecurityUser securityUser = userDetailsService.getUserByToken(token); |
|
|
|
|
if (securityUser == null || securityUser.getCurrentUserInfo() == null) { |
|
|
|
|
throw new AccessDeniedException("TOKEN已过期,请重新登录!"); |
|
|
|
|
//throw new AccessDeniedException("TOKEN已过期,请重新登录!");
|
|
|
|
|
printJson(response); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(securityUser, null, securityUser.getAuthorities()); |
|
|
|
|
// 全局注入角色权限信息和登录用户基本信息
|
|
|
|
|
SecurityContextHolder.getContext().setAuthentication(authentication); |
|
|
|
|
} |
|
|
|
|
/* else { |
|
|
|
|
else { |
|
|
|
|
throw new AccessDeniedException("TOKEN不存在,请重新登录!"); |
|
|
|
|
}*/ |
|
|
|
|
} |
|
|
|
|
filterChain.doFilter(wrappedRequest, wrappedResponse); |
|
|
|
|
} finally { |
|
|
|
|
stopWatch.stop(); |
|
|
|
|