NutzCN Logo
问答 org.apache.shiro.authc.pam.UnsupportedTokenException: Realm
发布于 2993天前 作者 Hamming 10065 次浏览 复制 上一个帖子 下一个帖子
标签:

这段代码是抄nutz-book-project

Subject subject = SecurityUtils.getSubject();
            Toolkit.doLogin(new SimpleShiroToken(user.getId()), user.getId());
            subject.getSession().setAttribute("me", user.getId());

难道我要重新写一个realm吗

2016-10-12 11:29:04,889 nuoshang.bluejay.common.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) DEBUG - [POST]URI=/app/app_users/login 231283ms
2016-10-12 11:29:04,890 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) WARN  - Error@/app/app_users/login :
org.apache.shiro.authc.pam.UnsupportedTokenException: Realm [nuoshang.bluejay.common.shiro.realm.NutDaoRealm@6ef3f5] does not support authentication token [org.nutz.integration.shiro.SimpleShiroToken@8f21b0].  Please ensure that the appropriate Realm implementation is configured correctly or that the realm accepts AuthenticationTokens of this type.
	at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:178)
	at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:267)
	at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)
	at org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106)
	at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:270)
	at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:256)
	at nuoshang.bluejay.common.util.Toolkit.doLogin(Toolkit.java:18)
	at nuoshang.bluejay.modules.app.controllers.AppUserModules.login(AppUserModules.java:79)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at nuoshang.bluejay.common.processor.XssSqlFilterProcessor.process(XssSqlFilterProcessor.java:35)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at nuoshang.bluejay.common.processor.NutShiroProcessor.process(NutShiroProcessor.java:52)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at nuoshang.bluejay.common.processor.GlobalsSettingProcessor.process(GlobalsSettingProcessor.java:36)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at nuoshang.bluejay.common.processor.LogTimeProcessor.process(LogTimeProcessor.java:21)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:198)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
	at nuoshang.bluejay.common.filter.RouteFilter.doFilter(RouteFilter.java:33)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
	at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:224)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
	at org.eclipse.jetty.server.Server.handle(Server.java:499)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
	at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
	at java.lang.Thread.run(Thread.java:745)

10 回复

realm实现类与token类是绑定关系

看realm实现类的构造方法,里面会声明允许接受的token类型

@wendal
就是这段代码喽

	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		SimpleShiroToken upToken = (SimpleShiroToken) token;

		User user = dao().fetch(User.class, ((Integer)upToken.getPrincipal()).longValue());
		if (user == null)
			return null;
		if (user.isLocked())
			throw new LockedAccountException("Account [" + user.getName() + "] is locked.");
		return new SimpleAccount(user.getId(), user.getPassword(), getName());
	}

@wendal
这段代码呀

	public SimpleAuthorizingRealm() {
		this(null, null);
	}

	public SimpleAuthorizingRealm(CacheManager cacheManager, CredentialsMatcher matcher) {
		super(cacheManager, matcher);
		setAuthenticationTokenClass(SimpleShiroToken.class);
	}

	public SimpleAuthorizingRealm(CacheManager cacheManager) {
		this(cacheManager, null);
	}

	public SimpleAuthorizingRealm(CredentialsMatcher matcher) {
		this(null, matcher);
	}

@wendal
nutzwk是酱紫实现的构造方法 我得怎么改呢


public class NutDaoRealm extends AbstractNutRealm { private static final Log log = Logs.get(); public NutDaoRealm() { setAuthenticationTokenClass(CaptchaToken.class); } protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { CaptchaToken authcToken = (CaptchaToken) token; String loginname = authcToken.getUsername(); String captcha = authcToken.getCaptcha(); if (Strings.isBlank(loginname)) { throw Lang.makeThrow(AuthenticationException.class, "Account name is empty"); } Sys_user user = getUserService().fetch(Cnd.where("loginname", "=", loginname)); if (Lang.isEmpty(user)) { throw Lang.makeThrow(UnknownAccountException.class, "Account [ %s ] not found", loginname); } int errCount = NumberUtils.toInt(Strings.sNull(SecurityUtils.getSubject().getSession(true).getAttribute("errCount"))); if (errCount > 2) { //输错三次显示验证码窗口 if (Strings.isBlank(captcha)) { throw Lang.makeThrow(EmptyCaptchaException.class, "Captcha is empty"); } String _captcha = Strings.sBlank(SecurityUtils.getSubject().getSession(true).getAttribute("captcha")); if (!authcToken.getCaptcha().equalsIgnoreCase(_captcha)) { throw Lang.makeThrow(IncorrectCaptchaException.class, "Captcha is error"); } } if (user.isDisabled()) { throw Lang.makeThrow(LockedAccountException.class, "Account [ %s ] is locked.", loginname); } SecurityUtils.getSubject().getSession(true).setAttribute("errCount", 0); SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), getName()); info.setCredentialsSalt(ByteSource.Util.bytes(user.getSalt())); return info; } /* protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { SimpleShiroToken upToken = (SimpleShiroToken) token; App_user user = dao().fetch(App_user.class, ((Integer)upToken.getPrincipal()).longValue()); if (user == null) return null; if (user.isDisabled()) throw new LockedAccountException("Account [" + user.getNickname() + "] is locked."); return new SimpleAccount(user.getId(), user.getPassword(), getName()); } public void setDao(Dao dao) { this.dao = dao; }*/ }

@wendal
nutzwk是酱紫实现的构造方法 我得怎么改呢


public class NutDaoRealm extends AbstractNutRealm { private static final Log log = Logs.get(); public NutDaoRealm() { setAuthenticationTokenClass(CaptchaToken.class); } protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { CaptchaToken authcToken = (CaptchaToken) token; String loginname = authcToken.getUsername(); String captcha = authcToken.getCaptcha(); if (Strings.isBlank(loginname)) { throw Lang.makeThrow(AuthenticationException.class, "Account name is empty"); } Sys_user user = getUserService().fetch(Cnd.where("loginname", "=", loginname)); if (Lang.isEmpty(user)) { throw Lang.makeThrow(UnknownAccountException.class, "Account [ %s ] not found", loginname); } int errCount = NumberUtils.toInt(Strings.sNull(SecurityUtils.getSubject().getSession(true).getAttribute("errCount"))); if (errCount > 2) { //输错三次显示验证码窗口 if (Strings.isBlank(captcha)) { throw Lang.makeThrow(EmptyCaptchaException.class, "Captcha is empty"); } String _captcha = Strings.sBlank(SecurityUtils.getSubject().getSession(true).getAttribute("captcha")); if (!authcToken.getCaptcha().equalsIgnoreCase(_captcha)) { throw Lang.makeThrow(IncorrectCaptchaException.class, "Captcha is error"); } } if (user.isDisabled()) { throw Lang.makeThrow(LockedAccountException.class, "Account [ %s ] is locked.", loginname); } SecurityUtils.getSubject().getSession(true).setAttribute("errCount", 0); SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), getName()); info.setCredentialsSalt(ByteSource.Util.bytes(user.getSalt())); return info; } /* protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { SimpleShiroToken upToken = (SimpleShiroToken) token; App_user user = dao().fetch(App_user.class, ((Integer)upToken.getPrincipal()).longValue()); if (user == null) return null; if (user.isDisabled()) throw new LockedAccountException("Account [" + user.getNickname() + "] is locked."); return new SimpleAccount(user.getId(), user.getPassword(), getName()); } public void setDao(Dao dao) { this.dao = dao; }*/ }

@wendal 这方法继承的是抽象类
重新的方法 带super就会提示错误
public NutDaoRealm(CacheManager cacheManager, CredentialsMatcher matcher) {
super(cacheManager, matcher);
setAuthenticationTokenClass(SimpleShiroToken.class);
}

package nuoshang.bluejay.common.shiro.realm;

import nuoshang.bluejay.modules.back.sys.models.Sys_role;
import nuoshang.bluejay.modules.back.sys.models.Sys_user;
import nuoshang.bluejay.modules.back.sys.services.RoleService;
import nuoshang.bluejay.modules.back.sys.services.UserService;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.nutz.ioc.Ioc;
import org.nutz.lang.Lang;
import org.nutz.mvc.Mvcs;

public abstract class AbstractNutRealm extends AuthorizingRealm {

    private UserService userService;
    private RoleService roleService;

    protected UserService getUserService() {
        if (Lang.isEmpty(userService)) {
            Ioc ioc = Mvcs.getIoc();
            userService = ioc.get(UserService.class);
        }
        return userService;
    }

    protected RoleService getRoleService() {
        if (Lang.isEmpty(roleService)) {
            Ioc ioc = Mvcs.getIoc();
            roleService = ioc.get(RoleService.class);
        }
        return roleService;
    }

    /**
     * 更新用户授权信息缓存.
     */
    public void clearCachedAuthorizationInfo(String principal) {
        SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
        clearCachedAuthorizationInfo(principals);
    }

    /**
     * 清除所有用户授权信息缓存.
     */
    public void clearAllCachedAuthorizationInfo() {
        Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
        if (cache != null) {
            for (Object key : cache.keys()) {
                cache.remove(key);
            }
        }
    }

    /**
     * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
     */
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        Sys_user user = (Sys_user) principals.getPrimaryPrincipal();
        if (!Lang.isEmpty(user)) {
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            info.addRoles(getUserService().getRoleCodeList(user));
            for (Sys_role role : user.getRoles()) {
                if (!role.isDisabled())
                    info.addStringPermissions(getRoleService().getPermissionNameList(role));
            }
            return info;
        } else {
            return null;
        }
    }
}

关键只有一句 setAuthenticationTokenClass

@wendal 那我还不如重新写一个realm

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