前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot应用-shiro简单权限管理

springboot应用-shiro简单权限管理

原创
作者头像
技术路漫漫
修改2020-07-02 09:56:55
7290
修改2020-07-02 09:56:55
举报

本文参考官方示例及相关实践,完整实现了springboot web应用集成shiro的简单权限管理

依赖引入

一方面需要引入shiro官方web依赖(特别说明,官方有两个starter,一个springboot,另一个springboot-web,此处我们要引入的是web starter)。同时,因为要基于thymeleaf进行html展示,还额外引入两个依赖:

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring-boot-web-starter</artifactId>
    <version>1.5.3</version>
</dependency>
<dependency>
    <groupId>com.github.theborakompanioni</groupId>
    <artifactId>thymeleaf-extras-shiro</artifactId>
    <version>2.0.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

后端程序编写

首先是基于shiro规范,实现相关配置类,完成Realm及ShiroFilterChainDefinition等的构建:

@Configuration
public class ShiroConfig {

    @Bean
    public Realm realm() {
        TextConfigurationRealm realm = new TextConfigurationRealm();
        realm.setUserDefinitions("zhangsan=hello,user\nlisi=good,admin");
        realm.setRoleDefinitions("user=read\nadmin=read,write");
        realm.setCachingEnabled(true);
        return realm;
    }

    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        chainDefinition.addPathDefinition("/login", "anon");
        chainDefinition.addPathDefinition("/doLogin", "anon");
        chainDefinition.addPathDefinition("/logout", "logout");
        chainDefinition.addPathDefinition("/**", "authc");
        return chainDefinition;
    }

    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }
}

接下来是编写示例控制器,实现对访问逻辑的处理:

@Controller
public class UserInfoController {

    @PostMapping("/doLogin")
    public String doLogin(String userName, String password, Model model) {
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
        } catch (AuthenticationException ex) {
            model.addAttribute("error", "用户名/密码错误,请重新输入");
            return "shiro/login";
        }
        return "redirect:/index";
    }

    @RequiresRoles("admin")
    @GetMapping("/admin")
    public String admin() {
        return "shiro/admin";
    }

    @RequiresRoles(value = {"admin", "user"}, logical = Logical.OR)
    @GetMapping("/user")
    public String user() {
        return "shiro/user";
    }
}

对于无需进行特别权限控制的,通过webconfig来实现对请求和视图的注册:

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("shiro/login");
        registry.addViewController("/index").setViewName("shiro/index");
        registry.addViewController("/except").setViewName("shiro/except");
    }
}

最后,对认证异常进行统一处理,并将信息返回到前端视图:

@ControllerAdvice
public class SimpleExceptionHandler {

    @ExceptionHandler(AuthorizationException.class)
    public ModelAndView error(AuthorizationException ex) {
        ModelAndView mv = new ModelAndView("shiro/except");
        mv.addObject("error", ex.getMessage());
        return mv;
    }
}

前端HTML编写

在resources的templates下,新建shiro文件夹,放置相关html文件。

首先,先实现login页面:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>登录页</title>
</head>
<body>
<p>样例账号:zhangsan=hello,user;lisi=good,admin</p>
<div>
    <form action="/doLogin" method="POST">
        <div th:text="${error}"></div>
        <input placeholder="用户名" name="userName" type="text"/>
        <input placeholder="密码" name="password" type="password"/>
        <input type="submit" value="登录"/>
    </form>
</div>
</body>
</html>

登陆成功后,接下来是进入index页面,展示用户基本信息:

<!DOCTYPE HTML>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<div>
    <h1>hello,
        <shiro:principal/>
    </h1>
    <h3><a href="/logout">退出登录</a></h3>
    <h3><a shiro:hasRole="admin" href="/admin">管理员页面</a></h3>
    <h3><a shiro:hasAnyRoles="admin,user" href="/user">用户详情页</a></h3>
</div>
</body>
</html>

接下来,为了验证admin和user两类权限,分别编写admin和user页面,admin.html具体如下:

<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>管理员</title>
</head>
<body>
<h1>你好,管理员:
    <shiro:principal/>
</h1>
<h3><a href="/logout">退出登录</a></h3>
</body>
</html>

user.html页面类似,只是提示信息稍微不一样:

<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>用户详情</title>
</head>
<body>
<h1>你好,用户:
    <shiro:principal/>
</h1>
<h3><a href="/logout">退出登录</a></h3>
</body>
</html>

最后,简单实现未授权异常except页面:

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>异常页</title>
</head>
<body>
<div>
    <h1>未授权异常</h1>
    <h2 th:text="${error}"></h2>
</div>
<a href="/login">登录页</a>
</body>
</html>

配置文件编写

完成前后端程序编写后,最后shiro相关配置项的配置:

shiro:
  loginUrl: /login
  successUrl: /index
  unauthorizedUrl: /except
spring:
  thymeleaf:
    cache: false

鉴权效果验证

接下来是启动主程序后,进行具体效果验证

先访问 http://localhost:8080/,此时会shiro会自动重定向到login页面,url会类似http://localhost:8080/login;jsessionid=6C5C69352FFFCFC2ADFA816A091C434B

login
login

登录页面中,先以错误的账号或密码登录,会在当前页面中提示相关错误信息:

login-error
login-error

以正确的user账号zhangsan/hello登录后,会进入index页面,展示基本信息:

userindex
userindex

点击退出登录后,会重新回到login页面,再以admin账号lisi/good登录后,index页面会额外多展示“管理员页面“链:

adminindex
adminindex

再次退出登录后,再以user账号登陆后,直接在地址栏访问http://localhost:8080/admin,会进入到授权异常页面:

except
except

至此,基于shiro的简单授权管理已经完成,相关源代码详见https://gitee.com/coolpine/backends/tree/master/hiboot/src/main/java/pers/techlmm/shiro/base,供参考

参考资料

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 依赖引入
  • 后端程序编写
  • 前端HTML编写
  • 配置文件编写
  • 鉴权效果验证
  • 参考资料
相关产品与服务
访问管理
访问管理(Cloud Access Management,CAM)可以帮助您安全、便捷地管理对腾讯云服务和资源的访问。您可以使用CAM创建子用户、用户组和角色,并通过策略控制其访问范围。CAM支持用户和角色SSO能力,您可以根据具体管理场景针对性设置企业内用户和腾讯云的互通能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档