Spring Security and JSON Authentication

前端 未结 8 1697
我在风中等你
我在风中等你 2020-11-28 05:11

I\'ve an application in spring/spring-mvc that totally uses JSON communications. Now I need to authenticate my application with spring security 3 (that uses LdapAuthenticati

8条回答
  •  无人及你
    2020-11-28 05:48

    According with Kevin suggestions,
    and after reading this posts: 1, 2, documentation 3, and thanks to this blog post,
    I wrote my own FORM_LOGIN_FILTER to directly manage JSON before authentication.
    I paste my code for the community.

    The goal is to grant both the classical browser form POST authentication with JSON based authentication. Also in JSON authentication I want to avoid the redirect to loginSuccesful.htm

    In context:

          
        
        
    
        
        
        
            
        
        
    
    
    
        
        
        
        
        
        
    
    
    
        
        
    
    
    
        
    
    
    
    

    CustomUsernamePasswordAuthenticationFilter class:

    public class CustomUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter{
        private String jsonUsername;
        private String jsonPassword;
    
        @Override
        protected String obtainPassword(HttpServletRequest request) {
            String password = null; 
    
            if ("application/json".equals(request.getHeader("Content-Type"))) {
                password = this.jsonPassword;
            }else{
                password = super.obtainPassword(request);
            }
    
            return password;
        }
    
        @Override
        protected String obtainUsername(HttpServletRequest request){
            String username = null;
    
            if ("application/json".equals(request.getHeader("Content-Type"))) {
                username = this.jsonUsername;
            }else{
                username = super.obtainUsername(request);
            }
    
            return username;
        }
    
        @Override
        public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response){
            if ("application/json".equals(request.getHeader("Content-Type"))) {
                try {
                    /*
                     * HttpServletRequest can be read only once
                     */
                    StringBuffer sb = new StringBuffer();
                    String line = null;
    
                    BufferedReader reader = request.getReader();
                    while ((line = reader.readLine()) != null){
                        sb.append(line);
                    }
    
                    //json transformation
                    ObjectMapper mapper = new ObjectMapper();
                    LoginRequest loginRequest = mapper.readValue(sb.toString(), LoginRequest.class);
    
                    this.jsonUsername = loginRequest.getUsername();
                    this.jsonPassword = loginRequest.getPassword();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            return super.attemptAuthentication(request, response);
        }
    }
    

    CustomAuthenticationSuccessHandler class:

    public class CustomAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
    
        public void onAuthenticationSuccess(
                HttpServletRequest request,
                HttpServletResponse response,
                Authentication auth
        )throws IOException, ServletException {
    
            if ("application/json".equals(request.getHeader("Content-Type"))) {
                /*
                 * USED if you want to AVOID redirect to LoginSuccessful.htm in JSON authentication
                 */         
                response.getWriter().print("{\"responseCode\":\"SUCCESS\"}");
                response.getWriter().flush();
            } else {
                super.onAuthenticationSuccess(request, response, auth);
            }
        }
    }
    

提交回复
热议问题