有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

登录后的java重定向不起作用(jsfglassfish)

我在glassfish jee/jsf jpa场景中, 当我试图访问其中一个ManagerBean中的受保护功能时(我不符合@RolesAllowed指定的要求),我会收到web中配置的登录页面的提示。xml。 登录后,我被重定向到通用主页(home.xhtml)

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

当我尝试访问受保护的页面时,重定向功能工作得非常完美,但受保护的函数不会返回任何内容,似乎“我不知从何而来,我不知从何而来”(originalURL为null,请参阅下面的代码了解详细信息)

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

登录。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>

LoginManagedBean。爪哇

   package userInterfaces;


 //imports

/**
 * Session Bean implementation class LoginManagedBean
 */
@ManagedBean(name="LoginManagedBean")
@ViewScoped

public class LoginManagedBean {
    //getters and setters

    private String username;


    private String password;
    private String originalURL;

    public LoginManagedBean() {
        // TODO Auto-generated constructor stub
    }

    @PostConstruct
    public void init() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        originalURL = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);
        String uri = ((HttpServletRequest) externalContext.getRequest()).getRequestURI();

        if (originalURL == null) {
            originalURL = externalContext.getRequestContextPath() + "/home.xhtml";
        } else {
            String originalQuery = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_QUERY_STRING);

            if (originalQuery != null) {
                originalURL += "?" + originalQuery;
            }
        }
    }

    public void login() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext externalContext = context.getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();

        try {
            request.login(username, password); 
            externalContext.redirect(originalURL);
        } catch (ServletException e) {
          context.addMessage(null, new FacesMessage("Unknown login"));
        }
    }
       public void logout() throws IOException {
            ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
            externalContext.invalidateSession();
            externalContext.redirect(externalContext.getRequestContextPath() + "/login.xhtml");
        }

}

网络。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后触发转发到登录页面)

<h:commandButton action="#{PacchettoManagedBean.aggiungiACarrello(PacchettoManagedBean.pacchetti.get(statoPack.index).idPacchetto)}" value="+CARRELLO" />

谢谢你抽出时间, 如果我的解释遗漏了什么,请告诉我,并为我糟糕的英语道歉

马西莫


共 (1) 个答案

  1. # 1 楼答案

    为了解决我的问题,我使用了本页http://mrj4mes.blogspot.it/2013/04/how-to-redirect-to-original-page-after.html中描述的自定义登录版本

    注意,我不得不改变建议

    <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> 
    

    希望这能帮助有同样问题的人。祝你好运