maven spring hibernate RBAC shiro mysql
代码发布于 http://git.oschina.net/alexgaoyh/alexgaoyh
1:数据库表结构设计采用RBAC权限模型,即五张表结构设计(用户,角色,用户-角色,权限,角色-权限),本文不再详细说明;
2:shiro使用过程中:
2.1:以下这个类是进行用户的数据库验证的,从数据库里面查出是否有这个用户。
public class ShiroDbRealm extends AuthorizingRealm
需要注意的是 doGetAuthenticationInfo 方法的
return new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getPassword()),getName()); 部分;
2.2: 在生成对用表结构的时候,需要注意本例中的user表中password字段使用了MD5加密,
所以,在生成表结构之后,执行以下方法生成对应的用户数据:
public String saveSysmanUserEntity() throws Exception{ SysmanUser su = new SysmanUser(); su.setUserName("admin"); su.setRealName("admim"); su.setPassword(new Md5Hash("admin").toHex()); su.setStatus(SysmanUser.STATUS_NORMAL); sysmanUserService.saveOrUpdate(su); return JSONObject.valueToString(su); }
2.3: 同时在登陆方法中
public ModelAndView doLogin(HttpServletRequest request, HttpServletResponse response) { Map<String, Boolean> map = new HashMap<String, Boolean>(); boolean loginStatus = false; boolean captchaStatus = false; String captcha = request.getParameter("captcha"); String exitCode = (String) request.getSession().getAttribute(CaptchaConstant.KEY_CAPTCHA); if (null == captcha || !captcha.equalsIgnoreCase(exitCode)) { System.out.println("验证码错误"); }else{ captchaStatus = true; String username = request.getParameter("username"); String password = request.getParameter("password"); Md5Hash md5Hash = new Md5Hash(password); UsernamePasswordToken token = new UsernamePasswordToken(username,md5Hash.toHex()); System.out.println(token.getUsername()); System.out.println(token.getPassword()); try { Subject subject = SecurityUtils.getSubject(); subject.login(token); token.clear(); SysmanUser user = (SysmanUser) subject.getPrincipal(); subject.getSession().setAttribute("adminCurrentUser", user); loginStatus = true; } catch (UnknownAccountException ex) { ex.printStackTrace(); } catch (IncorrectCredentialsException ex) { ex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } map.put("loginStatus", loginStatus); map.put("captchaStatus", captchaStatus); ModelAndView mav = new ModelAndView("views/admin/index", map); return mav; }
需要注意用户输入的password是明文,在生成token的时候,需要进行如下操作:
Md5Hash md5Hash = new Md5Hash(password); UsernamePasswordToken token = new UsernamePasswordToken(username,md5Hash.toHex());
时间: 2024-10-10 10:26:25