2 Star 1 Fork 1

Jonny / grails3-security

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Grails3.3.5集成spring security 3.2.1实现基于角色组的权限

创建项目,build.gradle中加入依赖,通过命令创建,通过此命令会在com.system下自动建表,共7张表

compile 'org.grails.plugins:spring-security-core:3.2.0.M1'
s2-quickstart com.system User Role RequestMap --groupClassName=RoleGroup

生成如下7张表
RequestMap
Role
RoleGroup
RoleGroupRole
User
UserRole
UserRoleGroup

权限交给RequestMap表管理,添加一个权限后需要清理缓存,或者禁用缓存

方法中注入
def springSecurityService

//清理了缓存
springSecurityService.clearCachedRequestmaps()

项目用户,角色,权限初始化通过service,在BootStrap.groovy的init方法中调用服务初始化

def requestMapService

def init {

    requestMapService.init()
}

若要允许允许通过get方式注销登录,则需在application.groovy中添加

grails.plugin.springsecurity.logout.postOnly = false

页面常用标签

//未登录
<sec:ifNotLoggedIn>
    登录
</sec:ifNotLoggedIn>

//已登录
<sec:ifLoggedIn>
    <sec:username/>
</sec:ifLoggedIn>

<!--同时匹配 -->
<sec:ifAllGranted roles="ROLE_ADMIN,ROLE_USER">
    ...
</sec:ifAllGranted>

<!--匹配任意一个 -->
<sec:ifAnyGranted roles='ROLE_ADMIN,ROLE_USER'>
    ...
</sec:ifAnyGranted>

<!--匹配非ROLE_USER角色 -->
<sec:ifNotGranted roles="ROLE_USER">
    ...
</sec:ifNotGranted>

<!--获取当前登录用户 -->
<sec:loggedInUserInfo field="username"/>

<!--匹配指定角色 -->
<sec:access expression="hasRole('ROLE_USER')">
    I'm a user.
</sec:access>

<!--匹配指定请求 -->
<sec:access url="/admin/user">
    The requestURL is "/admin/user"
</sec:access>

用户名密码有两个用户1:admin admin 用户2:test test,详情查看requestMapService服务类

新增功能说明

1.新增邮箱登录支持,支持邮箱或用户名登录
2.新增登录验证码,验证码是在后端验证的,验证码通过cookie保存,所以访问只能通过127.0.0.1进行访问

效果图

输入图片说明

用户名、邮箱登录实现

  • src/main/groovy下创建一个AuthFilter过滤器重写UsernamePasswordAuthenticationFilter类中的attemptAuthentication方法(主要用验证码验证)

  • grails-app/conf/spring/resources.groovy中添加配置

        authenticationProcessingFilter(com.system.AuthFilter) {
            def conf = SpringSecurityUtils.securityConfig
            authenticationManager = ref("authenticationManager")
            sessionAuthenticationStrategy = ref('sessionAuthenticationStrategy')
            authenticationSuccessHandler = ref('authenticationSuccessHandler')
            authenticationFailureHandler = ref('authenticationFailureHandler')
            rememberMeServices = ref('rememberMeServices')
            authenticationDetailsSource = ref('authenticationDetailsSource')
            filterProcessesUrl = conf.apf.filterProcessesUrl
            usernameParameter = conf.apf.usernameParameter
            passwordParameter = conf.apf.passwordParameter
            continueChainBeforeSuccessfulAuthentication = conf.apf.continueChainBeforeSuccessfulAuthentication
            allowSessionCreation = conf.apf.allowSessionCreation
            postOnly = conf.apf.postOnly
        }
        
        userDetailsService(com.system.CustomUserDetailsService){
            grailsApplication = ref('grailsApplication')
        }
  • 自定义CustomUserDetailsService服务类实现用户名、邮箱登录认证

druid

空文件

简介

Grails3.3.5集成spring security 3.2.1实现基于角色组的权限 展开 收起
Groovy
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Groovy
1
https://gitee.com/GntLee/grails3-security.git
git@gitee.com:GntLee/grails3-security.git
GntLee
grails3-security
grails3-security
master

搜索帮助