filter:
public class TokenFilter implements ActionFilter {
private static final Log log = Logs.get();
private ApiService apiService= Mvcs.ctx().getDefaultIoc().get(ApiService.class);
public View match(ActionContext context) {
String appId = Strings.sNull(context.getRequest().getParameter("appId"));
String token = Strings.sNull(context.getRequest().getParameter("token"));
if(token.isEmpty()||appId.isEmpty()){
return new UTF8JsonView(JsonFormat.compact()).setData(Result.error(-1,"no appId or token found"));
}
NutMap nutMap=(NutMap) apiService.verifyToken(appId,token);
if (nutMap==null||nutMap.get("ok")==null||!(boolean) nutMap.get("ok")){
return new UTF8JsonView(JsonFormat.compact()).setData(Result.error(-2,"token invalid"));
}else{
if(nutMap.get("userId")==null){
return new UTF8JsonView(JsonFormat.compact()).setData(Result.error(-3,"unknown error"));
}else{
//此处
context.getRequest().setAttribute("userId",nutMap.get("userId"));
}
}
return null;
}
}
method:
@At
@Ok("json")
@POST
public Object collectObject(@Param("channelId") String id, @Param("userId") String userId,HttpServletRequest req) {
try {
Rcd_article_collect rcd_article_collect = new Rcd_article_collect();
rcd_article_collect.setArticleId(id);
rcd_article_collect.setUserId(userId);
rcd_article_collect.setTrue(true);
rcdArticleCollectService.insert(rcd_article_collect);
return Result.success("收藏成功");
} catch (Exception e) {
return Result.success("收藏失败");
}
}
使用req.getParameter("userId"); 也是空