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>
豆 :
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;
}
ご覧のとおり、すべて (リクエスト、レスポンス、セッションなど) を試しましたが、成功しませんでした。
誰でも私を案内できますか?