How to authenticate and redirect a user to his 

2019-08-08 01:59发布

问题:

How to authenticate and redirect a user to his own page i.e to www.mysite.com/"user's email".

I am using the following algo which is not working...

userDB in User class:

Map<String,String> userdata=new HashMap<String,String>();

First my login process form :

@Path("/login")
    @POST
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void login(
            @FormParam("email") String emailc,
            @FormParam("password") String pass,
            @Context HttpServletResponse servletResponse
    ) throws IOException,RuntimeException {
        User u1=new User();
        pass=u1.getPassword();
        emailc=u1.getEmailaddrs();

              boolean checked=false;
        boolean exists;
        exists=u1.userdata.containsKey(emailc);
        if(exists){
            String mypass =u1.userdata.get(emailc);
            if(mypass==pass){
                checked=true;
            }else{
                checked=false;
            }
        }else{
            checked=false;
        }
        if(!checked){
            //User Doesn't exists
            servletResponse.sendRedirect("http://localhost:8080/MySite/pages/Create_Profile.html");
        }else{
            servletResponse.sendRedirect("http://localhost:8080/MySite/{email}");  <<<< How to redirect using @FormParam("email")   
        }
    }  

createprofile

@POST
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void newUser(
            @FormParam("email") String email,
            @FormParam("password") String password,
            @Context HttpServletResponse servletResponse
    ) throws IOException {
            User u = new User(email,password);
            User.userdata.put(email,password);
    }

回答1:

Your usage of userdata [Map] looks wrong to me. Is it a part of user class, is it non static or static ? If it is non static then every time you will do new User() .. that map will be initialized and it will have no data in it. Hence u1.userdata.containsKey(emailc); will be always false.

If you are using a hashmap as a temporary database for dev purposes then, make it static rather keep it in a different class like UserStore or some DB access layer. Exmaple below:

public class UserDAO(){

private static Map<String,User> userdata = new HashMap<String,User>();
public boolean hasUser(String email){
 return userdata.contains(email);
}

public User saveUser(String email, String password ...){
//make user object save it in map and return the same
}

// more methods for delete and edit etc.

}

And use this in your REST layer classes like this

exists = userDao.hasUser(email);

Advantages :

  1. Your problem will be solved.
  2. Later on when you move to actual db implementation you will just have to change your UserDao code and rest application code will be just fine. -- Loose coupling :)

Also regarding forward using email

servletResponse.sendRedirect("http://localhost:8080/MySite/{email}");  <<<< How to redirect using @FormParam("email")

add the email parameter there in the url only, if thats what you want:

servletResponse.sendRedirect("http://localhost:8080/MySite/"+emailc);

UPDATE :

See the fundamental thing is that you get request parameters [email , password]. You check it whether it is present in map or not. Now what you are doing wrong here is you create a new user like this User u = new User(); and then get email and password from it emailc = u.getEmail();. This emailc will always be null and your userdata map will always return false for that. You have two choices :

  1. Either set email and password in user object and then get the data from user object.
  2. Use the email and password obtained from request parameters for your logic. Do not alter them

One good practice to follow while programming is that at all times think of your method parameters as final parameters.

UPDATE 2 :

if(mypass==pass){
                checked=true;
            }else{
                checked=false;
            }

Change == to equals method. String matching should be done by equals or equalsIgnoreCase method not ==.



回答2:

You always create a new User without any parameters: User u1=new User();. All these User instances will have the same property values and probably exists is always false.