2

My JAX-RS resource for authentication looks like this (by the way I want to improve it for security matters because it's not safe to call the username and password by a GET Method):

@Path("/UserService")

public class UserServiceRS {

UserService user ;
AuthenticationManager authManager;
public UserServiceRS(){

user=(UserService)SpringApplicationContext.getBean("userDetailsService");
authManager(AuthenticationManager)SpringApplicationContext
.getBean("authenticationManager");

 }

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/authentification/{username}/{password}")
public String login(@PathParam( value="username" ) String username,
        @PathParam( value="password" ) String password )
{
     Logger LOG = LoggerFactory.getLogger(LoginBean.class);
    LOG.info("Starting to login");
    try{
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(username,password);
    Authentication authenticate = authManager.authenticate(usernamePasswordAuthenticationToken);
    SecurityContextHolder.getContext().setAuthentication(authenticate);

     return "1";
    }catch (final Exception e){
        LOG.error("Error log in" + e);

    }

    return "0";
}

And in my jQuery Mobile client the method I call in the "connect" button looks like this:

function login(){
 var uri="myuri";
 var login=$("#login").val();
 var password=$("#pass").val();
 uri=uri+"/"+login+"/"+password;
 $.getJSON(uri,function(data){
     if(JSON.stringify(data)==1){

$.mobile.changePage("#page-signup-succeeded",{transition:"slide" });
      }
     else {
        $.mobile.changePage("#page-signup-failed");

     }
 }); 
 };

I would like to manage the session in my jQuery Mobile client and I don't know how to get the session from the authenticated user. How can I achieve this?

  • @BalusC it's JSF not Java SE . – Mrabtei Ayoub Mar 18 '16 at 14:31
  • I'm not seeing any single line of JSF here. First block of code is a JAX-RS resource. Second block of code is JavaScript function using jQuery. The `FacesContext.getCurrentInstance()` absolutely doesn't make sense, it would only return `null` there. – BalusC Mar 18 '16 at 14:33
  • Yes you're right but in my backend I have a layer for webservices and a layer for JSF Managed Beans . – Mrabtei Ayoub Mar 18 '16 at 14:36
  • Yes the line you're talking about was for testing I have removed it. – Mrabtei Ayoub Mar 18 '16 at 14:38
  • I think you've after all same problem as answered here: http://stackoverflow.com/a/36084386 – BalusC Mar 21 '16 at 10:13
  • @BalusC I've read your response and you cleared my confusion but how can I get some of the information about the authenticated user ? – Mrabtei Ayoub Mar 21 '16 at 10:50

0 Answers0