shiro-realm查询认证信息在页面展示

来源:互联网 发布:网络兼职 知乎 编辑:程序博客网 时间:2024/05/30 18:30

认证信息在页面显示

1、认证后用户菜单在首页显示
2、认证后用户的信息在页头显示



修改realm设置完整认证信息

realm从数据库查询用户信息,将用户菜单、usercode、username等设置在SimpleAuthenticationInfo中。

先使用静态代码实现:


//注入SysService来调用数据库的相关数据@Autowiredprivate SysService sysService;//realm的认证方法,模拟从数据库查询用户信息protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {//token是用户输入的//模拟从数据库中查询到的密码String password = "111111";//模拟静态数据ActiveUser activeUser = new ActiveUser();activeUser.setUserid("zhangsan");activeUser.setUsercode("zhangsan");activeUser.setUsername("张三");List<SysPermission> menus = null;//根据用户的id取出菜单try {menus = sysService.findMenuListByUserId("zhangsan");//在实际的数据库中,张三存在菜单的} catch (Exception e) {e.printStackTrace();}//将用户的菜单设置到activeUser中activeUser.setMenus(menus);//将activeUser设置到simpleAuthenticationInfo中SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(activeUser, password, this.getName());return simpleAuthenticationInfo;}



修改first.action将认证信息在页面显示


//系统首页@RequestMapping("/first")public String first(Model model)throws Exception{//从shiro的session中取出activeUserSubject subject = SecurityUtils.getSubject();//取出身份信息ActiveUser activeUser = (ActiveUser) subject.getPrincipal();//通过model传给页面model.addAttribute("activeUser", activeUser);return "/first";}









原创粉丝点击