跳过正文

Shiro

3、Session
·870 字·2 分钟· loading · loading
Java JavaEE Shiro
生命周期 # 注意:这个Session不是Tomcat的那个,而是Shrio提供的一个Session
2、认证,加密,授权,缓存
·1602 字·4 分钟· loading · loading
Java JavaEE Shiro
认证 # 1、Controller登录方法 # @RequestMapping("/login.do") public String login(User user,ModelMap map){ //获得一个主体对象 Subject subject = SecurityUtils.getSubject(); //将请求得到的用户名和密码放入一个令牌中 UsernamePasswordToken token = new UsernamePasswordToken(user.getUaccount(),user.getUpsw()); //调用主体对象的认证方法login,对令牌进行认证 try{ //触发Realm中doGetAuthenticationInfo() subject.login(token); Session session = subject.getSession(); User nwoLogin = (User)subject.getPrincipal(); session.setAttribute("nowLogin", nwoLogin); }catch(UnknownAccountException accountException){ map.put("ex", "账号不存在"); return "tologin.do"; }catch(IncorrectCredentialsException passwordExeption){ map.put("ex", "密码校验错误"); return "tologin.do"; } return "redirect:toMain.do"; } 2、Realm类中,doGetAuthenticationInfo方法进行登录认证 # //身份认证方法,需要在用户登录系统时触发 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException { //通过方法形参arg0,获取封装了用户账号密码的令牌 UsernamePasswordToken token = (UsernamePasswordToken)arg0; //验证账号是否存在 String uaccount = token.getUsername(); User user = userService.selectByUaccount(uaccount); if(user == null){ //如果用户名不存在,返回null //则shiro底层返回了一个异常(UnknownAccountException) return null; } //验证密码是否正确 //第一个参数为,数据库查出的user对象,第二个参数为正确的密码,第三个参数为当前realm名 //如果密码不正确,该构造器会抛出异常(IncorrectCredentialsException) return new SimpleAuthenticationInfo(user,user.getUpsw(),getName()); } 加密 # 1、ShiroConfig类中,设置加密 # //设置加密类型(身份匹配器),以及方式 @Bean public HashedCredentialsMatcher credentialsMatcher() { HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher(); // 说密码的加密方式为MD5 (不可逆的加密方式,只能加密,不能解密) credentialsMatcher.setHashAlgorithmName("MD5"); // 针对MD5加密的信息,再加密1024次 credentialsMatcher.setHashIterations(1024); return credentialsMatcher; } //配置shiro框架中,用来完成身份认证,授权的域对象 @Bean public LoginAndAuthRealm loginAndAuthRealm() { LoginAndAuthRealm realm = new LoginAndAuthRealm(); // 给Realm中配置身份对比规则 realm.setCredentialsMatcher(credentialsMatcher()); return realm; } 2、修改Realm组件身份认证方法 # //身份认证方法,需要在用户登录系统时触发 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException { //通过方法形参arg0,获取封装了用户账号密码的令牌 UsernamePasswordToken token = (UsernamePasswordToken)arg0; //验证账号是否存在 String uaccount = token.getUsername(); User user = userService.selectByUaccount(uaccount); if(user == null){ //如果用户名不存在,返回null //则shiro底层返回了一个异常(UnknownAccountException) return null; } //验证密码是否正确 //获取当前用户名,创建盐值对象 ByteSource byteSource = ByteSource.Util.bytes(user.getUaccount()); //第一个参数为,数据库查出的user对象,第二个参数为正确的密码,第三个参数为盐值对象,第四个参数为当前Realm名称 //如果密码不正确,该构造器会抛出异常(IncorrectCredentialsException) return new SimpleAuthenticationInfo(user,user.getUpsw(),byteSource,getName()); } 3、控制层注册方法中,对密码进行MD5加密 # String pwd = new SimpleHash("MD5", 密码,盐值‘一般为用户名’, 加密次数‘1024’).toString();
1、Shiro
·2322 字·5 分钟· loading · loading
Java JavaEE Shiro
Shiro简介 # 公司项目中,常见的权限框架:shiro,spring security Apache Shiro是一个功能强大且灵活的开源安全框架,可以清晰地处理身份验证,授权,企业会话管理和加密 Apache Shiro的首要目标是易于使用和理解。安全有时可能非常复杂,甚至是痛苦的,但并非必须如此。框架应尽可能掩盖复杂性,并提供简洁直观的API,以简化开发人员确保其应用程序安全的工作 Shiro能帮系统做什么 # 做用户的身份认证,判断用户是否系统用户(重点) 给系统用户授权,用来帮助系统实现不同的用户展示不同的功能(重点) 针对密码等敏感信息,进行加密处理(明文变成密文)(重点) 提供了Session管理,但是它的Session不是HttpSession,是它自己自带的 做授权信息的缓存管理,降低对数据库的授权访问 提供测试支持,因为它也是一个轻量级框架,它也可以直接针对代码进行使用Junit单元测试 提供Remeber me的功能,可以做用户无需再次登录即可访问某些页面 Shiro提供的10大功能 #