Login using Database Table in Oracle ADF 12c
If you want create Security for your pages using Database tables, then this post will help you. Lets start, First we need to create USERS and ROLES table. Look at the screen shots for tables below.
Step I:
1.USERS Table:
User_Id is Primary Key |
2. ROLES Table:
Role_Id is Primary Key |
Step II:
Create New Fusion Web Application and Create EO, VO and AM based on Roles and Users tables as below.
Step III:
Open Application Module, enable Java class and write the below method in AMImpl class.
public boolean checkLogin(String userName, String password) {
System.out.println("userName: " + userName);
System.out.println("password: " + password);
boolean userFound = false;
String passwordEncoded = null;
if (!userName.isEmpty() && !password.isEmpty()) {
try {
passwordEncoded = this.encrypt(password, 1);
System.out.println("passwordEncoded:: " + passwordEncoded);
} catch (IOException e) {
ADFContext.getCurrent()
.getSessionScope()
.put("USERID", null);
ADFContext.getCurrent()
.getSessionScope()
.put("USERNAME", null);
throw new JboException("Issue in encoding");
}
ViewObject ADFUsersVO = this.getADFUsersVO().getViewObject();
ViewCriteria createVC = ADFUsersVO.createViewCriteria();
ViewCriteriaRow createVCR = createVC.createViewCriteriaRow();
createVCR.setAttribute("UserName", userName);
createVCR.setAttribute("UserPassword", passwordEncoded);
createVCR.setAttribute("Active", "Y");
createVC.insertRow(createVCR);
ADFUsersVO.applyViewCriteria(createVC);
ADFUsersVO.executeQuery();
if (ADFUsersVO.getRowCount() > 0) { // user found ok
userFound = true;
Number userId = (Number) ADFUsersVO.next().getAttribute("UserId");
System.out.println("UserId: " + userId);
ADFContext.getCurrent()
.getSessionScope()
.put("USERID", userId);
ADFContext.getCurrent()
.getSessionScope()
.put("USERNAME", userName);
} else { //user not ok
ADFContext.getCurrent()
.getSessionScope()
.put("USERID", null);
ADFContext.getCurrent()
.getSessionScope()
.put("USERNAME", null);
throw new JboException("Invalid Username or password");
}
} else {
ADFContext.getCurrent()
.getSessionScope()
.put("USERID", null);
ADFContext.getCurrent()
.getSessionScope()
.put("USERNAME", null);
throw new JboException("Username or Password are should be entered!");
}
return userFound;
}
public String encrypt(String value, int type) throws IOException {
//type 1 = encryption, 2 for decryption
String result = null;
if (type == 1) {
result = new sun.misc.BASE64Encoder().encode(value.getBytes());
} else {
byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(value);
result = new String(decode);
}
return result;
}
Expose the method into Client Interface.
Now we will move to ViewController project.
1. Create Login.jsf page and two input text fields for Username and Password. Add one button for Login as below.
Create Bean class using "Session Scope" as mentioned below.
Add the source code in Login.jsf page as given below.
Add the code for Login Button:
public String checkLogin() {
// Add event code here...
System.out.println("We are Entering into checkLogin() method::");
System.out.println("Username:: " + this.getUserName().getValue());
System.out.println("Password:: " + this.getPassword().getValue());
BindingContainer bindings = this.getBindings();
OperationBinding ob = bindings.getOperationBinding("checkLogin");
Map map = ob.getParamsMap();
map.put("userName", userName.getValue());
map.put("password", password.getValue());
Boolean userFound = (Boolean) ob.execute();
System.out.println("userFound:: "+userFound);
if(ob.getErrors().isEmpty()){
if(userFound){
System.out.println("User Okay");
}else{
System.out.println("User not Okay");
}
}
System.out.println("We are Leaving from checkLogin() method::");
return "MyHome";
}
Step V:
Deign the unbounded taskflow as below screen shot.
Create Home.jsf.
Home.jsf page source code:
<af:panelGroupLayout id="pgl1" layout="vertical">
<af:outputText value="Welcome to Home Page" id="ot1" inlineStyle="color:Blue; font-size:xx-large;"/>
<af:spacer id="s1"/>
<af:button text="Logout" id="b1" action="#{LoginBean.checkLogout}"/>
<af:spacer id="s2"/>
<af:outputText value="User Id: #{sessionScope.get("USERID") }" id="ot2"/>
<af:outputText value="User Name: #{sessionScope.get("USERNAME")}" id="ot3"/>
<af:spacer id="s3"/>
</af:panelGroupLayout>
Bean code for Logout button:
public String checkLogout() {
this.getUserName().setValue(null);
this.getPassword().setValue(null);
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "logout";
}
Now we can run the Login.jsf page.
This comment has been removed by the author.
ReplyDelete