init
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package com.newyetai.security.handle;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.DisabledException;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
|
||||
|
||||
import com.newyetai.common.AlitaResult;
|
||||
import com.newyetai.common.util.RequestUtils;
|
||||
import com.newyetai.common.util.ResponseUtils;
|
||||
import com.newyetai.security.expection.BadLoginException;
|
||||
import com.newyetai.security.expection.DuplicateOrgException;
|
||||
import com.newyetai.security.expection.DuplicateUserException;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author guhong
|
||||
* @date 2019/8/25
|
||||
*/
|
||||
@Slf4j
|
||||
public class MyAuthenctiationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
|
||||
|
||||
private static final String DIABLE_MSG = "用户被禁用";
|
||||
private static final String BAD_CREDENTIALS_MSG = "账号和密码不匹配,请重新登录";
|
||||
|
||||
|
||||
public MyAuthenctiationFailureHandler(String loginFormUrl) {
|
||||
super(loginFormUrl);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
|
||||
AuthenticationException exception) throws IOException, ServletException {
|
||||
log.debug("登录失败:{}", exception);
|
||||
if (exception instanceof BadLoginException) {
|
||||
ResponseUtils.printJson(response, ((BadLoginException) exception).getResult());
|
||||
return;
|
||||
}
|
||||
if (RequestUtils.isAjaxRequest(request)) {
|
||||
ResponseUtils.printJson(response, AlitaResult.error(dealExceptionMsg(exception)));
|
||||
} else {
|
||||
super.onAuthenticationFailure(request, response, exception);
|
||||
}
|
||||
}
|
||||
|
||||
public String dealExceptionMsg(AuthenticationException exception) {
|
||||
if (exception instanceof DisabledException) {
|
||||
return DIABLE_MSG;
|
||||
}
|
||||
if (exception instanceof BadCredentialsException) {
|
||||
return BAD_CREDENTIALS_MSG;
|
||||
}
|
||||
return exception.getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.newyetai.security.handle;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.newyetai.common.AlitaResult;
|
||||
import com.newyetai.common.dto.CasTokenDTO;
|
||||
import com.newyetai.common.util.CookieUtils;
|
||||
import com.newyetai.common.util.JsonUtils;
|
||||
import com.newyetai.common.util.RequestUtils;
|
||||
import com.newyetai.common.util.ResponseUtils;
|
||||
import com.newyetai.security.config.SecurityProperties;
|
||||
import com.newyetai.security.dto.SecurityUserDTO;
|
||||
import com.newyetai.security.jwt.JwtTokenUtils;
|
||||
import com.newyetai.security.ssb.SsbAuthenticationFilter;
|
||||
import com.newyetai.security.util.SecurityUtils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 登录成功处理器
|
||||
*
|
||||
* @author guhong
|
||||
* @date 2019/8/19
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class MyLoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
|
||||
|
||||
@Autowired
|
||||
private SecurityProperties securityProperties;
|
||||
|
||||
@Autowired
|
||||
private JwtTokenUtils jwtTokenUtils;
|
||||
|
||||
@Override
|
||||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
|
||||
Authentication authentication) throws ServletException, IOException {
|
||||
Object principal = authentication.getPrincipal();
|
||||
|
||||
log.debug("------MyLoginSuccessHandler onAuthenticationSuccess userDTO: {}",
|
||||
JsonUtils.toJsonString(principal));
|
||||
CasTokenDTO caseTokenDTO = null;
|
||||
if (principal instanceof SecurityUserDTO) {
|
||||
SecurityUserDTO securityUserDto = (SecurityUserDTO) principal;
|
||||
caseTokenDTO = CasTokenDTO.builder().register(securityUserDto.getRegister())
|
||||
.token(jwtTokenUtils.generateToken(securityUserDto)).build();
|
||||
} else if (principal instanceof CasTokenDTO) {
|
||||
caseTokenDTO = (CasTokenDTO) principal;
|
||||
} else {
|
||||
throw new RuntimeException("数据异常");
|
||||
}
|
||||
String token = caseTokenDTO.getToken();
|
||||
this.dealCookie(token, request, response);
|
||||
if (RequestUtils.isAjaxRequest(request) || RequestUtils.isFeignRequest(request)) {
|
||||
// ajax
|
||||
ResponseUtils.printJson(response,
|
||||
AlitaResult.success(CasTokenDTO.builder().token(token).build()));
|
||||
} else {
|
||||
String successPage = securityProperties.getLoginSuccessPage();
|
||||
if (request.getRequestURI().contains(SsbAuthenticationFilter.LOGIN_URI)) {
|
||||
successPage = securityProperties.getLoginH5SuccessPage();
|
||||
}
|
||||
String successToUrl =
|
||||
SecurityUtils.dealSuccessUrl(request, successPage, token);
|
||||
response.sendRedirect(successToUrl);
|
||||
}
|
||||
}
|
||||
|
||||
private void dealCookie(String token, HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
// 处理cookie
|
||||
String domainStr = RequestUtils.getRootDomain(request);
|
||||
String key = CookieUtils.getLoginTokenCookieKey();
|
||||
Cookie cookie = CookieUtils.createCookie(key, token, domainStr,
|
||||
null, jwtTokenUtils.getExpiration().intValue());
|
||||
//写入Cookie
|
||||
response.addCookie(cookie);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.newyetai.security.handle;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
|
||||
|
||||
import com.newyetai.common.AlitaResult;
|
||||
import com.newyetai.common.util.RequestUtils;
|
||||
import com.newyetai.common.util.ResponseUtils;
|
||||
import com.newyetai.security.expection.BadLoginException;
|
||||
|
||||
/**
|
||||
* 自定义认证失败处理器
|
||||
* @author guhong
|
||||
* @date 2019/8/25
|
||||
*/
|
||||
public class MyLoginUrlAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
|
||||
|
||||
/**
|
||||
* @param loginFormUrl URL where the login page can be found. Should either be relative to the
|
||||
* web-app context path (include a leading {@code /}) or an absolute URL.
|
||||
*/
|
||||
public MyLoginUrlAuthenticationEntryPoint(String loginFormUrl) {
|
||||
super(loginFormUrl);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commence(HttpServletRequest request, HttpServletResponse response,
|
||||
AuthenticationException authException) throws IOException, ServletException {
|
||||
// ajax|feign
|
||||
if (RequestUtils.isAjaxRequest(request) || RequestUtils.isFeignRequest(request)) {
|
||||
if (authException instanceof BadLoginException) {
|
||||
ResponseUtils.printJson(response, ((BadLoginException) authException).getResult());
|
||||
return;
|
||||
}
|
||||
ResponseUtils.printJson(response,
|
||||
AlitaResult.error(AlitaResult.LOGIN_ERROR_CODE, "鉴权失败:" + authException.getMessage()));
|
||||
} else {
|
||||
super.commence(request, response, authException);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.newyetai.security.handle;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.newyetai.common.AlitaResult;
|
||||
import com.newyetai.common.util.CookieUtils;
|
||||
import com.newyetai.common.util.RequestUtils;
|
||||
import com.newyetai.common.util.ResponseUtils;
|
||||
|
||||
/**
|
||||
* @author guhong
|
||||
*/
|
||||
@Component
|
||||
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
|
||||
|
||||
@Override
|
||||
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
|
||||
Authentication authentication) throws IOException, ServletException {
|
||||
//清空Cookie
|
||||
CookieUtils.removeCookie(CookieUtils.getLoginTokenCookieKey(),
|
||||
RequestUtils.getRootDomain(request), request, response);
|
||||
// ajax登出
|
||||
if (RequestUtils.isAjaxRequest(request)) {
|
||||
ResponseUtils.printJson(response, AlitaResult.success());
|
||||
} else {
|
||||
response.sendRedirect("/");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user