Compare commits
2 Commits
master
...
4_14versio
| Author | SHA1 | Date |
|---|---|---|
|
|
221952de21 | 3 years ago |
|
|
0bc36f8388 | 3 years ago |
10 changed files with 188 additions and 18 deletions
@ -0,0 +1,22 @@
@@ -0,0 +1,22 @@
|
||||
/** |
||||
* @author JOJO |
||||
* @class TokenMap |
||||
* @date 2023/4/12 |
||||
* @apiNote |
||||
*/ |
||||
|
||||
package co.depsystem.app.Demos.web.Author; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Objects; |
||||
|
||||
public class TokenMap { |
||||
public HashMap<String, Objects> getTokenMap(String ...av){ |
||||
return new HashMap<>(){ |
||||
private static final long serverid = 1L; |
||||
{ |
||||
|
||||
} |
||||
}; |
||||
} |
||||
} |
||||
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
/** |
||||
* @author JOJO |
||||
* @class LoginFilterImpl |
||||
* @date 2023/4/12 |
||||
* @apiNote |
||||
*/ |
||||
|
||||
package co.depsystem.app.service.Author.Impl; |
||||
|
||||
import com.alibaba.fastjson2.JSONObject; |
||||
import jakarta.servlet.http.HttpServletRequest; |
||||
import jakarta.servlet.http.HttpServletResponse; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.security.authentication.AuthenticationServiceException; |
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
||||
import org.springframework.security.core.Authentication; |
||||
import org.springframework.security.core.AuthenticationException; |
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
public class LoginFilterImpl extends UsernamePasswordAuthenticationFilter { |
||||
/** |
||||
* @param request resq |
||||
* @param response resp |
||||
* @return Authentication |
||||
* @throws AuthenticationException error |
||||
*/ |
||||
@Override |
||||
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { |
||||
if (!request.getMethod().equals("POST")){ |
||||
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod()); |
||||
} |
||||
// 判断是否为JSON
|
||||
if(request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) |
||||
{ |
||||
Map loginData = new HashMap<>(); |
||||
try |
||||
{ |
||||
//将Json数据转换为Map
|
||||
loginData = JSONObject.parseObject(request.getInputStream().toString(), Map.class); |
||||
}catch (IOException e) |
||||
{ |
||||
throw new RuntimeException(e); |
||||
} |
||||
String username = loginData.get(getUsernameParameter()).toString(); |
||||
String password = loginData.get(getPasswordParameter()).toString(); |
||||
if (username == null) |
||||
{ |
||||
throw new AuthenticationServiceException("用户名不能为空"); |
||||
} |
||||
if (password == null) |
||||
{ |
||||
throw new AuthenticationServiceException("密码不能为空"); |
||||
} |
||||
UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username, password); |
||||
setDetails(request, authRequest); |
||||
return this.getAuthenticationManager().authenticate(authRequest); |
||||
}else |
||||
{ |
||||
request.getContentType(); |
||||
//普通表单提交数据处理
|
||||
String username = this.obtainUsername(request); |
||||
String password = this.obtainPassword(request); |
||||
if (username == null) |
||||
{ |
||||
username = ""; |
||||
} |
||||
if (password == null) |
||||
{ |
||||
password = ""; |
||||
} |
||||
username = username.trim(); |
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username,password); |
||||
this.setDetails(request,authRequest); |
||||
return this.getAuthenticationManager().authenticate(authRequest); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue