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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.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 成功登录后无法重定向用户_Spring_Jsf_Spring Security - Fatal编程技术网

Spring 成功登录后无法重定向用户

Spring 成功登录后无法重定向用户,spring,jsf,spring-security,Spring,Jsf,Spring Security,我正在使用primefaces和spring security 在我的支持bean中,下面的方法运行良好。它调用用户详细信息服务并验证或拒绝登录尝试。 我的问题是重定向。身份验证后,将用户重定向到所需页面的正确方式是什么?目前,我可以看到用户已通过身份验证,但仍然显示登录表单 public String login(){ try{ Authentication request = new UsernamePasswordAuthenticationToken(this.

我正在使用primefaces和spring security 在我的支持bean中,下面的方法运行良好。它调用用户详细信息服务并验证或拒绝登录尝试。 我的问题是重定向。身份验证后,将用户重定向到所需页面的正确方式是什么?目前,我可以看到用户已通过身份验证,但仍然显示登录表单

public String login(){

    try{
        Authentication request = new UsernamePasswordAuthenticationToken(this.getUsername(), this.getPassword());
        Authentication result = authenticationManager.authenticate(request);
        SecurityContextHolder.getContext().setAuthentication(result);
    }
    catch(Exception e){

        e.printStackTrace();
        return "incorrect";
    }
 return "correct"; 

}



<http auto-config="true">

     <intercept-url pattern="/web/*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
     <intercept-url pattern="/**" access="ROLE_USER" />

     <form-login login-page="/web/login.xhtml" 
      default-target-url="/main.xhtml"
     always-use-default-target="true"     />

</http>

<authentication-manager alias="authenticationManager">

        <authentication-provider  user-service-ref="kullaniciDetayServisi" />

</authentication-manager>




</beans:beans>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
</h:head>
<h:body>
    <div align="center" style="">
        <h:form  id="loginFormId" prependId="false">
                <div id="loginFieldsPnlId">
                    <div id="loginFieldUsrContId">
                        <h:outputText id="outTxtUserNameId" value="Username: " name="outTxtUserNameNm"></h:outputText>
                        <h:inputText id="userName" required="true" value="#{loginBean.username}" requiredMessage="Please enter username"></h:inputText>
                        <h:outputLabel id="outLblUserNameId" for="userName" name="outLblUserNameNm"></h:outputLabel>
                    </div>
                    <div id="loginFieldPassContId">
                        <h:outputText id="outTxtPasswordId" value="Password: " name="outTxtPasswordNm"></h:outputText>
                        <h:inputSecret id="password"  required="true" value="#{loginBean.password}" requiredMessage="Please enter password" name="inTxtPasswordNm"></h:inputSecret>
                        <h:outputLabel id="outLblPasswordId" for="password" name="outLblPasswordNm"></h:outputLabel>
                    </div>
                </div>
                <div id="loginBtnPanelId">
                    <h:commandButton id="btnLoginId" value="Login" action="#{loginBean.login}"  ajax="false"></h:commandButton>
                    <h:commandButton id="btnCancelId" value="Cancel" action="#{loginBean.cancel}"   immediate="true" update="loginFormId" ajax="false"></h:commandButton>
                </div>
        </h:form>
    </div>
    <div>
        <h:messages></h:messages>
    </div>
</h:body>
</html>
公共字符串登录(){
试一试{
身份验证请求=新用户名PasswordAuthenticationToken(this.getUsername(),this.getPassword());
验证结果=authenticationManager.Authentication(请求);
SecurityContextHolder.getContext().setAuthentication(结果);
}
捕获(例外e){
e、 printStackTrace();
返回“不正确”;
}
返回“正确”;
}

看起来您忘记在控制器方法中映射
正确的
不正确的
路径

如果我想重定向,我可能会使用

return "redirect:/correct/" + user.getUserName();
并在映射此新请求的新控制器方法中处理此新请求。 或

从处理登录请求的控制器方法返回类似的内容

在JSF的
/web/web-INF/faces config.xml
中使用适当的导航规则如下:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <navigation-rule>
        <from-view-id>/index.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>incorrect</from-outcome>
            <to-view-id>/failure.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>correct</from-outcome>
            <to-view-id>/sucess.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>

/index.xhtml
不正确的
/failure.xhtml
对的
/success.xhtml

这两者之间有什么关系?它能与普通jsf一起工作吗?它与普通jsf是一样的。我添加了我们的登录表单。PF是项目的前端库,如果它在普通JSF中失败,我将删除PrimeFaces标记,并请创建一个与您之前的问题有何不同的问题?在这个问题上,我不是在寻找某个问题的解决方法,而是在寻找重定向return“/main.xhtml?faces redirect=true”的正确方法;给我开了个玩笑。谢谢Rohit。你让我开心了。
/greeting.xhtml success/response.xhtml
有关JSF的详细信息,请查看此官方网站。不应该使用这种伎俩。应该正确处理事件。
重定向:
是一个与Spring MVC相关的工件,不能与JSF一起使用。Spring MVC是一个完全不同的东西。它不等于弹簧。
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <navigation-rule>
        <from-view-id>/index.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>incorrect</from-outcome>
            <to-view-id>/failure.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>correct</from-outcome>
            <to-view-id>/sucess.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>