Announcement Announcement Module
Collapse
No announcement yet.
Spring security with Ajax logged user details Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring security with Ajax logged user details

    Hi

    I have already implemented user authentication via Ajax using AuthenticationEntryPoint and SimpleUrlAuthenticationSuccessHandler.

    Now I need to get logged user name in to my script variable.

    Can anyone please help me on this?

    Thanks

    MyAuthenticationSuccessHandler

    Code:
    public class MyAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
    
        private Log log = LogFactory.getLog(MyAuthenticationSuccessHandler.class);
    
        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                Authentication authentication) throws IOException, ServletException {
    
            log.info("point-2-->"+authentication.getName()); //this prints what I need.
    
            // This is actually not an error, but an OK message. It is sent to avoid redirects.
            response.sendError(HttpServletResponse.SC_OK);
    
        }
    }
    My JavaScript function

    Code:
    $("#login").on('click', function(e) {
            e.preventDefault();
            $.ajax({url: getHost() + "/j_spring_security_check",
                type: "POST",
                beforeSend: function(xhr) {
                    xhr.withCredentials = true;
                },
                data: $("#loginForm").serialize(),
                success: function(response, options) {
                        // We get a success from the server
                        //I need to get user name here
                },
                error: function(result, options) {
                        // We get a failure from the server...
                    $(".error").remove();
                    $('#j_username').before('<div class="error">Login failed, please try again.</div>');
            }
    
    
            });
        });
    I have already attached all relevant files for different question. Please access the link below for check them.

    Spring Security; custom-filter and user-service-ref not working together

  • #2
    I found a solution.

    Simply I made another Ajax call and there I captured logged user.

    My JavaScript function

    Code:
    success: function(response, options) {
                        // We get a success from the server
                        //I need to get user name here
    
    	$.post('/api/loggeduser', function(data) {
          	   $('#loggeduser').html(data);
        	}); 
    },
    loggeduser Controller

    Code:
    @RequestMapping(value = "/api/loggeduser", method = RequestMethod.POST)
        	   public String printWelcome(ModelMap model, Principal principal ) {
        	
        		try {
        			
        			String name = null;
        			
        			if (principal!=null) {
    				 			
    			   name = principal.getName();
    			
    			}
        		
        		
        	    		model.addAttribute("username", name);
        	    
        		
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	 }

    Comment

    Working...
    X