Configuring ADF Security-Creating Users and roles at runtime on Weblogic server
Before beginning working on ADF Security, it is required to explicitly add these three libraries
To add them Just Double click on the ViewController project,Libraries and classpath ,Add Jar /Directory
The location of the first two libraries is
C:\Oracle\Middleware\Oracle_Home\wlserver\modules
The location of the last one"wls-api.jar" is
C:\Oracle\Middleware\Oracle_Home\wlserver\server\lib
First ,we will start implementing login method.Create a managed bean"loginBean"
private void authenticateUser(String un, String pw) {
byte[] password = pw.getBytes();
FacesContext fctx = FacesContext.getCurrentInstance();
HttpServletRequest request = null;
request = (HttpServletRequest) fctx.getExternalContext().getRequest();
try {
Subject subject = Authentication.login(new URLCallbackHandler(un, password));
weblogic.servlet.security.ServletAuthentication.runAs(subject, request);
String loginUrl = "/adfAuthentication?success_url=/faces/home";
HttpServletResponse response = (HttpServletResponse) fctx.getExternalContext().getResponse();
sendForward(request, response, loginUrl);
} catch (FailedLoginException fle) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,"Invalid user name or password.",null));
// context.addMessage(null,
// new FacesMessage(FacesMessage.SEVERITY_ERROR,
// JSFUtils.getStringFromResourceBundle("INVALID_LOGIN_MSG"), null));
} catch (LoginException le) {
reportUnexpectedLoginError("LoginException", le);
}
}
private void sendForward(HttpServletRequest request, HttpServletResponse response, String forwardUrl) {
FacesContext ctx = FacesContext.getCurrentInstance();
RequestDispatcher dispatcher = request.getRequestDispatcher(forwardUrl);
try {
dispatcher.forward(request, response);
} catch (ServletException se) {
reportUnexpectedLoginError("ServletException", se);
} catch (IOException ie) {
reportUnexpectedLoginError("IOException", ie);
}
ctx.responseComplete();
}
private void reportUnexpectedLoginError(String errType, Exception e) {
FacesMessage msg =
new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unexpected error during login",
"Unexpected error during login (" + errType + "), please consult logs for detail");
FacesContext.getCurrentInstance().addMessage(null, msg);
e.printStackTrace();
}
To add them Just Double click on the ViewController project,Libraries and classpath ,Add Jar /Directory
The location of the first two libraries is
C:\Oracle\Middleware\Oracle_Home\wlserver\modules
The location of the last one"wls-api.jar" is
C:\Oracle\Middleware\Oracle_Home\wlserver\server\lib
First ,we will start implementing login method.Create a managed bean"loginBean"
private void authenticateUser(String un, String pw) {
byte[] password = pw.getBytes();
FacesContext fctx = FacesContext.getCurrentInstance();
HttpServletRequest request = null;
request = (HttpServletRequest) fctx.getExternalContext().getRequest();
try {
Subject subject = Authentication.login(new URLCallbackHandler(un, password));
weblogic.servlet.security.ServletAuthentication.runAs(subject, request);
String loginUrl = "/adfAuthentication?success_url=/faces/home";
HttpServletResponse response = (HttpServletResponse) fctx.getExternalContext().getResponse();
sendForward(request, response, loginUrl);
} catch (FailedLoginException fle) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,"Invalid user name or password.",null));
// context.addMessage(null,
// new FacesMessage(FacesMessage.SEVERITY_ERROR,
// JSFUtils.getStringFromResourceBundle("INVALID_LOGIN_MSG"), null));
} catch (LoginException le) {
reportUnexpectedLoginError("LoginException", le);
}
}
private void sendForward(HttpServletRequest request, HttpServletResponse response, String forwardUrl) {
FacesContext ctx = FacesContext.getCurrentInstance();
RequestDispatcher dispatcher = request.getRequestDispatcher(forwardUrl);
try {
dispatcher.forward(request, response);
} catch (ServletException se) {
reportUnexpectedLoginError("ServletException", se);
} catch (IOException ie) {
reportUnexpectedLoginError("IOException", ie);
}
ctx.responseComplete();
}
private void reportUnexpectedLoginError(String errType, Exception e) {
FacesMessage msg =
new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unexpected error during login",
"Unexpected error during login (" + errType + "), please consult logs for detail");
FacesContext.getCurrentInstance().addMessage(null, msg);
e.printStackTrace();
}
Of course you have to add two variables in your login bean for username and password
Comments