<div dir="ltr">Hi, I have one question. I have custom authentication class which authenticate users through mobile phone. It sends request to web service and then waits in while cycle for user to perform some action on his mobile phone. Problem is, that I want to implement cancel button so that users can cancel current authentication session. But I don't have idea how to do it. My current code is as follows:<div>
<br></div><div><div>String responseString = sendNfcRequest(username, serviceName, messageId); //sends request</div><div> ServerResponse wsResponse = parseServerResponse(responseString); //recieve resopnse</div>
<div><br></div><div> //initiate wait for user response</div><div> int userResult = 0;</div><div> while (userResult == 0) {</div><div> String userResponseString = getNfcUserResponse(wsResponse);</div>
<div> userResult = getNfcUserResult(userResponseString);</div><div><br></div><div> if (userResult == 0) {</div><div> Thread.sleep(2000);</div><div> }</div><div>
}</div><div><br></div><div> if (userResult == 1) { //successfull authentication on mobile phone</div><div> Subject subj = new Subject();</div><div><br></div><div> Principal principal = new UsernamePrincipal(username);</div>
<div> subj.getPrincipals().add(principal);</div><div><br></div><div> request.setAttribute(LoginHandler.PRINCIPAL_KEY, principal);</div><div> request.setAttribute(LoginHandler.PRINCIPAL_NAME_KEY, username);</div>
<div> request.setAttribute(LoginHandler.SUBJECT_KEY, subj);</div><div><br></div><div> Logger.getLogger(ExternalAuthServlet.class.getName()).log(Level.INFO, "Nfc authentication success", this);</div>
<div><br></div><div> //add cookies</div><div> Cookie userNameCookie = new Cookie("username", username);</div><div> response.addCookie(userNameCookie);</div><div> Cookie authMethodCookie = new Cookie("authMethod", "2");</div>
<div> response.addCookie(authMethodCookie);</div><div><br></div><div> AuthenticationEngine.returnToAuthenticationEngine(request, response);</div><div> }</div></div><div><br></div><div>
On my page I have form with one button and I change its label to "Authenticate" or "Cancel". When "Authenticate" is pressed, it runs code above and changes label to "Cancel". When "Cancel" is pressed it does nothing, only changes back to "Authenticate". Problem is, that when I hit "Authenticate" again I have now two sessions running and user can response to any of them. So my question is how to throw away previous session and hold only the curent (last) one?</div>
</div>