Custom login with java Class - ADF 12c

The common problems with custom login with Java class was related with Jar files that will need to add at library classpath.
In this post I'll explain which jar we need to add, the custom java class to login and share the project.

  • First able  who - first enable who and which jars we need to add:

in UI project properties, go to "Libraries and ClassPath", add "Weblogic 12.1 Thin Client" and "wls-api.jar", the first one you can find in libraries, the other one you can find in your oracle middleware path or here: https://github.com/jtiagomp/ADF_POCs/tree/master/ADFLoginPageandAltaUI/libs



  • Create a Classe java "LoginBean" with this code:
  • import java.io.IOException;
    
    import java.io.Serializable;
    
    import java.util.logging.Logger;
    
    import javax.faces.application.FacesMessage;
    import javax.faces.context.ExternalContext;
    import javax.faces.context.FacesContext;
    import javax.faces.event.ActionEvent;
    
    import javax.security.auth.Subject;
    import javax.security.auth.callback.CallbackHandler;
    
    import javax.security.auth.login.LoginException;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.http.HttpServletRequest;
    
    import javax.servlet.http.HttpServletResponse;
    
    import view.adftools.ADFUtils;
    
    import weblogic.servlet.security.ServletAuthentication;
    
    import weblogic.security.SimpleCallbackHandler;
    import weblogic.security.services.Authentication;
    
    public class LoginBean implements Serializable{
        @SuppressWarnings("compatibility:2992071069688435404")
        private static final long serialVersionUID = 1L;
    
        private static final Logger logger = Logger.getLogger(LoginBean.class.getName());
        private String userName;
        private String password;
    
        public LoginBean() {
            super();
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getUserName() {
            return userName;
        }
    
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getPassword() {
            return password;
        }
    
    
        public void doLogin(ActionEvent event) {
            FacesContext fctx = FacesContext.getCurrentInstance();
            ExternalContext ectx = fctx.getExternalContext();
            HttpServletRequest request = (HttpServletRequest) ectx.getRequest();
            @SuppressWarnings("deprecation")
            CallbackHandler handler = new SimpleCallbackHandler(this.userName, this.password.getBytes());
            try {
                Subject sub = Authentication.login(handler);
                ServletAuthentication.runAs(sub, request);
                ServletAuthentication.generateNewSessionID(request);
                String successUrl = "/adfAuthentication?success_url=/faces/welcome.jsf";
                HttpServletResponse response = (HttpServletResponse) fctx.getExternalContext().getResponse();
                RequestDispatcher dispatcher = request.getRequestDispatcher(successUrl);
                dispatcher.forward(request, response);
                fctx.responseComplete();
            } catch (Exception e) {
                ADFUtils.showFacesMessage(FacesMessage.SEVERITY_ERROR, "Login data error. Please confirm your login and password!");
                logger.severe("Login Exception with username:  "+ getUserName() );
            }
        }
    
    
        public String onLogout() {
            FacesContext fctx = FacesContext.getCurrentInstance();
            ExternalContext ectx = fctx.getExternalContext();
            String url = ectx.getRequestContextPath() + "/adfAuthentication?logout=true&end_url=/faces/login.jsf";
            try {
                ectx.redirect(url);
            } catch (IOException e) {
                e.printStackTrace();
            }
            fctx.responseComplete();
            return null;
    
        }
    }
    



thats it!! 

Cheers.


Related Posts:

No responses yet for "Custom login with java Class - ADF 12c"

Post a Comment