|
|
package com.tianbo.warehouse.security.handel.kakologin;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.tianbo.warehouse.bean.AuthSuccessResponse;
|
|
|
import com.tianbo.warehouse.model.KakoUser;
|
|
|
import com.tianbo.warehouse.model.USERS;
|
|
|
import com.tianbo.warehouse.security.config.SecurityProperties;
|
|
|
import com.tianbo.warehouse.security.filter.JwtTokenUtil;
|
|
|
import com.tianbo.warehouse.security.model.LoginType;
|
|
|
import com.tianbo.warehouse.service.PermissionService;
|
|
|
import com.tianbo.warehouse.util.RedisUtils;
|
|
|
import org.apache.commons.logging.Log;
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.security.authentication.DisabledException;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
|
|
|
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
|
|
|
import org.springframework.security.web.savedrequest.RequestCache;
|
|
|
import org.springframework.security.web.savedrequest.SavedRequest;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 登录成功后的返回处理
|
|
|
*/
|
|
|
@Component
|
|
|
public class MyKakoAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler{
|
|
|
protected final Log logger = LogFactory.getLog(this.getClass());
|
|
|
|
|
|
@Value("${jwt.max-alive}")
|
|
|
protected Integer jwtMaxAlive;
|
|
|
|
|
|
@Autowired
|
|
|
private ObjectMapper objectMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private PermissionService permissionService;
|
|
|
|
|
|
private RequestCache requestCache = new HttpSessionRequestCache();
|
|
|
|
|
|
@Autowired
|
|
|
private SecurityProperties securityProperties;
|
|
|
|
|
|
@Autowired
|
|
|
RedisUtils redisUtils;
|
|
|
@Override
|
|
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
|
|
|
logger.info("登录成功");
|
|
|
if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())){
|
|
|
//将 authention 信息打包成json格式返回
|
|
|
response.setContentType("application/json;charset=UTF-8");
|
|
|
// response.setHeader("Access-Control-Allow-Origin","*");
|
|
|
|
|
|
|
|
|
KakoUser user = (KakoUser) authentication.getPrincipal();
|
|
|
|
|
|
//返回前端登陆成功后的用户信息
|
|
|
KakoUser loginedUser = new KakoUser();
|
|
|
loginedUser.setLoginName(user.getUsername());
|
|
|
loginedUser.setId(user.getId());
|
|
|
loginedUser.setName(user.getName());
|
|
|
|
|
|
|
|
|
//设置用户的TOKEN的有效时间,时间配置在配置文件中设置
|
|
|
String jwtToken = JwtTokenUtil.generateToken(loginedUser.getUsername(), jwtMaxAlive);
|
|
|
loginedUser.setToken(jwtToken);
|
|
|
//这里将登录成功的[user]对象数据写入redis缓存,KEY为token value为user的JSON对象
|
|
|
String json = JSON.toJSONString(user);
|
|
|
redisUtils.set(jwtToken, json,3600*24*7);
|
|
|
Map<String,Object> menuMap = permissionService.getUserMenusKako(user.getId());
|
|
|
//返回用户信息和用户可访问的目录列表
|
|
|
response.getWriter().write(objectMapper.writeValueAsString(new AuthSuccessResponse(loginedUser,menuMap)));
|
|
|
}else {
|
|
|
//走原来的处理流程
|
|
|
SavedRequest savedRequest = this.requestCache.getRequest(request, response);
|
|
|
if (savedRequest == null) {
|
|
|
super.onAuthenticationSuccess(request, response, authentication);
|
|
|
} else {
|
|
|
String targetUrlParameter = this.getTargetUrlParameter();
|
|
|
if (!this.isAlwaysUseDefaultTargetUrl() && (targetUrlParameter == null || !StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
|
|
|
this.clearAuthenticationAttributes(request);
|
|
|
String targetUrl = savedRequest.getRedirectUrl();
|
|
|
this.logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl);
|
|
|
this.getRedirectStrategy().sendRedirect(request, response, targetUrl);
|
|
|
} else {
|
|
|
this.requestCache.removeRequest(request, response);
|
|
|
super.onAuthenticationSuccess(request, response, authentication);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|