栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

权限管理系统之集成Shiro实现登录、url和页面按钮的访问控制

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

权限管理系统之集成Shiro实现登录、url和页面按钮的访问控制

用户权限管理一般是对用户页面、按钮的访问权限管理。Shiro框架是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理,对于Shiro的介绍这里就不多说。本篇博客主要是了解Shiro的基础使用方法,在权限管理系统中集成Shiro实现登录、url和页面按钮的访问控制。

一、引入依赖

使用SpringBoot集成Shiro时,在pom.xml中可以引入shiro-spring-boot-web-starter。由于使用的是thymeleaf框架,thymeleaf与Shiro结合需要 引入thymeleaf-extras-shiro。

        
        
            org.apache.shiro
            shiro-spring-boot-web-starter
            1.4.0
        
        
        
            com.github.theborakompanioni
            thymeleaf-extras-shiro
            2.0.0
        

二、增加Shiro配置

有哪些url是需要拦截的,哪些是不需要拦截的,登录页面、登录成功页面的url、自定义的Realm等这些信息需要设置到Shiro中,所以创建Configuration文件ShiroConfig。

package com.example.config;import org.apache.shiro.mgt.SecurityManager;import org.apache.shiro.spring.web.ShiroFilterFactoryBean;import org.apache.shiro.web.mgt.DefaultWebSecurityManager;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;import java.util.linkedHashMap;import java.util.Map;


@Configurationpublic class ShiroConfig {
    @Bean("shiroFilterFactoryBean")    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        System.out.println("ShiroConfiguration.shirFilter()");
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);        //拦截器.
        Map filterChainDefinitionMap = new linkedHashMap();        // 配置不会被拦截的链接 顺序判断
        filterChainDefinitionMap.put("/static
    @Override    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)            throws AuthenticationException {
        System.out.println("MyShiroRealm.doGetAuthenticationInfo()");        //获取用户的输入的账号.
        String username = (String)token.getPrincipal();
        System.out.println(token.getCredentials());        //通过username从数据库中查找 User对象,如果找到,没找到.        //实际项目中,这里可以根据实际情况做缓存,如果不做,Shiro自己也是有时间间隔机制,2分钟内不会重复执行该方法
        User user = userService.getUserById(username);
        System.out.println("----->>userInfo="+user);        if(user == null){            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                user, //用户名
                "123456", //密码
                getName()  //realm name        );        return authenticationInfo;
    }

}

View Code

四、登录认证

1.登录页面

这里做了一个非常丑的登录页面,主要是自己懒,不想在网上复制粘贴找登录页面了。


    
    
    
    
    
    
    
    

View Code

2.处理登录请求

在LoginController中通过登录名、密码获取到token实现登录。

package com.example.controller;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.IncorrectCredentialsException;import org.apache.shiro.authc.UnknownAccountException;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.subject.Subject;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;

@Controllerpublic class LoginController {    //退出的时候是get请求,主要是用于退出
    @RequestMapping(value = "/login",method = RequestMethod.GET)    public String login(){        return "login";
    }    //post登录
    @RequestMapping(value = "/login",method = RequestMethod.POST)    public String login(Model model,String id,String pwd){        //添加用户认证信息
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
                id,                "123456");        try {            
                subject.login(usernamePasswordToken);            
                return "home";   
            }        catch (UnknownAccountException e) {            
            //用户名不存在            
            model.addAttribute("msg","用户名不存在");            
            return "login";        
            }catch (IncorrectCredentialsException e) {            
                //密码错误            
                model.addAttribute("msg","密码错误");            
                return "login";        
                }
        
    }
    @RequestMapping(value = "/index")    public String index(){        return "home";
    }

}

View Code

五、Controller层访问控制

1.首先来数据库的数据,两张图是用户角色、和角色权限的数据。

2.设置权限

这里在用户页面点击编辑按钮时设置需要有id=002的角色,在点击选择角色按钮时需要有code=002的权限。

    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    @RequiresRoles("002")//权限管理;
    public String editGet(Model model,@RequestParam(value="id") String id) {
        model.addAttribute("id", id);        return "/user/edit";
    }

    @RequestMapping(value = "/selrole",method = RequestMethod.GET)
    @RequiresPermissions("002")//权限管理;
    public String selctRole(Model model,@RequestParam("id") String id,@RequestParam("type") Integer type) {
        model.addAttribute("id",id);
        model.addAttribute("type", type);        return "/user/selrole";
    }

 当使用用户001登录时,点击编辑,弹出框如下,提示没有002的角色

点击选择角色按钮时提示没有002的权限。

当使用用户002登录时,点击编辑按钮,显示正常,点击选择角色也是提示没002的权限,因为权限只有001。

六、前端页面层访问控制

有时为了不想像上面那样弹出错误页面,需要在按钮显示上进行不可见,这样用户也不会点击到。前面已经引入了依赖并配置了bean,这里测试下在html中使用shiro。

1.首先设置html标签引入shiro

2.控制按钮可见

这里使用shiro:hasAnyRoles="002,003"判断用户角色是否是002或003,是则显示不是则不显示。

    
        添加用户
    
    
        批量删除
    

当001用户登录时,添加用户、批量删除按钮都不显示,只显示查询按钮。

当002用户登录时,添加用户、批量删除按钮都显示

七、小结

这里只是实现了Shiro的简单的功能,Shiro还有很多很强大的功能,比如session管理等,而且目前权限管理模块还有很多需要优化的功能,左侧导航栏的动态加载和权限控制、Shiro与Redis结合实现session共享、Shiro与Cas结合实现单点登录等。后续可以把项目做为开源项目,慢慢完善集成更多模块例如:Swagger2、Redis、Druid、RabbitMQ等供初学者参考。

---------------我是有底线的--------------------
作者:社会主义接班人
出处:http://www.cnblogs.com/5ishare/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/238433.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号