带密码的JSF重定向
我试图做一个简单的页面,将重定向到OpenNMS服务器 我知道OpenNMS使用“基本授权”,当我这样做请求时:带密码的JSF重定向,jsf,opennms,Jsf,Opennms,我试图做一个简单的页面,将重定向到OpenNMS服务器 我知道OpenNMS使用“基本授权”,当我这样做请求时: URL url = new URL(webPage); URLConnection urlConnection = url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc); 我没有任何问题 但是,当我尝试在外部上下文(或类似的东西)中执行
URL url = new URL(webPage);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
我没有任何问题
但是,当我尝试在外部上下文(或类似的东西)中执行此操作时,我总是被重定向到登录页面
我的“代码”是:
xtml:
正如你所看到的,我想我已经尝试了一切(请求、响应、会话等),但没有成功
有人能指引我吗?好吧,我太蠢了。。。
OpenNMS具有spring安全性,所以我只需要更改表单,就完成了
<?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>
Facelet标题
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;
}
<?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>