JSF重定向密码

时间:2014-07-08 14:57:19

标签: jsf opennms

我正在尝试创建一个将重定向到OpenNMS服务器的简单页面。

我知道OpenNMS使用“基本授权”,当我执行这样的请求时:

 URL url = new URL(webPage);
 URLConnection urlConnection = url.openConnection();
 urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);

我没有任何问题。

然而,当我尝试使用ExternalContext(或类似的东西)时,我总是被重定向到登录页面。

我的“代码”是这样的:

XTML:

<?xml version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <p:inputText value="#{newJSFManagedBean.user}" label="user" />
            <p:inputText value="#{newJSFManagedBean.pass}" label="pass" />


            <p:commandButton value="redirect1" action="#{newJSFManagedBean.redirect}"/>
<!--            <ui:include src="http://10.46.16.85:8980/opennms/"/>-->

        </h:form>
    </h:body>
</html>

Bean:

public String redirect() throws IOException, ServletException {
        System.err.println("" + this.pass + "" + this.user);
        FacesContext facesContext = FacesContext.getCurrentInstance();

        String name = "admin";
        String password = "admin";
        String authString = name + ":" + password;
        System.out.println("auth string: " + authString);

        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
        String authStringEnc = new String(authEncBytes);

        Cookie userCookie = new Cookie("Authorization", "Basic " + authStringEnc);

        userCookie.setMaxAge(3600);


        ((HttpServletResponse) facesContext.getExternalContext()
                .getResponse()).addCookie(userCookie);




        HttpServletResponse response = ((HttpServletResponse) facesContext.getExternalContext().getResponse());
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        System.out.println("blicaivens"+req.getHeader("Authorization"));


        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);

        session.setAttribute("Authorization", "Basic " + authStringEnc);

        response.setHeader("Authorization", "Basic " + authStringEnc);
        req.setAttribute("Authorization", "Basic " + authStringEnc);

        externalContext.addResponseHeader("Authorization", "Basic " + authStringEnc);
        externalContext.redirect("http://10.46.16.85:8980/opennms/index.jsp?");


        return null;
    }

正如你所看到的,我认为我已经尝试了一切(请求,回复,会话等),但我没有成功。

任何人都可以指导我吗?

1 个答案:

答案 0 :(得分:0)

好吧,这对我来说很愚蠢...... OpenNMS具有弹簧安全性,因此我只需要更改表单即可。

<?xml version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <form action="http://10.46.16.85:8980/opennms/j_spring_security_check"  method="post" >
        <!--<h:form>-->

            <h:inputText value="#{newJSFManagedBean.user}" label="user" id="j_username"/>

            <h:inputText value="#{newJSFManagedBean.pass}" label="pass" id="j_password"/>


            <h:commandButton value="redirect1" action="#{newJSFManagedBean.redirect}"/>
<!--            <ui:include src="http://10.46.16.85:8980/opennms/"/>-->

        <!--</h:form>-->

        </form>
    </h:body>
</html>