forked from grails-plugins/grails-spring-security-shiro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpringSecurityShiroGrailsPlugin.groovy
137 lines (111 loc) · 4.67 KB
/
SpringSecurityShiroGrailsPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Copyright 2013 SpringSource.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import grails.plugin.springsecurity.SecurityFilterPosition
import grails.plugin.springsecurity.SpringSecurityUtils
import grails.plugin.springsecurity.shiro.GormShiroPermissionResolver
import grails.plugin.springsecurity.shiro.ShiroLogoutHandler
import grails.plugin.springsecurity.shiro.ShiroSpringSecurityEventListener
import grails.plugin.springsecurity.shiro.ShiroSubjectBindingFilter
import grails.plugin.springsecurity.shiro.SpringSecurityRealm
import org.apache.shiro.cache.MemoryConstrainedCacheManager
import org.apache.shiro.spring.LifecycleBeanPostProcessor
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor
import org.apache.shiro.web.mgt.DefaultWebSecurityManager
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator
class SpringSecurityShiroGrailsPlugin {
String version = '1.0-RC1'
String grailsVersion = '2.0 > *'
List pluginExcludes = [
'docs/**',
'src/docs/**',
'grails-app/domain/**',
'grails-app/services/**'
]
List loadAfter = ['springSecurityCore']
String author = 'Burt Beckwith'
String authorEmail = '[email protected]'
String title = 'Shiro support for the Spring Security plugin'
String description = 'Shiro support for the Spring Security plugin'
String documentation = 'http://grails-plugins.github.io/grails-spring-security-shiro/'
String license = 'APACHE'
def organization = [name: 'SpringSource', url: 'http://www.springsource.org/']
def issueManagement = [system: 'Github', url: 'https://github.com/grails-plugins/grails-spring-security-shiro/issues']
def scm = [url: 'https://github.com/grails-plugins/grails-spring-security-shiro']
private Logger log = LoggerFactory.getLogger('grails.plugin.springsecurity.shiro.SpringSecurityShiroGrailsPlugin')
def doWithSpring = {
def conf = SpringSecurityUtils.securityConfig
if (!conf || !conf.active) {
return
}
SpringSecurityUtils.loadSecondaryConfig 'DefaultShiroSpringSecurityConfig'
// have to get again after overlaying DefaultShiroSpringSecurityConfig
conf = SpringSecurityUtils.securityConfig
if (!conf.shiro.active) {
return
}
boolean printStatusMessages = (conf.printStatusMessages instanceof Boolean) ? conf.printStatusMessages : true
if (printStatusMessages) {
println '\nConfiguring Spring Security Shiro ...'
}
SpringSecurityUtils.registerFilter 'shiroSubjectBindingFilter',
SecurityFilterPosition.SECURITY_CONTEXT_FILTER.order + 1
shiroLifecycleBeanPostProcessor(LifecycleBeanPostProcessor)
shiroAdvisorAutoProxyCreator(DefaultAdvisorAutoProxyCreator) { bean ->
bean.dependsOn = 'shiroLifecycleBeanPostProcessor'
proxyTargetClass = true
}
shiroAttributeSourceAdvisor(AuthorizationAttributeSourceAdvisor) {
securityManager = ref('shiroSecurityManager')
}
shiroPermissionResolver(GormShiroPermissionResolver) {
grailsApplication = ref('grailsApplication')
}
boolean useCache = conf.shiro.useCache // true
springSecurityRealm(SpringSecurityRealm) {
authenticationTrustResolver = ref('authenticationTrustResolver')
shiroPermissionResolver = ref('shiroPermissionResolver')
if (useCache) {
cacheManager = ref('shiroCacheManager')
}
}
if (useCache) {
shiroCacheManager(MemoryConstrainedCacheManager)
}
shiroSecurityManager(DefaultWebSecurityManager) { bean ->
realm = ref('springSecurityRealm')
if (useCache) {
cacheManager = ref('shiroCacheManager')
}
}
shiroSpringSecurityEventListener(ShiroSpringSecurityEventListener) {
realm = ref('springSecurityRealm')
securityManager = ref('shiroSecurityManager')
}
shiroSubjectBindingFilter(ShiroSubjectBindingFilter) {
authenticationTrustResolver = ref('authenticationTrustResolver')
realm = ref('springSecurityRealm')
securityManager = ref('shiroSecurityManager')
}
shiroLogoutHandler(ShiroLogoutHandler)
if (printStatusMessages) {
println '... finished configuring Spring Security Shiro\n'
}
}
def doWithApplicationContext = { ctx ->
ctx.logoutHandlers.add 0, ctx.shiroLogoutHandler // must be before SecurityContextLogoutHandler
}
}