目标
- 尽可能简单的使用Grails Shiro Plugin实现身份验证功能.
安装
- Grails版本:2.1.1
- 约定:在代码示例中,任何使用$开头,说明这是一个shell命令
1 $ grails create-app shiro-example
2 $ cd shiro-example
- shiro plugin最新的插件版本是1.1.4,可以使用grails install-plugin shiro安装,但是这里使用最新的快照版本。不需要用shell安装只需要在项目的BuildConfig.groovy中增加插件的引用即可
plugins {
runtime ":hibernate:$grailsVersion"
runtime ":jquery:1.8.3"
runtime ":resources:1.1.6"
runtime ":shiro:1.2.0-SNAPSHOT"
- 当我们修改了BuildConfig.groovy文件后,需要对系统进行重新编译,修改才能生效
- 现在插件已经安装好了,下一步建立一个初步的脚手架,执行这个shell后,系统会帮我们生成一系列的脚手架文件,这个--prefix=[包路径]是可选项,如果使用包路径,不要忘记最后面的那个"."
$ grails shiro-quick-start --prefix=com.example.
| Created file grails-app/domain/com/example/User.groovy
| Created file grails-app/domain/com/example/Role.groovy
| Created file grails-app/realms/com/example/DbRealm.groovy
| Created file grails-app/controllers/com/example/AuthController.groovy
| Created file grails-app/views/auth/login.gsp
| Created file grails-app/conf/com/example/SecurityFilters.groovy
配置
Bootstrap.groovy
1 import com.example.Role
2 import com.example.User
3
4 class BootStrap {
5
6 def shiroSecurityService
7
8 def init = { servletContext ->
9 // Create the admin role
10 def adminRole = Role.findByName('ROLE_ADMIN') ?:
11 new Role(name: 'ROLE_ADMIN').save(flush: true, failOnError: true)
12
13 // Create the user role
14 def userRole = Role.findByName('ROLE_USER') ?:
15 new Role(name: 'ROLE_USER').save(flush: true, failOnError: true)
16
17 // Create an admin user
18 def adminUser = User.findByUsername('admin') ?:
19 new User(username: "admin",
20 passwordHash: shiroSecurityService.encodePassword('password'))
21 .save(flush: true, failOnError: true)
22
23 // Add roles to the admin user
24 assert adminUser.addToRoles(adminRole)
25 .addToRoles(userRole)
26 .save(flush: true, failOnError: true)
27
28 // Create an standard user
29 def standardUser = User.findByUsername('joe') ?:
30 new User(username: "joe",
31 passwordHash: shiroSecurityService.encodePassword('password'))
32 .save(flush: true, failOnError: true)
33
34 // Add role to the standard user
35 assert standardUser.addToRoles(userRole)
36 .save(flush: true, failOnError: true)
37
38 }
39 def destroy = {
40 }
41 }
- 在代码第6行,引入了shiro的安全服务,并使用服务的encodePassword方法进行加密
- 在代码第9到15行,新增admin和user两个角色
- 在代码第17到21行,新增一个账号为admin的用户,并对password进行加密
- 在代码第23到26行,给admin用户进行角色授权
- 在代码第28到36行,给user用户进行角色授权
运行测试一下效果
$grails run-app
or
$grails -Dserver.port=8888 run-app
登录http://localhost:8080/shiro-example,点com.example.AuthController控制器,系统弹出登录窗口,输入账号和密码后,系统返回主界面,什么都没发生过。这是因为我们没有需要验证的界面,下面再增加一点东西
$ grails create-controller com.example.Home
1 package com.example
2
3 class HomeController {
4
5 def index() {
6 render "这个页面不需要验证"
7 }
8
9 def secured() {
10 render "这个页面需要user角色才能访问"
11 }
12
13 def admin() {
14 render "这个页面需要admin角色才能访问"
15 }
16 }
- 正如我们看到的,我可以分别登陆不同的视图,特定的角色才能返回正常的结果
- 如果权限不够的用户登录到了相应页面,系统提示“You do not have permission to access this page”
- 如果我们现在运行,并访问,不管登录到index、secured还是admin,系统都会提示“You do not have permission to access this page”
现在,分别给不同的action进行授权,打开SecurityFilters.groovy,拷贝如下代码
1 package com.example
2
3 /**
4 * Generated by the Shiro plugin. This filters class protects all URLs
5 * via access control by convention.
6 */
7 class SecurityFilters {
8
9 /**
10 * Array of controller/action combinations which will be skipped from authentication
11 * if the controller and action names match. The action value can also be '*' if it
12 * encompasses all actions within the controller.
13 */
14 static nonAuthenticatedActions = [
15 [controller: 'home', action: 'index']
16 ]
17
18 /**
19 * Array of controller/action combinations that will be authenticated against the user's
20 * role. The map also includes the roles which the controller/action pair will match
21 * against.
22 */
23 static authenticatedActions = [
24 [controller: 'home', action: 'secured', roles: ['ROLE_ADMIN', 'ROLE_USER']],
25 [controller: 'home', action: 'admin', roles: ['ROLE_ADMIN']]
26 ]
27
28 def filters = {
29
30 all(controller: '*', action: '*') {
31 before = {
32
33 // Determine if the controller/action belongs is not to be authenticated
34 def needsAuth = !nonAuthenticatedActions.find {
35 (it.controller == controllerName) &&
36 ((it.action == '*') || (it.action == actionName))
37 }
38
39 if (needsAuth) {
40
41 // Get the map within the authenticated actions which pertain to the current
42 // controller and view.
43 def authRoles = authenticatedActions.find {
44 (it.controller == controllerName) &&
45 ((it.action == '*') || (it.action == actionName))
46 }
47
48 if (authRoles) {
49
50 // Perform the access control for each of the roles provided in the authRoles
51 accessControl {
52 authRoles.roles.each { roleName ->
53 role(roleName)
54 }
55 }
56 }
57
58 // Skip authentication if the authRoles was not found
59 else {
60 return true
61 }
62 }
63
64 // Skip authentication if no auth is needed
65 else {
66 return true
67 }
68 }
69 }
70
71 }
72 }
- 代码第14到16行,登记不需要授权的action
- 代码第23到26行,登记需要授权的action,并指定相应的角色
现在,大功告成,再使用不同的角色,访问不同的页面,应该可以得到不同的效果
http://localhost:8080/shiro-example/home/index : non
http://localhost:8080/shiro-example/home/secured: user,admin
http://localhost:8080/shiro-example/home/admin : admin
分享到:
相关推荐
- **Authorizer**:一旦身份验证成功,Authorizer将根据用户的权限信息判断用户能否执行特定操作,即解答“你能做什么”的问题。 - **SessionManager**:负责管理会话,确保异构客户端之间的通信。该组件的设计基于...
资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。
《基于YOLOv8的智慧社区独居老人生命体征监测系统》(包含源码、可视化界面、完整数据集、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计
Android Studio Meerkat 2024.3.1 Patch 1(android-studio-2024.3.1.14-mac.dmg)适用于macOS Intel系统,文件使用360压缩软件分割成两个压缩包,必须一起下载使用: part1: https://download.csdn.net/download/weixin_43800734/90557060 part2: https://download.csdn.net/download/weixin_43800734/90557056
侧轴承杯加工工艺编制及夹具设计.zip
NASA数据集锂电池容量特征提取(Matlab完整源码和数据) 作者介绍:机器学习之心,博客专家认证,机器学习领域创作者,2023博客之星TOP50,主做机器学习和深度学习时序、回归、分类、聚类和降维等程序设计和案例分析,文章底部有博主联系方式。从事Matlab、Python算法仿真工作8年,更多仿真源码、数据集定制私信。
板料折弯机液压系统设计.zip
C6150车床的设计.zip
机器学习之KNN实现手写数字
python爬虫;智能切换策略,反爬检测机制
mpls-vpn-optionA-all
56tgyhujikolp[
GB 6442-86企业职工伤亡事故调查分析规则.pdf
汽车液压式主动悬架系统的设计().zip
2000-2024年各省专利侵权案件结案数数据 1、时间:2000-2024年 2、来源:国家知识产权J 3、指标:专利侵权案件结案数 4、范围:31省 5、用途:可用于衡量知识产权保护水平
资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。
内容概要:本文档详细复现了金融数学课程作业,涵盖欧式看涨期权定价和投资组合优化两大部分。对于欧式看涨期权定价,分别采用Black-Scholes模型和蒙特卡洛方法进行了计算,并对彩虹期权进行了基于最大值的看涨期权定价。投资组合优化部分则探讨了最小方差组合、给定收益的最小方差组合、最大效用组合以及给定风险的最大收益组合四种情形,还对比了拉格朗日乘数法和二次规划求解器两种方法。文中不仅提供了详细的MATLAB代码,还有详尽的中文解释,确保每一步骤清晰明了。 适合人群:金融工程专业学生、量化分析师、金融数学爱好者。 使用场景及目标:①帮助学生理解和掌握金融衍生品定价的基本原理和方法;②为从事量化分析的专业人士提供实用工具和技术支持;③作为教学材料辅助高校教师讲授相关内容。 其他说明:文档还包括了完整的论文结构建议,从封面页到结论,再到附录,涵盖了所有必要元素,确保提交的作业符合学术规范。此外,还特别强调了数据预处理步骤,确保代码可以顺利运行。
脉冲电解射流加工喷射装置设计(1)
ThinkPad S1 (2nd Generation) 和ThinkPad Yoga 260 用户指南V3.0,包含如何拆机更换硬件
charles描述文件下载