Shiro框架

来源:互联网 发布:servlet源码包下载 编辑:程序博客网 时间:2024/06/10 14:26

   这周分配的任务主要和网络安全有点关系,对防止sql注入,如何走https协议有所了解,刚让我搞sql注入的时候,我看了下我们的权限框架是用的shiro,关于权限这个东西,之前接触的蛮多,但是没有了解过这方面的框架。然后就在网上找了点教程看了看,shiro结合spring实现权限的控制,主要就是用到了两个东西,一个是过滤器,一个是spring的动态代理,总的来说不是很难,东西有点碎,但是真的很强大,什么都想到了。

来看下结构图


shiro这个框架不仅仅可以对用户的权限进行控制,对其他语言的访问也可以控制,这个也叫做主体,subject,这个其他主体可以是c++、c,php编写的程序,如上图所示、

而shiro里面负责整个控制的东西叫SecurityManager,可以对用户进行Authenticator(认证,主要是判断用户输入的帐号和密码是否正确),Authorizer(授权,查看用户是否有哪些权限),SessionManager(会话管理,实现记住我功能呢),CacheManager(缓存功能,可以整合ehcache,将查询到的授权信息放入到缓存中),而授权和认证功能的实现正是基于Realm,这个其实有点类似于数据库的意味,在整合spring框架中,需要自定义一个realm,继承与AuthorizingRealm这个类,这个类中主要有两个方法需要重写。一个是doGetAuthenticationInfo(用于认证),一个是doGetAuthorizationInfo(用于授权),最后sessionDao可以将session直接存储与数据库中,最右边的

Cryptography即对密码管理器,可以对密码进行加密。散列多次等等操作。

1.用户认证和授权

public class CustomRealm extends AuthorizingRealm {//注入service@Autowiredprivate SysService sysService;// 设置realm的名称@Overridepublic void setName(String name) {super.setName("customRealm");}//realm的认证方法,从数据库查询用户信息@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {// token是用户输入的用户名和密码 // 第一步从token中取出用户名String userCode = (String) token.getPrincipal();// 第二步:根据用户输入的userCode从数据库查询SysUser sysUser = null;try {sysUser = sysService.findSysUserByUserCode(userCode);} catch (Exception e1) {// TODO Auto-generated catch blocke1.printStackTrace();}// 如果查询不到返回nullif(sysUser==null){//return null;}// 从数据库查询到密码String password = sysUser.getPassword();//盐String salt = sysUser.getSalt();// 如果查询到返回认证信息AuthenticationInfo//activeUser就是用户身份信息ActiveUser activeUser = new ActiveUser();activeUser.setUserid(sysUser.getId());activeUser.setUsercode(sysUser.getUsercode());activeUser.setUsername(sysUser.getUsername());//..//根据用户id取出菜单List<SysPermission> menus  = null;try {//通过service取出菜单 menus = sysService.findMenuListByUserId(sysUser.getId());} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}//将用户菜单 设置到activeUseractiveUser.setMenus(menus);//将activeUser设置simpleAuthenticationInfoSimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(activeUser, password,ByteSource.Util.bytes(salt), this.getName());return simpleAuthenticationInfo;}// 用于授权@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {//从 principals获取主身份信息//将getPrimaryPrincipal方法返回值转为真实身份类型(在上边的doGetAuthenticationInfo认证通过填充到SimpleAuthenticationInfo中身份类型),ActiveUser activeUser =  (ActiveUser) principals.getPrimaryPrincipal();//根据身份信息获取权限信息//从数据库获取到权限数据List<SysPermission> permissionList = null;try {permissionList = sysService.findPermissionListByUserId(activeUser.getUserid());} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}//单独定一个集合对象 List<String> permissions = new ArrayList<String>();if(permissionList!=null){for(SysPermission sysPermission:permissionList){//将数据库中的权限标签 符放入集合permissions.add(sysPermission.getPercode());}}//查到权限数据,返回授权信息(要包括 上边的permissions)SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();//将上边查询到授权信息填充到simpleAuthorizationInfo对象中simpleAuthorizationInfo.addStringPermissions(permissions);return simpleAuthorizationInfo;}
在control里面/login方法中只要得到realm返回的异常,给予前台合适的提示即可,如果密码和账户都正确即放行。

常见的异常有

UnknownAccountException账号不存在异常IncorrectCredentialsException密码错误会抛此异常DisabledAccountException帐号被禁用LockedAccountException帐号被锁定ExcessiveAttemptsException登录失败次数过多ExpiredCredentialsException凭证过期
2.退出,退出功能在shiro框架中已经自己实现,在配置文件中配置好相应的url,当用户点击次url,会自动清除session

3.会话管理,实现rememberme的功能,因为要将用户的信息写到磁盘上,所有用户的实体类需要继承Serializable接口,在spring。xml文件里面配置securityManager的地方配置好即可,有一点需要注意的是,在jsp页面中记住我按钮的名字要叫rememberme,这个也可以在FormAuthenticationFilter这个拦截器中指定相应的名字,这个拦截器主要用在当用户输入账户和密码后,未验证之前可进行一系列的判断,如判断验证码是否正确。

4.缓存的管理,ehcache可以和shiro进行很好的整合,在配置文件中配置好相应的东西即可。

0 0
原创粉丝点击