五分钟带你玩转SpringSecurity(十)全网最佳方案,解决无跳转地址报错问题

简介: 五分钟带你玩转SpringSecurity(十)全网最佳方案,解决无跳转地址报错问题


前文说当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;
    }
}


相关文章
|
SQL 关系型数据库 MySQL
MySQL执行SQL文件出现【Unknown collation ‘utf8mb4_0900_ai_ci‘】的解决方案
MySQL执行SQL文件出现【Unknown collation ‘utf8mb4_0900_ai_ci‘】的解决方案
MySQL执行SQL文件出现【Unknown collation ‘utf8mb4_0900_ai_ci‘】的解决方案
|
12月前
|
Java 大数据 Apache
Excel工具-HUTOOL-输出Excel
基于Hutool与Apache POI,封装Excel写入功能,提供ExcelWriter和BigExcelWriter,支持List、Map、Bean等数据类型导出,可自定义样式、多Sheet操作,并避免内存溢出,适用于高效生成Excel文件及Web下载场景。
599 0
|
负载均衡
OpenFeign重试组件Retryer原理
该文章主要讲解了OpenFeign中的重试组件Retryer的工作原理及其实现细节。
OpenFeign重试组件Retryer原理
|
Java
为什么Java不支持多继承
本文讨论了Java不支持多继承的原因,包括避免菱形继承问题、简化编程语言和防止层次膨胀,同时提供了实现多继承效果的替代方案,如实现多接口、使用组合和继承加接口的方式。
760 0
为什么Java不支持多继承
|
缓存 算法 Java
认真学习Java集合之LinkedHashMap的实现原理
认真学习Java集合之LinkedHashMap的实现原理
640 0
认真学习Java集合之LinkedHashMap的实现原理
|
安全 Java 数据库连接
Springboot中启动器及自定义启动器
Springboot中启动器及自定义启动器
770 0
|
存储 缓存 算法
【算法与数据结构】LRU 算法
LRU(Least Recently Used),这种算法认为最近使用的数据是热点数据,下一次有很大概率会再次使用这个数据。而最近很少被使用的数据,很大概率下一次不会使用。所以当缓存容量满时,优先淘汰掉最近最少使用的数据。
|
存储 安全
LinkedHashMap深度解析
LinkedHashMap深度解析
695 0
LinkedHashMap深度解析
|
Java 测试技术 Spring
Spring系列(十一):@Profile 注解用法介绍
在Spring容器中如果存在同一类型的多个组件,可以使用@Profile注解标识实际要获取的是哪一个bean,这在不同的环境使用不同的变量的场景下非常有用。 最典型的例子:开发环境、测试环境、生产环境会配置不同的数据源,在尽量不修改代码的情况下,可以使用这个注解来动态指定要连接的数据源。
Spring系列(十一):@Profile 注解用法介绍
|
SpringCloudAlibaba 前端开发 安全
SpringCloud Alibaba实战二十六 - Oauth2认证服务器自定义异常
SpringCloud Alibaba实战二十六 - Oauth2认证服务器自定义异常
1037 0