NutzCN Logo
问答 自定义ActionFilter的使用
发布于 2567天前 作者 1037424761 1785 次浏览 复制 上一个帖子 下一个帖子
标签:
@At({"/getUser","/getUser/?"})
@Fail("http:200")
@Filters({@By(type = UserFilter.class)})

我自己写了一个拦截器,

String code = Strings.sNull(context.getRequest().getParameter("code"));

有一个code可以.getParameter()获得

 public void getUser(String key,HttpServletRequest req)

其中还有一个原本String key接收的,在拦截器中怎么获得

9 回复
    @At({"/getUser","/getUser/?"})
    @Fail("http:200")
    @Filters({@By(type = UserFilter.class)})
    public void getUser(String key,HttpServletRequest req) throws IOException {
    	/*String code = req.getParameter("code");
    	Wx_config appInfo = wxConfigService.fetch(Cnd.where("id", "=", key));
    	String strings = "";
    	try {
			 strings = HttpUtil.sendPost("https://api.weixin.qq.com/sns/oauth2/access_token", "appid="+appInfo.getAppid()+"&secret="+appInfo.getAppsecret()+"&code="+code+"&grant_type=authorization_code");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} */
    	System.out.println("111111111111111111111111111111");
    }
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb4177e49ba85894c&redirect_uri=http://1037424761.ngrok.wendal.cn/clouds/open/weixin/getUser/bb45db545d8f4330a7fe633f31b0f446&response_type=code&scope=snsapi_base&state=123456#wechat_redirect

微信的权限认证url,我要做一个拦截处理,需要获得appId,还有“code”值
原本 public void getUser(String key,HttpServletRequest req) throws IOException 中的key就是我传的appid,但是在拦截器中,我不知道该怎么获得?
@wendal

哦, 拿不到的,但是可以做个处理器Processor,放在AdaptorProcessor之后

单纯是这个话, 路径参数倒是可以取到

ac.getPathArgs

就是这种效果,十分感谢!

public class UserFilter implements ActionFilter {
	
	private CloudBindService cloudBindService= Mvcs.ctx().getDefaultIoc().get(CloudBindService.class);
	
	private WxConfigService wxConfigService= Mvcs.ctx().getDefaultIoc().get(WxConfigService.class);
	
	@Override
	public View match(ActionContext context) {
		String code = Strings.sNull(context.getRequest().getParameter("code"));
        String key = Strings.sNull(context.getPathArgs()).replace("[", "").replace("]", "");
        Wx_config appInfo = wxConfigService.fetch(Cnd.where("id", "=", key));
        String strings = HttpUtil.sendPost("https://api.weixin.qq.com/sns/oauth2/access_token", "appid="+Strings.sNull(appInfo.getAppid())+"&secret="+Strings.sNull(appInfo.getAppsecret())+"&code="+code+"&grant_type=authorization_code");
        JSONObject json = JSONObject.parseObject(strings);
        if (cloudBindService.count(Cnd.where("id","=",json.get("openid")))<=0) {
        	return new AbstractPathView();
            /*return new UTF8JsonView(JsonFormat.compact()).setData(Result.error(-1,"未绑定商户!"));*/
        }
		return null;
	}

}

@wendal
大神,如果我拦截想要跳转到某个界面,就像登录拦截一样,我改怎么实现。

添加回复
请先登陆
回到顶部