|
|
@@ -1,37 +1,39 @@
|
|
|
-package cn.reghao.autodop.dmaster.auth.impl.formlogin;
|
|
|
+package cn.reghao.autodop.dmaster.auth.jwt;
|
|
|
|
|
|
+import cn.reghao.autodop.common.utils.JsonUtil;
|
|
|
import com.google.gson.JsonObject;
|
|
|
import com.google.gson.JsonParser;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
|
|
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|
|
|
|
|
+import javax.servlet.FilterChain;
|
|
|
import javax.servlet.ServletException;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.PrintWriter;
|
|
|
|
|
|
/**
|
|
|
- * 替换 UsernamePasswordAuthenticationFilter
|
|
|
+ * 替换 UsernamePasswordAuthenticationFilter,用于认证用户。匹配指定 URL 才会进行处理
|
|
|
*
|
|
|
* @author reghao
|
|
|
* @date 2019-11-18 08:55:59
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
public class UsernamePasswordAuthFilter extends AbstractAuthenticationProcessingFilter {
|
|
|
- private String httpMethod;
|
|
|
-
|
|
|
public UsernamePasswordAuthFilter(String authUrl, String httpMethod) {
|
|
|
super(new AntPathRequestMatcher(authUrl, httpMethod));
|
|
|
- this.httpMethod = httpMethod;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
|
|
|
throws AuthenticationException, IOException, ServletException {
|
|
|
-
|
|
|
// form-data 中的 username 和 password 参数
|
|
|
String username = request.getParameter("user");
|
|
|
String password = request.getParameter("password");
|
|
|
@@ -53,31 +55,44 @@ public class UsernamePasswordAuthFilter extends AbstractAuthenticationProcessing
|
|
|
* @return
|
|
|
* @date 2019-11-18 上午11:15
|
|
|
*/
|
|
|
- /*@Override
|
|
|
+ @Override
|
|
|
protected void successfulAuthentication(HttpServletRequest request,
|
|
|
HttpServletResponse response,
|
|
|
FilterChain chain,
|
|
|
Authentication auth) throws IOException, ServletException {
|
|
|
+ // TODO 将 username:token 存储在缓存中,用户注销时,在缓存中将 token 置为不可用
|
|
|
+ String accessToken = JwtToken.newToken(auth.getName());
|
|
|
+ ResultData resultData = new ResultData();
|
|
|
+ resultData.setMsg("登录成功");
|
|
|
+ resultData.setSuccess(true);
|
|
|
+ resultData.setToken(accessToken);
|
|
|
+ resultData.setUser(auth.getName());
|
|
|
+ Result result = new Result();
|
|
|
+ result.setCode(0);
|
|
|
+ result.setData(resultData);
|
|
|
|
|
|
- SavedRequest savedRequest = (SavedRequest) request.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST");
|
|
|
- if (savedRequest != null) {
|
|
|
- String redirectUrl = savedRequest.getRedirectUrl();
|
|
|
- // 跳转到登录前
|
|
|
- response.sendRedirect(redirectUrl);
|
|
|
- } else {
|
|
|
- JwtTokenProvider.setAuthentication(response, auth.getName());
|
|
|
- }
|
|
|
- }*/
|
|
|
+ response.setHeader("Authorization", "Bearer " + accessToken);
|
|
|
+ response.setContentType("text/html; charset=utf-8");
|
|
|
+
|
|
|
+ // 由于没有托管在 Spring MVC 中,因此需要单独处理跨域
|
|
|
+ response.addHeader("Access-Control-Allow-Origin", "*");
|
|
|
+ response.addHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,PUT,DELETE");
|
|
|
+ response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization,Content-Type");
|
|
|
+
|
|
|
+ PrintWriter pt = response.getWriter();
|
|
|
+ log.info("认证成功并返回");
|
|
|
+ pt.println(JsonUtil.objectToJson(result));
|
|
|
+ }
|
|
|
|
|
|
- /*@Override
|
|
|
+ @Override
|
|
|
protected void unsuccessfulAuthentication(HttpServletRequest request,
|
|
|
HttpServletResponse response,
|
|
|
AuthenticationException failed) throws IOException, ServletException {
|
|
|
|
|
|
response.setContentType("application/json");
|
|
|
- response.setStatus(HttpServletResponse.SC_OK);
|
|
|
- response.getOutputStream().println("Internal Server Error");
|
|
|
- }*/
|
|
|
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
+ response.getOutputStream().println("username or password invalid");
|
|
|
+ }
|
|
|
|
|
|
private JsonObject getBody(HttpServletRequest request) throws IOException {
|
|
|
BufferedReader br = request.getReader();
|
|
|
@@ -89,4 +104,18 @@ public class UsernamePasswordAuthFilter extends AbstractAuthenticationProcessing
|
|
|
|
|
|
return new JsonParser().parse(sb.toString()).getAsJsonObject();
|
|
|
}
|
|
|
+
|
|
|
+ @Data
|
|
|
+ class Result {
|
|
|
+ private int code;
|
|
|
+ private ResultData data;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Data
|
|
|
+ class ResultData {
|
|
|
+ private String msg;
|
|
|
+ private boolean success;
|
|
|
+ private String token;
|
|
|
+ private String user;
|
|
|
+ }
|
|
|
}
|