75 lines
3.2 KiB
Java
75 lines
3.2 KiB
Java
package com.redstar.auth.login;
|
|
|
|
|
|
import com.redstar.auth.Feign.AuthServiceClient;
|
|
import com.redstar.auth.vo.AuthSession;
|
|
import com.redstar.auth.vo.AuthUser;
|
|
import com.redstar.common.exception.BusinessException;
|
|
import com.redstar.common.service.RedisService;
|
|
import com.redstar.common.utils.ResponseTool;
|
|
import com.redstar.common.utils.ResponseUtils;
|
|
import com.redstar.common.utils.UserIdTool;
|
|
import com.redstar.common.vo.result.JWT;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
|
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
* <p> 认证成功处理 </p>
|
|
*
|
|
* @description :
|
|
*/
|
|
@Component
|
|
public class AdminAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
|
@Autowired
|
|
RedisService redisService;
|
|
@Resource
|
|
AuthServiceClient client;
|
|
@Value("${auth.serverUrl}")
|
|
private String serverUrl;
|
|
@Override
|
|
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
|
|
try {
|
|
AuthSession authSession = (AuthSession) redisService.get(authentication.getPrincipal());
|
|
AuthUser authUser = new AuthUser();
|
|
BeanUtils.copyProperties(authSession, authUser);
|
|
if (UserIdTool.isNumber((String) authentication.getPrincipal())) {
|
|
authUser.setMobile(((String) authentication.getPrincipal()).substring(0, 11));
|
|
}
|
|
List<String> authorities = (List<String>) redisService.get(authentication.getPrincipal() + "AUTH");
|
|
authUser.setAuthoritiesList(authorities);
|
|
JWT jwt = client.getToken("Basic dXNlci1zZXJ2aWNlOjEyMzQ1Ng==", "password",
|
|
(String)authentication.getPrincipal(), (String)authentication.getCredentials());
|
|
|
|
if (jwt == null) {
|
|
throw new BusinessException("error internal");
|
|
}
|
|
DefaultSavedRequest savedRequest = (DefaultSavedRequest)httpServletRequest.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST");
|
|
authUser.setJwt(jwt);
|
|
if(savedRequest!=null &&savedRequest.getQueryString().indexOf("response_type=code")!=-1){
|
|
//response.setStatus(302);
|
|
//response.setHeader("Location", serverUrl+"?"+savedRequest.getQueryString());//设置新请求的URL
|
|
authUser.setUrl(serverUrl+"?"+savedRequest.getQueryString());
|
|
ResponseUtils.out(response, ResponseTool.success(authUser));
|
|
}else {
|
|
ResponseUtils.out(response, ResponseTool.success(authUser));
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|