认证信息在页面显示
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";
}