react 前端框架如何驱动企业数字化转型与创新发展
727
2023-07-08
Spring自定义参数解析器代码实例
这篇文章主要介绍了Spring自定义参数解析器代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
结合redis编写User自定义参数解析器UserArgumentResolver
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
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;
import cn.xingaohbd.seckil.model.User;
import cn.xingaohbd.seckil.service.UserService;
@Service
public class UserArgumentResolver implements HandlerMethodArgumentResolver {
@Autowired
public UserService userService;
public String getParameterCokies(HttpServletRequest request,String tokenName){
Cookie[] cookies = request.getCookies();
for (Cookie ck : cookies) {
if(ck.getName().equals(tokenName)){
return ck.getValue();
http:// }
}
return null;
}
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
Class> p_class = methodParameter.getParameterType();
return p_class == User.class;
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest nativeWebRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
String requestParameter_token = request.getParameter("token");
String cokies_token = getParameterCokies(request, "token");
if(requestParameter_token == null && cokies_token == null){
return null;
}
return userService.getUserFromRedisByToken(requestParameter_token != null ? requestParameter_token : cokimbxJqKTHes_token);
}
}
UserService
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserRedis userRedis;
@Override
public Object getUserFromRedisByToken(String token) {
return userRedis.get(token);
}
}
在WebConfig中注册自定义参数解析器UserArgumentResolver
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebConfig extends WebMvcConfigurationSupport{
@Autowired
UserArgumentResolver userArgumentResolver;
@Override
protected void addArgumentResolvers(List
argumentResolvers.add(userArgumentResolver);
}
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_UmbxJqKTHRL_PREFIX+"/static/");
super.addResourceHandlers(registry);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~