重新发帖
shiro如何生成token呢?比如通过shiro生成一个3个月后过期的token该如何实行呢
14 回复
@wendal 那shiro可以和OAuth 2.0结合生成token么
@tonyyule 你指的token到底是什么?是不是跟shiro登录用的UsernamePasswordToken混淆了?
@wendal 就是shiro登录用的UsernamePasswordToken 这个token怎么生成
@tonyyule 直接new的
@wendal 有案例吗 Shiro单点登录(自定义Token令牌)
@tonyyule 谷歌shiro cas很多教程
@wendal CAS好复杂 我就想做成想论坛APP 识别登录的
@wendal 想用session相关的access token jwts 不用了
@tonyyule 那抄一下论坛的代码不就哦了,客户端上也是
@wendal
看不懂代码 at获取到 怎么验证的呢?看代码貌似是从redis取
package net.wendal.nutzbook.mvc;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.nutz.integration.shiro.SimpleShiroToken;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.View;
import org.nutz.mvc.impl.processor.AbstractProcessor;
import net.wendal.nutzbook.module.BaseModule;
import net.wendal.nutzbook.service.yvr.YvrService;
/**
* 通过请求参数中的accesstoken进行授权
* @author wendal
*
*/
public class AccessTokenFilter extends AbstractProcessor implements ActionFilter {
private static final Log log = Logs.get();
YvrService yvrService;
public View match(ActionContext ac) {
if (yvrService == null)
yvrService = ac.getIoc().get(YvrService.class);
HttpServletRequest req = ac.getRequest();
String at = req.getParameter("accesstoken");
if (req.getHeader("Api-Version") != null) {
log.debug("api version = " + req.getHeader("Api-Version"));
String loginname = req.getHeader("Api-Loginname");
String nonce = req.getHeader("Api-Nonce");
String key = req.getHeader("Api-Key");
String time = req.getHeader("Api-Time");
if (Strings.isBlank(loginname) || Strings.isBlank(nonce) || Strings.isBlank(key) || Strings.isBlank(time)) {
return BaseModule.HTTP_403;
}
if (!yvrService.checkNonce(nonce, time)){
return BaseModule.HTTP_403;
}
at = yvrService.accessToken(loginname);
if (Strings.isBlank(at)) {
return BaseModule.HTTP_403;
}
String tmp = Strings.join(",", at, loginname, nonce, time);
String _key = Lang.sha1(tmp);
log.debug("tmp="+tmp);
log.debug("_key=" + _key);
log.debug(" key=" + key);
if (!_key.equals(key)) {
return BaseModule.HTTP_403;
}
log.debug("api access token check ok");
}
else if (Strings.isBlank(at)) { // TODO 移除这种兼容性,改成必须用nonce加密
return BaseModule.HTTP_403;
}
int uid = yvrService.getUserByAccessToken(at);
if (uid < 1) {
return BaseModule.HTTP_403;
}
SecurityUtils.getSubject().getSession().setAttribute("me", uid);
return null;
}
public void process(ActionContext ac) throws Throwable {
Subject subject = SecurityUtils.getSubject();
Integer uid = (Integer) subject.getSession().getAttribute("me");
if (!subject.isAuthenticated())
subject.login(new SimpleShiroToken(uid));
doNext(ac);
if (!subject.isAuthenticated())
subject.logout();
}
}
@tonyyule 我给的链接有解释
添加回复
请先登陆