我正在尝试做一个将重定向到 OpenNMS 服务器的简单页面。
我知道 OpenNMS 使用“基本授权”,当我执行这样的请求时:
URL url = new URL(webPage);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
我没有任何问题。
但是,当我尝试使用 ExternalContext(或类似的东西)时,我总是被重定向到登录页面。
我的“代码”是这样的:
xml:
<?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>
豆 :
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;
}
如您所见,我认为我已经尝试了所有方法(请求、响应、会话等),但没有成功。
谁能指导我?