前文说当 http://192.168.19.277:6001/authentication/loginPage 路径登录后没有成功后的转跳地址 所以会报错 以下代码可以解决
解决方案
在登录成功后 前文我们配置CustomAuthenticationSuccessHandler 返回json 这个类调用了onAuthenticationSuccess 那就重写onAuthenticationSuccess方法
@Component("customAuthenticationSuccessHandler")
public class CustomAuthenticationSuccessHandler extends CustomSavedRequestAwareAuthenticationSuccessHandler {
@Autowired
Utils utils;
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SysUser sysUser = (SysUser)authentication.getPrincipal();
logger.info("|" + "用户" + sysUser.getUsername() + "于" + sd.format(new Date()) + "通过web端登录系统,ip为"
+ utils.getIpAddr() + "。" + "|" + sd.format(new Date()) + "|" + sysUser.getUsername());
super.onAuthenticationSuccess(request, response, authentication);
}
}
重写onAuthenticationSuccess 大致意思就是 在登录成功后获取转跳地址 如果没有 我们指定给他一个固定地址 这里我们指定了百度
public class CustomSavedRequestAwareAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
protected final Log logger = LogFactory.getLog(this.getClass());
private RequestCache requestCache = new HttpSessionRequestCache();
public CustomSavedRequestAwareAuthenticationSuccessHandler() {}
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws ServletException, IOException {
// 获取code码路径 现在有两种判断
// 1.如果查询缓存有 定向到缓存 也就是数据库配置
// 2.如果前台已经登录 但是退回导致session丢失 跳转可道云页面
String targetUrl;
// 获取缓存
SavedRequest savedRequest = this.requestCache.getRequest(request, response);
if (savedRequest == null) {
// 跳转可道云
targetUrl = "https://www.baidu.com";
} else {
// 跳转缓存
targetUrl = savedRequest.getRedirectUrl();
}
String targetUrlParameter = this.getTargetUrlParameter();
if (!this.isAlwaysUseDefaultTargetUrl()
&& (targetUrlParameter == null || !StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
this.clearAuthenticationAttributes(request);
this.logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl);
this.getRedirectStrategy().sendRedirect(request, response, targetUrl);
} else {
this.requestCache.removeRequest(request, response);
super.onAuthenticationSuccess(request, response, authentication);
}
}
public void setRequestCache(RequestCache requestCache) {
this.requestCache = requestCache;
}
}