Skip to content

Commit 5acbf4a

Browse files
committed
支持JSON格式登录
1 parent 59660b5 commit 5acbf4a

File tree

1 file changed

+35
-1
lines changed
  • multi-filter-chain/src/main/java/com/sspath/multifilterchain/config

1 file changed

+35
-1
lines changed

multi-filter-chain/src/main/java/com/sspath/multifilterchain/config/LoginFilter.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package 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;
37
import org.springframework.security.authentication.AuthenticationServiceException;
8+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
49
import org.springframework.security.core.Authentication;
510
import org.springframework.security.core.AuthenticationException;
611
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
712
import org.springframework.util.StringUtils;
813

914
import javax.servlet.http.HttpServletRequest;
1015
import 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

Comments
 (0)