public interface HandlerInterceptor {
/**
* Intercept the execution of a handler.
Called after HandlerMapping determined
* an appropriate handler object, but before
HandlerAdapter invokes the handler.
*
* 在業(yè)務處理器處理請求之前被調用,。預處理,可以進行編碼,、安全控制,、權限校驗等處理
*
* handler :controller內的方法,,可以通過HandlerMethod method= ((HandlerMethod)handler);獲取到@RequestMapping
*/
boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception;
/**
* Intercept the execution of a handler.
Called after HandlerAdapter actually
* invoked the handler, but before the
DispatcherServlet renders the view.
*
* 在業(yè)務處理器處理請求執(zhí)行完成后,生成視圖之前執(zhí)行,。后處理(調用了Service并返回ModelAndView,,但未進行頁面渲染),有機會修改ModelAndView
*/
void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception;
/**
* Callback after completion of request
processing, that is, after rendering
* the view. Will be called on any outcome of
handler execution, thus allows
* for proper resource cleanup.
*
* 在DispatcherServlet完全處理完請求后被調用,,可用于清理資源等,。返回處理(已經渲染了頁面)
*
*/
void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) throws Exception;
}
// 你可以基于有些url進行攔截
@Configuration
public class UserSecurityInterceptor extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry)
{
String[] securityUrls = new String[]{ "/**" };
String[] excludeUrls = new String[]{ "/**/esb/**" , "/**/dictionary/**" };
registry.addInterceptor(userLoginInterceptor()).excludePathPatterns(excludeUrls).addPathPatterns(securityUrls);
super .addInterceptors(registry);
}
/** fixed: url 中包含 // 報錯
*
org.springframework.security.web.firewall.RequestRejectedException: The
request was rejected because the URL was not normalized.
* @return
*/
@Bean
public HttpFirewall
allowUrlEncodedSlashHttpFirewall() {
DefaultHttpFirewall firewall = new DefaultHttpFirewall();
firewall.setAllowUrlEncodedSlash( true );
return firewall;
}
@Bean
public AuthInterceptor userLoginInterceptor() {
return new AuthInterceptor();
}
public class AuthInterceptor implements HandlerInterceptor {
public Logger logger = LoggerFactory.getLogger(AuthInterceptor. class );
@Autowired
private ApplicationContext applicationContext;
public AuthInterceptor() {
}
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
LoginUserInfo user = null ;
try {
user = (LoginUserInfo)
SSOUserUtils.getCurrentLoginUser();
} catch (Exception e) {
logger.error( " 從SSO登錄信息中獲取用戶信息失敗,! 詳細錯誤信息:%s" , e);
throw new ServletException( " 從SSO登錄信息中獲取用戶信息失?。?quot; , e);
}
String[] profiles =
applicationContext.getEnvironment().getActiveProfiles();
if (!Arrays.isNullOrEmpty(profiles)) {
if ( "dev" .equals(profiles[ 0 ])) {
return true ;
}
}
if (user == null ||
UserUtils.ANONYMOUS_ROLE_ID.equals(user.getRoleId())) {
throw new ServletException( " 獲取登錄用戶信息失??!" );
}
return true ;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
}
|