代码拉取完成,页面将自动刷新
创建项目,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
服务类实现用户名、邮箱登录认证
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。