博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shiro整合SpringMVC
阅读量:4476 次
发布时间:2019-06-08

本文共 5578 字,大约阅读时间需要 18 分钟。

1.取消原springmvc认证和授权拦截器

去掉springmvc.xml中配置的LoginInterceptor和PermissionInterceptor拦截器。

2.web.xml添加shiro Filter

 

shiroFilter
org.springframework.web.filter.DelegatingFilterProxy
targetFilterLifecycle
true
targetBeanName
shiroFilter
shiroFilter
/*
View Code

 

3.applicationContext-shiro.xml

/logout.action = logout
/refuse.jsp = anon
/item/list.action = roles[item],authc /js/** anon /images/** anon /styles/** anon /validatecode.jsp anon /item/* authc
/** = authc
View Code

securityManager:这个属性是必须的。

loginUrl:没有登录认证的用户请求将跳转到此地址进行认证,不是必须的属性,不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面。

unauthorizedUrl:没有权限默认跳转的页面。

4.自定义realm

此realm先不从数据库查询权限数据,当前需要先将shiro整合完成,在上边章节定义的realm基础上修改。

 

public class CustomRealm1 extends AuthorizingRealm {    @Override    public String getName() {        return "customRealm";    }    // 支持什么类型的token    @Override    public boolean supports(AuthenticationToken token) {        return token instanceof UsernamePasswordToken;    }    // 认证    @Override    protected AuthenticationInfo doGetAuthenticationInfo(            AuthenticationToken token) throws AuthenticationException {        // 从token中 获取用户身份信息        String username = (String) token.getPrincipal();        // 拿username从数据库中查询        // ....        // 如果查询不到则返回null        if (!username.equals("zhang")) {
// 这里模拟查询不到 return null; } // 获取从数据库查询出来的用户密码 String password = "123";// 这里使用静态数据模拟。。 // 根据用户id从数据库取出菜单 //...先用静态数据 List
menus = new ArrayList
();; SysPermission sysPermission_1 = new SysPermission(); sysPermission_1.setName("商品管理"); sysPermission_1.setUrl("/item/queryItem.action"); SysPermission sysPermission_2 = new SysPermission(); sysPermission_2.setName("用户管理"); sysPermission_2.setUrl("/user/query.action"); menus.add(sysPermission_1); menus.add(sysPermission_2); // 构建用户身份信息 ActiveUser activeUser = new ActiveUser(); activeUser.setUserid(username); activeUser.setUsername(username); activeUser.setUsercode(username); activeUser.setMenus(menus); // 返回认证信息由父类AuthenticatingRealm进行认证 SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo( activeUser, password, getName()); return simpleAuthenticationInfo; } // 授权 @Override protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals) { // 获取身份信息 ActiveUser activeUser = (ActiveUser) principals.getPrimaryPrincipal(); //用户id String userid = activeUser.getUserid(); // 根据用户id从数据库中查询权限数据 // ....这里使用静态数据模拟 List
permissions = new ArrayList
(); permissions.add("item:query"); permissions.add("item:update"); // 将权限信息封闭为AuthorizationInfo SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); for (String permission : permissions) { simpleAuthorizationInfo.addStringPermission(permission); } return simpleAuthorizationInfo; }}
View Code

 

5.登录

// 用户登陆提交    @RequestMapping("/login")    public String loginsubmit(Model model, HttpServletRequest request)            throws Exception {        // shiro在认证过程中出现错误后将异常类路径通过request返回        String exceptionClassName = (String) request                .getAttribute("shiroLoginFailure");        if(exceptionClassName!=null){            if (UnknownAccountException.class.getName().equals(exceptionClassName)) {                throw new CustomException("账号不存在");            } else if (IncorrectCredentialsException.class.getName().equals(                    exceptionClassName)) {                throw new CustomException("用户名/密码错误");            } else if("randomCodeError".equals(exceptionClassName)){                throw new CustomException("验证码错误");            } else{                throw new Exception();//最终在异常处理器生成未知错误            }        }        return "login";            }
View Code

6.首页

由于session由shiro管理,需要修改首页的controller方法,将session中的数据通过model传到页面。

//系统首页    @RequestMapping("/first")    public String first(Model model)throws Exception{                //主体        Subject subject = SecurityUtils.getSubject();        //身份        ActiveUser activeUser = (ActiveUser) subject.getPrincipal();        model.addAttribute("activeUser", activeUser);        return "/first";    }
View Code

7.退出

由于使用shiro的sessionManager,不用开发退出功能,使用shiro的logout拦截器即可。

/logout.action = logout

8.无权限refuse.jsp

当用户无操作权限,shiro将跳转到refuse.jsp页面。

 

转载于:https://www.cnblogs.com/yanmingyuan/p/10585017.html

你可能感兴趣的文章
可选择关卡的贪吃蛇游戏
查看>>
云计算之路-迁入阿里云后:20130314云服务器故障经过
查看>>
徐志摩:决断
查看>>
ubuntu系统之难
查看>>
Sql从邮件中提取国家代码
查看>>
mysql基本用法
查看>>
Cocos2D界面切换方式
查看>>
CocoaPods-- install & update 急速引入、更新
查看>>
求π(派)的近似值
查看>>
js 中关于this用变量存起来的原因
查看>>
再编写代码中报错:CS8107 C# 7.0 中不支持功能“xxxxxx”。请使用 7.1 或更高的语言版本。...
查看>>
Python 枚举 Enum
查看>>
spring常用jar包下载
查看>>
场景测试
查看>>
sharepint开发过程的一些性能总结
查看>>
win7 vs2010 局域网的bug联机调试
查看>>
Python 探索性数据分析(Exploratory Data Analysis,EDA)
查看>>
NOIP前夕:noi.openjudge,带通配符的字符串匹配
查看>>
yum和apt-get的区别
查看>>
hds学院
查看>>