Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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 登录后重定向不起作用(jsf glassfish)_Java_Jsf_Glassfish - Fatal编程技术网

Java 登录后重定向不起作用(jsf glassfish)

Java 登录后重定向不起作用(jsf glassfish),java,jsf,glassfish,Java,Jsf,Glassfish,我在glassfish jee/jsf jpa场景中, 当我尝试访问其中一个ManagerBean中的受保护函数时,如果我不满足@RolesAllowed指定的要求,我会收到web.xml中配置的登录页面的提示。 登录后,我被重定向到通用主页home.xhtml 当我的函数返回void时,它会在数据库中执行一些业务,我希望在登录后被重定向到相同的页面 当我尝试访问受保护的页面时,重定向功能工作得非常完美,但是由于受保护的函数不返回任何内容,因此我似乎不知从何而来,也不知从何而来。original

我在glassfish jee/jsf jpa场景中, 当我尝试访问其中一个ManagerBean中的受保护函数时,如果我不满足@RolesAllowed指定的要求,我会收到web.xml中配置的登录页面的提示。 登录后,我被重定向到通用主页home.xhtml

当我的函数返回void时,它会在数据库中执行一些业务,我希望在登录后被重定向到相同的页面

当我尝试访问受保护的页面时,重定向功能工作得非常完美,但是由于受保护的函数不返回任何内容,因此我似乎不知从何而来,也不知从何而来。originalURL为null,有关详细信息,请参阅下面的代码

我在StackOverflow上搜索了一整天,我读了很多答案,但没有一个有效,到目前为止,这些是我的代码行

login.xhtml

   <h:form>
        <h:outputLabel for="username" value="Username" />
        <h:inputText id="username" value="#{LoginManagedBean.username}"
            required="true" />
        <h:message for="username" />
        <br />
        <h:outputLabel for="password" value="Password" />
        <h:inputSecret id="password" value="#{LoginManagedBean.password}"
            required="true" />
        <h:message for="password" />
        <br />
        <h:commandButton value="Login" action="#{LoginManagedBean.login}" />
        <h:messages globalOnly="true" />

    </h:form>
web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>TravelDreamDynamicWeb</display-name>
  <welcome-file-list>
    <welcome-file>home.jsf</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
    <url-pattern>*.xhtml</url-pattern>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
      <form-login-page>/login.xhtml</form-login-page>
      <form-error-page>/loginError.xhtml</form-error-page>
    </form-login-config>
  </login-config>
  <error-page>
    <error-code>403</error-code>
    <location>/loginError.xhtml</location>
  </error-page>
  <error-page>
    <error-code>500</error-code>
    <location>/login.xhtml</location>
  </error-page>
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Pagine Cliente</web-resource-name>
      <description></description>
      <url-pattern>/cliente/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>CLIENTE</role-name>
    </auth-constraint>
  </security-constraint>
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Pagine Impiegato</web-resource-name>
      <description></description>
      <url-pattern>/impiegato/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>IMPIEGATO</role-name>
    </auth-constraint>
  </security-constraint>
</web-app>
具体函数的实现只是为了了解情况,很抱歉它是意大利语和英语的混合体

@Override
@RolesAllowed({"CLIENTE"})
public void aggiungiACarrelloMgr(int idPacchetto) {
    String username=FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
    Query query=em.createQuery("SELECT u FROM UtenteRegistrato u WHERE u.username='"+username+"'");
    List<UtenteRegistrato> utente= query.getResultList();
    int idCarrello=utente.get(0).getIdCarrello();
    Carrello carrello=new Carrello(new CarrelloPK(idCarrello,idPacchetto));
    em.persist(carrello);
}
Visualizapacchetti.xhtml中的命令按钮对未登录的用户可见,该按钮尝试调用受保护的函数,该函数在启动javax.ejb.AccessLocalException后触发forward to login页面

<h:commandButton action="#{PacchettoManagedBean.aggiungiACarrello(PacchettoManagedBean.pacchetti.get(statoPack.index).idPacchetto)}" value="+CARRELLO" />
谢谢你抽出时间, 如果我的解释遗漏了什么,请告诉我,并为我糟糕的英语道歉


Massimo

嗨,Massimo,欢迎使用堆栈溢出。我很快检查了您的代码,您的LoginManagedBean似乎是ViewScope,这意味着它会一直保持状态,直到视图相同为止。在您的情况下,您请求的login.xhtml可能与原始视图不同,因此LoginManagedBean的状态丢失

尝试在LoginManagedBean中使用@SessionScoped更改@ViewScoped


我不知道它是否符合您的逻辑,以防它不只是为登录相关的东西创建会话范围的bean,因为您无论如何都需要它。查看您的方法名称,我假设您正在构建一个电子商务,我也是意大利人,因此您可能已经有了会话范围的bean。

为了解决我的问题,我使用了本页中描述的自定义登录版本

注意,我不得不改变建议

<f:view>
<ui:param name="originalURL" value="#{request.requestURI}?#{request.queryString}" />
<f:metadata>
<f:event rendered="#{not userSession.loggedIn}" type="preRenderView" listener="#{userSession.recordOriginalURL(originalURL)}" />
</f:metadata>
</f:view>
对此

<f:view>
<f:metadata>
<ui:param name="originalURL" value="#{request.requestURI}?#{request.queryString}" />
<f:event rendered="#{not userSession.loggedIn}" type="preRenderView" listener="#{userSession.recordOriginalURL(originalURL)}" />
</f:metadata>
</f:view> 

希望这能帮助有同样问题的人。祝你好运,谢谢你的回答。很抱歉耽搁了,但我们必须尽快交付我们的项目,我没有时间检查您的答案是否有效。现在我已经用另一种方式找到了解决方案,我将把它作为我自己问题的答案发布。当所有的考试都完成后,我一定会尝试你的建议,然后带着答案回来,但现在我不能。没问题,无论如何都应该行得通。我检查了您的解决方案,并将重定向URL作为GET参数传递,这听起来不是一个好主意。
<f:view>
<f:metadata>
<ui:param name="originalURL" value="#{request.requestURI}?#{request.queryString}" />
<f:event rendered="#{not userSession.loggedIn}" type="preRenderView" listener="#{userSession.recordOriginalURL(originalURL)}" />
</f:metadata>
</f:view>