NutzCN Logo
问答 shiro环境下方法已经调用但是页面没有跳转
发布于 2676天前 作者 qq_ff330a3f 2048 次浏览 复制 上一个帖子 下一个帖子
标签:

重写授权方法:

package net.retror.bd.shiro.realm;

import java.util.List;
import net.retror.bd.bean.User;
import net.retror.bd.service.UserOpService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAccount;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.nutz.integration.shiro.SimpleShiroToken;
import org.nutz.mvc.Mvcs;
/**
 *
 * @author Retror
 */
public class SimpleAuthorizingRealm extends AuthorizingRealm {
    //声明自定义service,并生成getter和setter方法,在getter方法利用Nutz方法获取自定义service
    private UserOpService userOpService;
    
    protected Dao dao;

    //授权
    /**
     *
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("doGetAuthorizationInfo......");
        // null usernames are invalid
//        if (principals == null) {
//            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
//        }
//        int userId = (Integer) principals.getPrimaryPrincipal();
//        User user = dao().fetch(User.class, userId);
//        if (user == null)
//            return null;
//
//        SimpleAuthorizationInfo auth = new SimpleAuthorizationInfo();
//        user = dao().fetchLinks(user, null);
//        return auth;
        
        //用户名  
        //String username = (String) principals.fromRealm(getName()).iterator().next();  
        //Integer username = (Integer) principals.fromRealm(getName()).iterator().next(); 
        Integer userId = (Integer)principals.fromRealm(getName()).iterator().next(); 
        User user = dao().fetch(User.class, userId.longValue());
        if(user!=null){
            //根据用户ID去查询其角色和拥有的权限
            List<Record> lists = getUserOpService().getRolePermissionData(userId);
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); 
            for(Record record:lists){
                info.addRole(record.getString("roleCode"));
                info.addStringPermission(record.getString("roleCode")+":"+record.getString("resourceDesp"));
                System.out.println(record.getString("roleCode")+":"+record.getString("resourceDesp"));
            }
            return info;
        }
        
//        String username = user.getUserName();
//        //String username = "guest";
//         
//        /*这些代码应该是动态从数据库中取出的,此处写死*/  
//        if(username!=null&&username.equals("admin")){  
//            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();  
//            info.addRole("ROLE_ADMIN");//添加一个角色,不是配置意义上的添加,而是证明该用户拥有admin角色  
//            info.addStringPermission("ROLE_ADMIN:sys-manage");//添加权限  
//            return info;  
//            
//
//        }
//        else{
//            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();  
//            info.addRole("ROLE_GUEST");//添加一个角色,不是配置意义上的添加,而是证明该用户拥有admin角色  
//            info.addStringPermission("ROLE_GUEST:manage");//添加权限  
//            return info; 
//        }
        return null;
        
        
        
    }

    //认证
    /**
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("doGetAuthenticationInfo......");
        SimpleShiroToken upToken = (SimpleShiroToken) token;

        //还可以把用户存储到session当中
        
        User user = dao().fetch(User.class, ((Integer)upToken.getPrincipal()).longValue());
        if (user == null)
            return null;
        return new SimpleAccount(user.getUserId(), user.getPassword(), getName());
    }
    
    /**
     * 覆盖父类的验证,直接pass
     * @param token
     * @param info
     */
    @Override
    protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {
    }

    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);
    }

    public Dao dao() {
        if (dao == null) {
            dao = Mvcs.ctx().getDefaultIoc().get(Dao.class, "dao");
            return dao;
        }
        return dao;
    }

    public void setDao(Dao dao) {
        this.dao = dao;
    }

    public UserOpService getUserOpService() {
        if(userOpService==null){
            userOpService = Mvcs.ctx().getDefaultIoc().get(UserOpService.class);
        }
        return userOpService;
    }

    public void setUserOpService(UserOpService userOpService) {
        this.userOpService = userOpService;
    }

}
    

入口方法:

@At
    @Ok("jsp:admin-unit")
    public Object findAllDepartment(HttpServletRequest resq){
        NutMap re = new NutMap();
        List<Department> departments =  deptOpService.findAllDepartment();
        re.setv("unitList", departments).setv("dataCount", departments.size());
        return re;
    }

查看页面控制台的时候方法已经调用了,但是页面却没有跳转。。。

10 回复

jsp, 浏览器URL不会变的

@wendal 那我应该如何修改呢?因为我都是跳转到jsp页面的

@qq_ff330a3f 纠结的是页面url变化还是内容?

@wendal 我倒是不纠结,我就是想跳转到那个jsp页面。。。仅此而已了。。。是不是我这种方式不对还是怎么呢?如果不对的话该如何修改呢?因为我从静态页面上访问url就可以页面跳转,但是我一旦利用shiro进行登录认证之后,访问url就只是调用方法了

@wendal

<div class="menu_dropdown bk_2">
		<dl id="menu-admin">
			<dt><i class="Hui-iconfont">&#xe62d;</i> 管理员管理<i class="Hui-iconfont menu_dropdown-arrow">&#xe6d5;</i></dt>
			<dd>
				<ul>
					<li><a _href="<%=basePath%>user/findAllUser" data-title="用户管理" href="javascript:void(0)">用户管理</a></li>
                                        <shiro:hasRole name="unitEdit">
                                            <li><a _href="<%=basePath%>dept/findAllDepartment" data-title="单位管理" href="javascript:void(0)">单位管理</a></li>
                                        </shiro:hasRole>
					<li><a _href="<%=basePath%>role/findAllRole" data-title="角色管理" href="javascript:void(0)">角色管理</a></li>
					<li><a _href="<%=basePath%>resource/toMenu" data-title="菜单管理" href="javascript:void(0)">菜单管理</a></li>
				</ul>
			</dd>
		</dl>
	</div>

这是我登录进来的jsp页面(部分代码),就是点击单位管理的时候,只是调用了,但是没有跳转过去。。。

@qq_ff330a3f 看上去是ajax/pjax做跳转的?

@wendal 是不是我不太清楚了,我就是查出来数据之后就返回到我指定的jsp页面,然后在jsp动态加载。现在就是跳转不到那个页面。。。。

@wendal 现在就是不明白为什么方法调用了,但是却没有跳转的方法成功调用返回的jsp页面,不清楚是我写法的问题还是跟权限配置那里有关系。。。。

有没有进入口方法

@wendal 我解决了,是我那个打开页面的位置不对,我不该用[_href]谢谢兽总了~~~~

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