|
|
@@ -7,16 +7,26 @@ import cn.hutool.http.useragent.UserAgentUtil;
|
|
|
import com.zhongzheng.common.constant.Constants;
|
|
|
import com.zhongzheng.common.core.domain.model.LoginUser;
|
|
|
import com.zhongzheng.common.core.redis.RedisCache;
|
|
|
+import com.zhongzheng.common.exception.CustomException;
|
|
|
+import com.zhongzheng.common.exception.user.UserPasswordNotMatchException;
|
|
|
+import com.zhongzheng.common.utils.MessageUtils;
|
|
|
import com.zhongzheng.common.utils.ServletUtils;
|
|
|
import com.zhongzheng.common.utils.ip.AddressUtils;
|
|
|
import com.zhongzheng.common.utils.ip.IpUtils;
|
|
|
+import com.zhongzheng.framework.manager.AsyncManager;
|
|
|
+import com.zhongzheng.framework.manager.factory.AsyncFactory;
|
|
|
import io.jsonwebtoken.Claims;
|
|
|
import io.jsonwebtoken.Jwts;
|
|
|
import io.jsonwebtoken.SignatureAlgorithm;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.security.authentication.AuthenticationManager;
|
|
|
+import org.springframework.security.authentication.BadCredentialsException;
|
|
|
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
@@ -51,6 +61,9 @@ public class TokenService
|
|
|
@Autowired
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private AuthenticationManager authenticationManager;
|
|
|
+
|
|
|
/**
|
|
|
* 获取用户身份信息
|
|
|
*
|
|
|
@@ -58,6 +71,11 @@ public class TokenService
|
|
|
*/
|
|
|
public LoginUser getLoginUser(HttpServletRequest request)
|
|
|
{
|
|
|
+ //测试用户
|
|
|
+ String test_token = request.getHeader("X-Auth-Token");
|
|
|
+ if("test".equals(test_token)){
|
|
|
+ return getTestUser();
|
|
|
+ }
|
|
|
// 获取请求携带的令牌
|
|
|
String token = getToken(request);
|
|
|
if (Validator.isNotEmpty(token))
|
|
|
@@ -69,9 +87,36 @@ public class TokenService
|
|
|
LoginUser user = redisCache.getCacheObject(userKey);
|
|
|
return user;
|
|
|
}
|
|
|
+
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ private LoginUser getTestUser(){
|
|
|
+ Authentication authentication = null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
+ authentication = authenticationManager
|
|
|
+ .authenticate(new UsernamePasswordAuthenticationToken("test", "123456"));
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ if (e instanceof BadCredentialsException)
|
|
|
+ {
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor("test", Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
+ throw new UserPasswordNotMatchException();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor("test", Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
+ throw new CustomException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
+ loginUser.setExpireTime(System.currentTimeMillis()+200);
|
|
|
+ return loginUser;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 设置用户身份信息
|
|
|
*/
|