移动端用户登录信息保存实现

对于H5端的用户,系统部分内容需要登录才能访问。最好做到用到的时候去验证。目前项目里面用户状态管理好像就是这样做的。

参数解析器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.meiyuan.catering.wx.annotation.support;

import com.meiyuan.catering.core.exception.UnauthorizedException;
import com.meiyuan.catering.core.util.SpringContextUtils;
import com.meiyuan.catering.wx.annotation.LoginUser;
import com.meiyuan.catering.wx.dto.UserTokenDTO;
import com.meiyuan.catering.wx.utils.WechatUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
* @author admin
*/
public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
public static final String LOGIN_TOKEN_KEY = "X-Catering-Token";

@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterType().isAssignableFrom(UserTokenDTO.class)
&& parameter.hasParameterAnnotation(LoginUser.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container, NativeWebRequest request,
WebDataBinderFactory factory) throws Exception {

LoginUser loginUser = parameter.getParameterAnnotation(LoginUser.class);
String token = request.getHeader(LOGIN_TOKEN_KEY);
if (StringUtils.isEmpty(token)) {
if (loginUser.required()) {
throw new UnauthorizedException();
}
return null;
}
WechatUtils wechatUtils = SpringContextUtils.getBean(WechatUtils.class);
UserTokenDTO user = wechatUtils.getUser(token);
if (user == null) {
if (loginUser.required()) {
throw new UnauthorizedException();
}
return null;
}
String redisToken = wechatUtils.getToken(user.getUserIdReal());
if (redisToken == null) {
if (loginUser.required()) {
throw new UnauthorizedException("登录信息过期 请重新登录");
}
return null;
}
if (!token.equals(redisToken)) {
if (loginUser.required()) {
throw new UnauthorizedException("账号在其他地方登录 请重新登录");
}
return null;
}
return user;

}
}

配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.meiyuan.catering.wx.config;

import com.meiyuan.catering.wx.annotation.support.LoginUserHandlerMethodArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
* @author admin
*/
@Configuration
public class WxApiMvcConfiguration implements WebMvcConfigurer {

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new LoginUserHandlerMethodArgumentResolver());
}

// @Override
// public void addInterceptors(InterceptorRegistry registry) {
// registry.addInterceptor(tokenInterceptor).excludePathPatterns(excludedPaths);
// }
//
// @Override
// public void addResourceHandlers(ResourceHandlerRegistry registry) {
// registry.addResourceHandler("/**")
// .addResourceLocations("classpath:/static/");
// registry.addResourceHandler("/webjars/**")
// .addResourceLocations("classpath:/META-INF/resources/webjars/");
// registry.addResourceHandler("doc.html", "swagger-ui.html")
// .addResourceLocations("classpath:/META-INF/resources/");
// }

}

辅助注解

1
2
3
4
5
6
7
8
9
10
11
12
package com.meiyuan.catering.wx.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginUser {
boolean required() default true;
}

实际使用

1
2
3
4
public Result xxx(@LoginUser UserTokenDTO token,@Validated @RequestBody ShopApplyDTO param){
param.setApplyUserId(token.getUserId());
return this.xxx.xxx(param);
}
评论

:D 一言句子获取中...