目标是授予传统浏览器形式的POST身份验证和基于JSON的身份验证。同样在JSON身份验证中,我想避免重定向到loginSuccesful.htm
在上下文中:
<security:http use-expressions="true" auto-config="false" entry-point-ref="http403EntryPoint"> <security:intercept-url pattern="/logs 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类:
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"))) { response.getWriter().print("{"responseCode":"SUCCESS"}"); response.getWriter().flush(); } else { super.onAuthenticationSuccess(request, response, auth); } }}


