Java Spring password encoding fail - java

I don't know why my UserEntity.java fail to encode the password when I test it in debug mode. The password appear without MD5 encoded. Else is when I click on org.springframework.security.authentication it's say:
this element has no attached Javadoc and the Javadoc could not be found in the attached source
I have springframework-core in Maven dependencies which contain the both classes of Md5PasswordEncoder.java and PasswordEncoder.java.
If this two needs a bean definition how do I create it?
package com.example.j2eeapp.domain;
import java.io.Serializable;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.authentication.encoding.PasswordEncoder;
/**
* Entity to hold application user data - first name, last name, etc.
*/
public class UserEntity implements Serializable {
private static final long serialVersionUID = 9014169812363387062L;
private String firstName;
private String lastName;
private String userName;
private String password;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
PasswordEncoder crypto = new Md5PasswordEncoder();
this.password = crypto.encodePassword(password, null);
}
}

Related

POJO class with Constructor for the POST request body which has nested JSON

I want to create a POJO class with Constructor for the POST request body which has nested JSON, But I am not sure how to call JSONArray inside it?
PS: I do not want to set data using the setter method, I want to use Constructor for setting the data.
Here is the JSON:
{
"FirstName": "test",
"LastName": "account",
"PASSWORD": "Password123*",
"Email": [
{
"TYPE": "Primary",
"VALUE": "arpitay6#mail7.io"
}
]}
POJO I've created -
import java.util.List;
public class PostAccountCreateAPI {
private List <Email> email;
private String password;
private String firstname;
private String lastname;
public PostAccountCreateAPI(List<Email> email, String password, String firstname, String lastname) {
this.email = email;
this.password = password;
this.firstname = firstname;
this.lastname = lastname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<Email> getEmail() {
return email;
}
public void setEmail(List<Email> email) {
this.email = email;
}
}
package pojo;
public class Email {
private String type;
private String value;
public Email(String type, String value) {
this.type = type;
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
In the main method, I am calling POJO using -
PostAccountCreateAPI PostAccountCreateAPIPayLoad = new PostAccountCreateAPI("pri#mail7.io", "P#$$w0rd", "arpita", "garg");
But It is not working. Can anyone please suggest how to do this?
Ok....Here I am Giving a complete example:
The PostAccountCreateApi class:
import java.util.List;
public class PostAccountCreateAPI{
private String FirstName;
private String LastName;
private String PASSWORD;
private List<Email> Email;
public PostAccountCreateAPI(){}
public PostAccountCreateAPI(String FirstName, String LastName, String PASSWORD, List<Email> Email){
this.FirstName = FirstName;
this.LastName = LastName;
this.PASSWORD = PASSWORD;
this.Email = Email;
}
public void setFirstName(String FirstName){
this.FirstName = FirstName;
}
public String getFirstName(){
return this.FirstName;
}
public void setLastName(String LastName){
this.LastName = LastName;
}
public String getLastName(){
return this.LastName;
}
public void setPASSWORD(String PASSWORD){
this.PASSWORD = PASSWORD;
}
public String getPASSWORD(){
return this.PASSWORD;
}
public void setEmail(List<Email> Email){
this.Email = Email;
}
public List<Email> getEmail(){
return this.Email;
}
}
The Email Class:
public class Email {
String TYPE;
String VALUE;
public Email() {
}
public Email(String TYPE, String VALUE) {
this.TYPE = TYPE;
this.VALUE = VALUE;
}
public void setTYPE(String TYPE) {
this.TYPE = TYPE;
}
public String getTYPE() {
return this.TYPE;
}
public void setVALUE(String VALUE) {
this.VALUE = VALUE;
}
public String getVALUE() {
return this.VALUE;
}
}
The main class with a dummy main method:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Email> emailList = new ArrayList<>();
emailList.add(new Email("Primary", "pri#mail7.io"));
emailList.add(new Email("Primary", "amimulahsan7#gmail.com"));
//And list goes on......
PostAccountCreateAPI postAccountCreateAPI = new PostAccountCreateAPI("arpita", "garg",
"P#$$w0rd", emailList);
}
}
The first parameter to the constructor is List<Email>, but you are currently passing a String as the first argument to the constructor. Create a list of Email objects and then invoke the PostAccountCreateAPI constructor.

JPARepository searching queries returning null

I am having trouble understanding why my userRepository is returning null even when there is a record like it in my table. I tried doing it with my demo codes and it works but when I try doing it with user Authentication it does not work.
Security Services
#Path("/securityservice")
public class SecurityServices {
private UserRepository userRepo;
// http://localhost:8990/login/securityservice/security
#GET
#Path("security")
#Produces(MediaType.APPLICATION_JSON)
public Response getOrderById(#QueryParam("orderId") int orderID,
#HeaderParam("Authorization") String authString) throws JSONException {
JSONObject json = new JSONObject();
if (isUserAuthenticated(authString)) {
json.put("INFO", "Authorized User!");
return Response.status(200)
.entity(json.toString())
.type(MediaType.APPLICATION_JSON)
.build();
} else {
json.put("ERROR", "Unauthorized User!");
return Response.status(403)
.entity(json.toString())
.type(MediaType.APPLICATION_JSON)
.build();
}
}
private boolean isUserAuthenticated(String authString) {
//authString = Basic 3hfjdksiwoeriounf
String[] authParts = authString.split("\\s+");
//authParts[0] = Basic
//authParts[1] = 3hfjdksiwoeriounf
String authInfo = authParts[1];
byte[] bytes = Base64.getDecoder().decode(authInfo);
String decodedAuth = new String(bytes);
// decodedAuth = dj:1234
String[] credentials = decodedAuth.split(":");
//credentials[0]=dj
//credentials[1]=1234
System.out.println("HELLO"+credentials[0]);
System.out.println("HELLO"+credentials[1]);
User user = userRepo.findByUsername(credentials[0]); //this line returns null
if (user != null) {
return true;
} else {
return false;
}
}
User class (Getters and setters for the JPA Repo)
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Column(name="firstname")
private String firstName;
#Column(name="lastname")
private String lastName;
private String password;
private String username;
#Column(name="accesstype")
private String accessType;
public User() {
super();
}
public User(String firstName, String lastName, String password,
String username, String accessType) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.password = password;
this.username = username;
this.accessType = accessType;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAccessType() {
return accessType;
}
public void setAccessType(String accessType) {
this.accessType = accessType;
}
}

Getters and setters not working in android

package com.location.location;
/**
* Created by user on 14-12-2015.
*/
public class UserDetailsTable {
String firstName,lastName,userName,password;
public UserDetailsTable(String firstName, String lastName, String userName,
String password) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.userName = userName;
this.password = password;
}
public UserDetailsTable() {
super();
this.firstName = null;
this.lastName = null;
this.userName = null;
this.password = null;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public void setUserName(String userName) { this.userName = userName; }
public String getUserName() { return userName; }
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
I dont know where i am going wrong.... i get three values out of four arguments...the arguments which i get is firstname, lastname,and password,
for username the setter method setuserName says "Method Setter is never used"
I know this is foolish but I don't see any way out of this.... Please help...
First of all, constructors shouldn't have super(); since the class doesn't extend another one.
next your setter/getter are case sensitive . Try calling setUserName instead

objectify, javascript and google endpoint

I am trying to figure out how integration between google endpoint, objectify and javscripts works. I made a real simple class named 'User'. All I am trying is to fetch a record from a Data Store and return this object back and use this in javascript.
However, the object does not seem to flow back properly. I do not see any details around this object using Chrome's developer tools... Any ideas? FYI, the record exists in the data store as I can see it while using the development console. I also can see the information as I've logged this info to the console as well. Thanks.
** JAVA CLASS **
package com.Backend;
import com.googlecode.objectify.annotation.*;
#Entity
public class User {
String firstName;
String lastName;
#Id String email;
public User(){
super();
}
public User(String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
** GOOGLE ENDPOINT **
public class UserEndpoint {
private static final Logger LOG = Logger.getLogger(UserEndpoint.class.getName());
static {
ObjectifyService.register(User.class);
}
#ApiMethod(name = "getUser")
public User getUser() {
User u = ofy().load().type(User.class).id("johndoe#domain.com").now();
return u;
}
}
Finally found out what the issue is. It has to do with how the object was returned... It did not have any return value.

Logging In With JSP Hibernate

I have a problem with Hibernate to make login process. All codes are perfectly correct in terms of syntax. NetBeans tell me that my code have no problem. However, when I run the web, and I test the login process, it doesn't reacting and the address is stucked on the doLogin.
All classes have been mapped correctly.
This is my problem: when I try to retrieve data, my code is stucked on a line.
on doLogin servlet (I use the template provided by NetBeans and just filling in my code on the try. Here's in brief:
Connect con = new Connect(); //my code is stucked on this line.
//I've done testing where's the cause of the stuck, and this line is the cause.
List logger = con.getLogin(username, password);
and to make it clear:
Connect.java
public class Connect {
Session sesi;
public Connect() {
sesi = HibernateUtil.getSessionFactory().openSession();
}
public List getLogin(String username, String password){
return sesi.createQuery("from MsUser WHERE username = '"+username+"' and password = '"+password+"'").list();
}
}
and since that query is HQL, here is the MsUser class:
public class MsUser {
public MsUser() {
}
private int userID;
private String username;
private String firstname;
private String lastname;
private String email;
private String password;
private String gender;
private String address;
private String phone;
private String photo;
public MsUser(int userID, String username, String firstname, String lastname, String email, String password, String gender, String address, String phone, String photo) {
this.userID = userID;
this.username = username;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.password = password;
this.gender = gender;
this.address = address;
this.phone = phone;
this.photo = photo;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
please help. But I suspect on Connect's constructor as the main cause. Anybody can suggest or fix or tell me what causing me this.
appendix:
HibernateUtil.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Controller;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
/**
* Hibernate Utility class with a convenient method to get Session Factory
* object.
*
* #author Ginanjar
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
You can try following code in your hibernateUtil.java file:
SessionFactory factory = new Configuration().configure().buildSessionFactory();
session = factory.openSession();
String query = "select reg.username,reg.password from MsUser as reg where reg.username='" + username + "' and reg.password='" + password + "'";
Query DBquery = session.createQuery(query);
for (Iterator it = DBquery.iterate(); it.hasNext();) { it.next();
count++;
}
System.out.println("Total rows: " + count);
if (count == 1) {
return true;
} else {
return false;
}
}

Categories

Resources