grails-shiro权限认证
一.引用shiro插件
//在BuildConfig的plugins下面添加
compile ":shiro:1.2.1"
二.引用新插件后要进行编译
//grails命令
compile
三.生成脚手架文件
//grials命令 , 要注意的是后面的那个点,否则生成好的文件会混乱
shiro-quick-start --prefix=com.security.
四.配置Bootstrap.groovy
class BootStrap {
def shiroSecurityService
def init = { servletContext ->
// Create the admin role
def adminRole = Role.findByName('ROLE_ADMIN') ?:
new Role(name: 'ROLE_ADMIN').save(flush: true, failOnError: true)
// Create the user role
def userRole = Role.findByName('ROLE_USER') ?:
new Role(name: 'ROLE_USER').save(flush: true, failOnError: true)
// Create an admin user
def adminUser = User.findByUsername('admin') ?:
new User(username: "admin",
passwordHash: shiroSecurityService.encodePassword('password'))
.save(flush: true, failOnError: true)
// Add roles to the admin user
assert adminUser.addToRoles(adminRole)
.addToRoles(userRole)
.save(flush: true, failOnError: true)
// Create an standard user
def standardUser = User.findByUsername('joe') ?:
new User(username: "joe",
passwordHash: shiroSecurityService.encodePassword('password'))
.save(flush: true, failOnError: true)
// Add role to the standard user
assert standardUser.addToRoles(userRole)
.save(flush: true, failOnError: true)
}
def destroy = {
}
}
五.增加一个Controller
package com.security
class HomeController {
def index() {
render ("此页面不需要登陆")
}
def secured() {
render ("此页面需要用户或者管理员登陆")
}
def admin() {
render ("此页面需要管理员登陆")
}
}
六.修改com.security.SecurityFilters.groovy
package com.security /**
* Generated by the Shiro plugin. This filters class protects all URLs
* via access control by convention.
*/
class SecurityFilters {
def filters = {
//1.role_admin
home(controller: "home",action: "admin"){
before = {
accessControl{
role("ROLE_ADMIN");
}
}
}
//2.role_user
home_securied(controller: "home",action: "secured"){
before = {
accessControl{
role("ROLE_USER");
}
}
}
}
}
这里使用的是shiroPlugin提供的accessControl,role方法会划横线,这里是不影响程序运行的,
使用 role( …… ),验证访问对象是否具有相应的角色;
使用 permission( …… ),验证访问对象是否具有相应的 Permission。
这里没有使用shiro的Tag但是也做一点称述
下是经常使用到的 Tag:
- principal,输出当前用户的标识
- hasRole,判断当前用户是否属于给定的角色,参数:name
- hasPermission, 判断当前用户是否具有指定的权限,参数:type,action 或者 permission
- isLoggedIn,判断当前用户是否已经登录
- hasAnyRole,判断当前用户是否属于给定的某个角色,参数:in
使用方式
<shiro:hasPermission permission="home:index,admin">
<span class="button">
<g:actionSubmit class="edit" value="Edit" />
</span>
<span class="button">
<g:actionSubmit class="delete"
onclick="return confirm('Are you sure?');"
value="Delete" />
</span>
</shiro:hasPermission>
grails-shiro权限认证的更多相关文章
- Shiro入门之一 -------- Shiro权限认证与授权
一 将Shirojar包导入web项目 二 在web.xml中配置shiro代理过滤器 注意: 该过滤器需要配置在struts2过滤器之前 <!-- 配置Shiro的代理过滤器 --> ...
- 学习Spring Boot:(十三)配置 Shiro 权限认证
经过前面学习 Apache Shiro ,现在结合 Spring Boot 使用在项目里,进行相关配置. 正文 添加依赖 在 pom.xml 文件中添加 shiro-spring 的依赖: <d ...
- shiro权限认证与授权
什么是shiro? Shiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证,权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架. 为什么要用sh ...
- shiro权限认证Realm的四大用法
一.SimpleAccountRealm public class AuthenticationTest { SimpleAccountRealm sar=new SimpleAcc ...
- springboot+mybatis+shiro——登录认证和权限控制
转载:https://z77z.oschina.io/ 一.引入依赖 shiro-all包含shiro所有的包.shiro-core是核心包.shiro-web是与web整合.shiro-spring ...
- spring-boot整合shiro作权限认证
spring-shiro属于轻量级权限框架,即使spring-security更新换代,市场上大多数企业还是选择shiro 废话不多说 引入pom文件 <!--shiro集成spring--& ...
- 十、 Spring Boot Shiro 权限管理
使用Shiro之前用在spring MVC中,是通过XML文件进行配置. 将Shiro应用到Spring Boot中,本地已经完成了SpringBoot使用Shiro的实例,将配置方法共享一下. 先简 ...
- Spring Boot Shiro 权限管理 【转】
http://blog.csdn.net/catoop/article/details/50520958 主要用于备忘 本来是打算接着写关于数据库方面,集成MyBatis的,刚好赶上朋友问到Shiro ...
- 4.SSM配置shiro权限管理
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.搭建SSM项目: http://www.cnblogs.com/yysbolg/p/6909021.html ...
- Spring Boot Shiro 权限管理
Spring Boot Shiro 权限管理 标签: springshiro 2016-01-14 23:44 94587人阅读 评论(60) 收藏 举报 .embody{ padding:10px ...
随机推荐
- paper 47 :Latex中文显示(转)
中文支持需要cjk-latex,总得来说中文可以使用GB和GBK两种字体,GBK需要从windows下copy *.ttc或*.ttf, GB字体则在linux下就用. 先说支持GB的中文显示,安装以 ...
- notepad++ 行末尾添加指定字符
在查找目标中输入“^”代表行首,“$”代表行末,下方的查找模式要改成“正则表达式”. 如果替换中有字符,则用“\”转义, 例如 : 目标中输入: $ 替换字符输入: \, 则是每行后面加 ...
- 夺命雷公狗—angularjs—6—单条数据的遍历
我们在实际的工作中常常会处理到一些数据的遍历,这些数据都是后端传到前端的,有些公司会让前端帮忙处理一点遍历的工作,废话不多说,直接上代: <!doctype html> <html ...
- 前端单页应用SEO解决方案
在这里只会提到Google的解决方案,日后再补充百度的解决方案 我们经常使用的单页都是#!来做应用的前端路由,因为这个在多个版本浏览器上有很好的兼容性 当Google发现URL里有#!符号,Googl ...
- [Ubuntu] 转载-使用Ubuntu修复grub
原文地址:http://fav.coketea.com/article_show.php?id=1 步骤一.以试用方式进入ubuntu光盘系统,打开终端(快捷键ctrl+alt+t): 步骤二.获取r ...
- ionic入门之AngularJS扩展(一)
ionic ionic是一个强大的混合式/hybridHTML5移动开发框架,特点是使用标准的HTML. CSS和JavaScript,开发跨平台(目前支持:Android.iOS,计划支持:Wind ...
- 解决Android SDK Manager更新下载速度慢
1.在Host文件加上 203.208.46.146 www.google.com 74.125.113.121 developer.android.com 203.208.46.146 dl.goo ...
- 【python cookbook】【数据结构与算法】13.通过公共键对字典列表排序
问题:想根据一个或多个字典中的值来对列表排序 解决方案:利用operator模块中的itemgetter()函数对这类结构进行排序是非常简单的. # Sort a list of a dicts on ...
- split function of Perl,Python,Awk
使用中常用到Perl,Python,AWK,R, 虽然Java,C,C++,Vala也学过但是就是不喜欢,你说怎么办. 看来一辈子脚本的命. Perl @rray = split /PATTERN/, ...
- Spring事务注解@Transactional回滚问题
Spring配置文件,声明事务时,如果rollback-for属性没有指定异常或者默认不写:经测试事务只回滚运行时异常(RuntimeException)和错误(Error). <!-- 配置事 ...