Browse Source

先用着 再优化

dev
jianjun 4 years ago
parent
commit
861cc6cf46
  1. 13
      tduck-account/src/main/java/com/tduck/cloud/account/service/impl/UserServiceImpl.java
  2. 35
      tduck-api/src/main/java/com/tduck/cloud/api/web/interceptor/AuthorizationInterceptor.java

13
tduck-account/src/main/java/com/tduck/cloud/account/service/impl/UserServiceImpl.java

@ -111,17 +111,18 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
*/ */
@Override @Override
public LoginUserVO getLoginResult(UserEntity userEntity, AccountChannelEnum channel, String requestIp) { public LoginUserVO getLoginResult(UserEntity userEntity, AccountChannelEnum channel, String requestIp) {
LoginUserVO loginUserVO = new LoginUserVO();
loginUserVO.setCustomerId(userEntity.getCustomerId());
loginUserVO.setUserId(userEntity.getId());
LoginUserUtil.set(loginUserVO);
userEntity.setLastLoginIp(requestIp); userEntity.setLastLoginIp(requestIp);
userEntity.setLastLoginChannel(channel); userEntity.setLastLoginChannel(channel);
userEntity.setLastLoginTime(LocalDateTime.now()); userEntity.setLastLoginTime(LocalDateTime.now());
this.updateById(userEntity); this.updateById(userEntity);
String token = jwtUtils.generateToken(userEntity.getId()); String token = jwtUtils.generateToken(userEntity.getId());
//获取当前用户的客户Id及用户Id
LoginUserVO loginUserVO = LoginUserUtil.get(); return new LoginUserVO(userEntity.getAvatar(), userEntity.getName(), token, userEntity.getCustomerId(), userEntity.getId());
if (loginUserVO == null){
loginUserVO = new LoginUserVO();
}
return new LoginUserVO(userEntity.getAvatar(), userEntity.getName(), token, loginUserVO.getCustomerId(), loginUserVO.getUserId());
} }
@Override @Override

35
tduck-api/src/main/java/com/tduck/cloud/api/web/interceptor/AuthorizationInterceptor.java

@ -10,11 +10,14 @@ import com.tduck.cloud.common.vo.LoginUserVO;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod; import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
/** /**
* @author qing * @author qing
@ -23,6 +26,15 @@ import javax.servlet.http.HttpServletResponse;
public class AuthorizationInterceptor extends HandlerInterceptorAdapter { public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
public static final String USER_KEY = "userId"; public static final String USER_KEY = "userId";
private final JwtUtils jwtUtils; private final JwtUtils jwtUtils;
private static List<String> skipUrlList;
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
@PostConstruct
private void init(){
skipUrlList = new ArrayList<>();
skipUrlList.add("/tduck-api/captcha/get");
skipUrlList.add("/tduck-api/captcha/check*");
skipUrlList.add("/tduck-api/login/account*");
}
public AuthorizationInterceptor(JwtUtils jwtUtils) { public AuthorizationInterceptor(JwtUtils jwtUtils) {
this.jwtUtils = jwtUtils; this.jwtUtils = jwtUtils;
@ -31,7 +43,7 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Login annotation; Login annotation;
if (handler instanceof HandlerMethod) { /*if (handler instanceof HandlerMethod) {
annotation = ((HandlerMethod) handler).getMethodAnnotation(Login.class); annotation = ((HandlerMethod) handler).getMethodAnnotation(Login.class);
} else { } else {
return true; return true;
@ -39,8 +51,15 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
if (annotation == null) { if (annotation == null) {
return true; return true;
} }*/
String requestURI = request.getRequestURI();
if (isSkip(requestURI)){
System.out.println("=====skip=====url"+requestURI);
return true;
}else {
System.out.println("==========url"+requestURI);
}
//获取用户凭证 //获取用户凭证
String token = request.getHeader(jwtUtils.getHeader()); String token = request.getHeader(jwtUtils.getHeader());
String customerId = request.getHeader(CommonConstants.KEY_CUSTOMER_ID); String customerId = request.getHeader(CommonConstants.KEY_CUSTOMER_ID);
@ -79,4 +98,14 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter {
//清空threadLocal //清空threadLocal
LoginUserUtil.remove(); LoginUserUtil.remove();
} }
private boolean isSkip(String url){
for (String skipurl : skipUrlList) {
System.out.println(antPathMatcher.match(skipurl, url)+",path"+url+",skipurl:"+skipurl);
if (antPathMatcher.match(skipurl, url)){
return true;
}
}
return false;
}
} }

Loading…
Cancel
Save