Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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
Spring Security-如果已登录,则重定向_Spring_Spring Security - Fatal编程技术网

Spring Security-如果已登录,则重定向

Spring Security-如果已登录,则重定向,spring,spring-security,Spring,Spring Security,我是春天的新手: 我不想让经过身份验证的用户访问登录页面。如果用户已经通过身份验证,那么为“/login”处理重定向的正确方法是什么?比如说,如果已经登录,我想重定向到“/index” 我在登录时尝试了“isAnonomous()”,但它会重定向到“拒绝访问”页面 <security:http auto-config="true" use-expressions="true" ...> <form-login login-processing-url="/resourc

我是春天的新手:

我不想让经过身份验证的用户访问登录页面。如果用户已经通过身份验证,那么为“/login”处理重定向的正确方法是什么?比如说,如果已经登录,我想重定向到“/index”

我在登录时尝试了“isAnonomous()”,但它会重定向到“拒绝访问”页面

<security:http auto-config="true" use-expressions="true" ...>
    <form-login login-processing-url="/resources/j_spring_security_check"
                 default-target-url="/index"
                login-page="/login" authentication-failure-url="/login?login_error=t" />
    <logout logout-url="/resources/j_spring_security_logout"  />
   ...
  <security:intercept-url pattern="/login" access="permitAll" />
  <security:intercept-url pattern="/**" access="isAuthenticated()" />
</security:http>

...

在登录页面的控制器功能中:

  • 检查用户是否已登录

  • 在这种情况下,将他转发/重定向到索引页

  • 相关代码:

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    
    if (!(auth instanceof AnonymousAuthenticationToken)) {
    
        /* The user is logged in :) */
        return new ModelAndView("forward:/index");
    }
    
    更新 或者在另一个场景中,映射可能包含
    路径变量
    ,如
    @GetMapping(path=“/user/{id}”)
    ,在这种情况下,您也可以实现此逻辑:

    @GetMapping(value = "/login")
    public String getLogin() throws Exception {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    
        if (!(auth instanceof AnonymousAuthenticationToken)) {
            User loggedInUser = userService.findByEmail(auth.getName())
                        .orElseThrow(Exception::new);
            /* The user is logged in :) */
            return "redirect:/user/" + loggedInUser.getUserId();
        }
        return "login";
    }
    
    嘿,你能做到

    <h:head>
    <sec:authorize access="isAuthenticated()">
        <meta http-equiv="refresh" content="0;url=http://your index.xhtml url (full url)" /> 
    </sec:authorize>
    </h:head>
    
    
    
    这个方法非常简单方便,不是吗?

    login.xhtml

    <h:head >
        <f:metadata>
          <f:event type="preRenderView" listener="#{loginBean.onPageLoad}"/>
      </f:metadata>
    </h:head>
    

    要成功从登录页面重定向,如果用户已登录,请将以下内容添加到login.jsp:

    将安全标记库标题添加到jsp的顶部:

    <%@taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>
    
    
    
    然后在“头部”标签内添加以下标签(最好靠近顶部):

    
    
    如果访问登录页面的用户已经登录,这将重定向到main.html(或main.jsp映射到的任何对象)


    通过控制器执行此操作对我不起作用,因为有效的登录页面实践是让spring security的“form login”bean完成所有重定向工作,因此,没有登录控制器可供我修改。

    此链接解决了此问题可能重复的问题。请查看此链接,它仅适用于登录->主页之类的情况。如果我已经登录并在地址栏中键入登录url,该怎么办?这将是有意义的重定向到主页(或无论我已经是)再次。。。
    <%@taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>
    
    <sec:authorize access="isAuthenticated()">
        <% response.sendRedirect("main"); %>
    </sec:authorize>