java随记

坚持就是胜利!

 

springboot中action绑定ServletRequest的attirbute的值传参

有些时候需要在后台处理一封装一些参数送到action中便于业务处理.比于app中的token验证,
根据token查找到对应的用户信息。比如分布式框架中获取用户信息等.springboot中可以自
定义参数解析器来绑定参数,通过它可以拿到ServletRequest中的attirbute中的值进行参数
绑定。

自定义一个annotation,通过这个注解中的 name查找attribute中的key对应的值 

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 AttributeResolve {
String name() default "user";
}

自定义一个解析器类

import javax.servlet.http.HttpServletRequest;
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;
public class AttributeArgumentResolver implements HandlerMethodArgumentResolver{
@Override
public Object resolveArgument(MethodParameter arg0, ModelAndViewContainer arg1, NativeWebRequest arg2,
WebDataBinderFactory arg3) throws Exception {
// TODO Auto-generated method stub
Object resultObj=null;
 AttributeResolve mp =  arg0.getParameterAnnotation(AttributeResolve.class);
 if(mp!=null) {
String attributeName= mp.name();
HttpServletRequest request = arg2.getNativeRequest(HttpServletRequest.class);
resultObj = request.getAttribute(attributeName);
 }
return resultObj;
}
@Override
public boolean supportsParameter(MethodParameter arg0) {
// TODO Auto-generated method stub
return  arg0.hasParameterAnnotation(AttributeResolve.class);
}

springboot中注册自定义的参数解析器
@Configuration
public class MyWebMvcConfig extends WebMvcConfigurerAdapter{
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new AttributeArgumentResolver());
    }
}


使用方法.
需要在action调用前向HttpServletRequest中的attribute中注入值 ,可以自定义一个filter,在filter中进行处理
如在filter中处理app传过来的token验证后取得对应的用户信息等.下面例子简单放入一个对象
@WebFilter(filterName = "axssFilter", urlPatterns = "/*",
initParams = {
@WebInitParam(name = "ignores", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源
}
)
public class XssFilter implements javax.servlet.Filter{
private Set<String> prefixIignores = new HashSet<String>();
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
            Blogger user = new Blogger();
          user.setUsername("asfdasdf");
         request.setAttribute("user", user);
               chain.doFilter(request, response);  
}
}

action获取attribute中放入的对象
        @RequestMapping("/index")
public String index(@AttributeResolve(name="user") Bloggerbh, HttpServletRequest request,Model model) 

嗯,还算优雅


posted on 2017-09-29 11:58 傻 瓜 阅读(1687) 评论(0)  编辑  收藏 所属分类: 杂项


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(7)

我参与的团队

随笔分类

随笔档案

文章分类

友情链接

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜