11package com .sspath .multifilterchain .config ;
22
3+ import com .fasterxml .jackson .core .JsonParseException ;
4+ import com .fasterxml .jackson .databind .JsonMappingException ;
5+ import com .fasterxml .jackson .databind .ObjectMapper ;
6+ import org .springframework .http .MediaType ;
37import org .springframework .security .authentication .AuthenticationServiceException ;
8+ import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
49import org .springframework .security .core .Authentication ;
510import org .springframework .security .core .AuthenticationException ;
611import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
712import org .springframework .util .StringUtils ;
813
914import javax .servlet .http .HttpServletRequest ;
1015import javax .servlet .http .HttpServletResponse ;
16+ import java .io .IOException ;
17+ import java .util .HashMap ;
18+ import java .util .Map ;
1119
1220/**
1321 * @FileName: LoginFilter.java
@@ -21,8 +29,34 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
2129 if (!request .getMethod ().equals ("POST" )) {
2230 throw new AuthenticationServiceException ("Authentication method not supported: " + request .getMethod ());
2331 }
24- String kaptcha = request . getParameter ( "kaptcha" );
32+
2533 String sessionKaptcha = (String ) request .getSession ().getAttribute ("kaptcha" );
34+
35+ if (request .getContentType ().equalsIgnoreCase (MediaType .APPLICATION_JSON_VALUE ) ||
36+ request .getContentType ().equalsIgnoreCase (MediaType .APPLICATION_JSON_UTF8_VALUE )) {
37+ Map <String , String > userInfo = new HashMap <>();
38+ try {
39+ userInfo = new ObjectMapper ().readValue (request .getInputStream (), Map .class );
40+ String username = userInfo .get (getUsernameParameter ());
41+ String password = userInfo .get (getPasswordParameter ());
42+ String kaptcha = userInfo .get ("kaptcha" );
43+ if (StringUtils .hasText (kaptcha ) && StringUtils .hasText (sessionKaptcha ) && kaptcha .equalsIgnoreCase (sessionKaptcha )) {
44+ UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken (
45+ username , password );
46+ setDetails (request , authRequest );
47+ return this .getAuthenticationManager ().authenticate (authRequest );
48+ }
49+ } catch (JsonMappingException e ) {
50+ e .printStackTrace ();
51+ } catch (JsonParseException e ) {
52+ e .printStackTrace ();
53+ } catch (IOException e ) {
54+ e .printStackTrace ();
55+ }
56+ }
57+
58+ String kaptcha = request .getParameter ("kaptcha" );
59+
2660 if (StringUtils .hasText (kaptcha ) && StringUtils .hasText (sessionKaptcha ) && kaptcha .equalsIgnoreCase (sessionKaptcha )) {
2761 return super .attemptAuthentication (request , response );
2862 }
0 commit comments