Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 身份验证后重定向到受保护页面_Java_Spring_Spring Boot_Spring Security_Kotlin - Fatal编程技术网

Java 身份验证后重定向到受保护页面

Java 身份验证后重定向到受保护页面,java,spring,spring-boot,spring-security,kotlin,Java,Spring,Spring Boot,Spring Security,Kotlin,默认情况下,spring security after authentication会将您重定向到之前尝试访问的受保护页面 当我实现自己的成功处理程序时 @Component class MyS: AuthenticationSuccessHandler { override fun onAuthenticationSuccess(request: HttpServletRequest?, response: HttpServletResponse?, authentication: A

默认情况下,spring security after authentication会将您重定向到之前尝试访问的受保护页面

当我实现自己的成功处理程序时

@Component
class MyS: AuthenticationSuccessHandler {
    override fun onAuthenticationSuccess(request: HttpServletRequest?, response: HttpServletResponse?, authentication: Authentication?) {

        response?.sendRedirect(request?.getHeader(HttpHeaders.REFERER))

    }
}

我不能达到同样的效果。我尝试重定向到referer,但在本例中referer是/en/login页面

基本上:

  • 用户尝试访问受保护的url
    /protected
  • 将用户重定向到
    /login
    页面
  • 身份验证后,应再次将用户重定向到
    /protected

  • 如何使用自定义successHandler?在我的项目中,我使用了满足我要求的
    DefaultSavedRequest
    DefaultSavedRequest
    类由AbstractAuthenticationProcessingFilter和SavedRequestAwareWrapper用于在成功身份验证后复制请求。ExceptionTranslationFilter在发生身份验证异常时存储此类的实例


    多亏了Mhod的回答,这就成功了

    @Component
    class MyS: AuthenticationSuccessHandler {
        override fun onAuthenticationSuccess(request: HttpServletRequest?, response: HttpServletResponse?, authentication: Authentication?) {
    
            val defaultSavedRequest = request?.session?.getAttribute("SPRING_SECURITY_SAVED_REQUEST") as DefaultSavedRequest
            response?.sendRedirect(defaultSavedRequest.requestURI)
    
        }
    }
    
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) 
            throws IOException, ServletException {
        DefaultSavedRequest defaultSavedRequest = (DefaultSavedRequest) session.getAttribute("SPRING_SECURITY_SAVED_REQUEST");
        if(defaultSavedRequest != null){
           String targetURL = defaultSavedRequest.getRedirectUrl();
           redirectStrategy.sendRedirect(request, response, targetURL);
           return;
        }
    }
    
    @Component
    class MyS: AuthenticationSuccessHandler {
        override fun onAuthenticationSuccess(request: HttpServletRequest?, response: HttpServletResponse?, authentication: Authentication?) {
    
            val defaultSavedRequest = request?.session?.getAttribute("SPRING_SECURITY_SAVED_REQUEST") as DefaultSavedRequest
            response?.sendRedirect(defaultSavedRequest.requestURI)
    
        }
    }