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

认证信息在页面显示

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



修改realm设置完整认证信息

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

先使用静态代码实现:


	//注入SysService来调用数据库的相关数据
		@Autowired
		private 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中取出activeUser
		Subject subject = SecurityUtils.getSubject();
		//取出身份信息
		ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
		//通过model传给页面
		model.addAttribute("activeUser", activeUser);
		return "/first";
	}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值