Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
reg-app
Regapp
Commits
2de3bb62
Commit
2de3bb62
authored
Jul 23, 2015
by
michael.simon
Browse files
Cache roles in AuthorizationBean to speed up left-side-bar.xhtml
parent
9f3f7477
Changes
3
Hide whitespace changes
Inline
Side-by-side
bwreg-webapp/src/main/java/edu/kit/scc/webreg/sec/AuthorizationBean.java
View file @
2de3bb62
...
...
@@ -37,6 +37,7 @@ import edu.kit.scc.webreg.service.RegistryService;
import
edu.kit.scc.webreg.service.RoleService
;
import
edu.kit.scc.webreg.service.ServiceService
;
import
edu.kit.scc.webreg.service.UserService
;
import
edu.kit.scc.webreg.util.RoleCache
;
import
edu.kit.scc.webreg.util.SessionManager
;
@Named
(
"authorizationBean"
)
...
...
@@ -73,6 +74,9 @@ public class AuthorizationBean implements Serializable {
@Inject
private
ApplicationConfig
appConfig
;
@Inject
private
RoleCache
roleCache
;
@PostConstruct
private
void
init
()
{
if
(
sessionManager
.
getUserId
()
==
null
)
...
...
@@ -144,12 +148,12 @@ public class AuthorizationBean implements Serializable {
if
(
roleName
.
startsWith
(
"ROLE_"
))
roleName
=
roleName
.
substring
(
5
);
RoleEntity
role
=
role
Service
.
findByN
ame
(
roleName
);
Long
role
Id
=
role
Cache
.
getIdFromRolen
ame
(
roleName
);
if
(
role
==
null
)
if
(
role
Id
==
null
)
return
false
;
return
sessionManager
.
isUserInRole
(
role
.
getId
()
);
return
sessionManager
.
isUserInRole
(
role
Id
);
}
public
boolean
isUserInRole
(
RoleEntity
role
)
{
...
...
bwreg-webapp/src/main/java/edu/kit/scc/webreg/sec/Saml2PostHandlerServlet.java
View file @
2de3bb62
...
...
@@ -104,16 +104,19 @@ public class Saml2PostHandlerServlet {
String
persistentId
=
saml2AssertionService
.
extractPersistentId
(
assertion
,
spConfig
);
logger
.
debug
(
"Storing relevant SAML data in session"
);
session
.
setPersistentId
(
persistentId
);
Map
<
String
,
List
<
Object
>>
attributeMap
=
saml2AssertionService
.
extractAttributes
(
assertion
);
session
.
setAttributeMap
(
attributeMap
);
UserEntity
user
=
userService
.
findByPersistentWithRoles
(
spConfig
.
getEntityId
(),
idpEntity
.
getEntityId
(),
persistentId
);
if
(
user
==
null
)
{
logger
.
info
(
"New User detected, sending to register Page"
);
// Store SAML Data temporarily in Session
logger
.
debug
(
"Storing relevant SAML data in session"
);
session
.
setPersistentId
(
persistentId
);
session
.
setAttributeMap
(
attributeMap
);
// Role -1 is for new users
session
.
addRole
(-
1L
);
response
.
sendRedirect
(
"/register/register.xhtml"
);
...
...
bwreg-webapp/src/main/java/edu/kit/scc/webreg/util/RoleCache.java
0 → 100644
View file @
2de3bb62
package
edu.kit.scc.webreg.util
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.TimeUnit
;
import
javax.annotation.PostConstruct
;
import
javax.enterprise.context.ApplicationScoped
;
import
javax.inject.Inject
;
import
org.slf4j.Logger
;
import
com.google.common.cache.CacheBuilder
;
import
com.google.common.cache.CacheLoader
;
import
com.google.common.cache.LoadingCache
;
import
com.google.common.cache.RemovalListener
;
import
com.google.common.cache.RemovalNotification
;
import
edu.kit.scc.webreg.entity.RoleEntity
;
import
edu.kit.scc.webreg.service.RoleService
;
@ApplicationScoped
public
class
RoleCache
{
@Inject
private
Logger
logger
;
@Inject
private
RoleService
roleService
;
private
LoadingCache
<
String
,
Long
>
cache
;
@PostConstruct
public
void
init
()
{
logger
.
info
(
"Initializing roleCache"
);
cache
=
CacheBuilder
.
newBuilder
()
.
concurrencyLevel
(
4
)
.
maximumSize
(
1000
)
.
expireAfterWrite
(
1
,
TimeUnit
.
MINUTES
)
.
removalListener
(
removalListener
)
.
build
(
cacheLoader
);
}
public
Long
getIdFromRolename
(
String
roleName
)
{
try
{
return
cache
.
get
(
roleName
);
}
catch
(
ExecutionException
e
)
{
logger
.
info
(
"Execution Exception on cache"
,
e
);
return
null
;
}
}
private
CacheLoader
<
String
,
Long
>
cacheLoader
=
new
CacheLoader
<
String
,
Long
>()
{
public
Long
load
(
String
key
)
{
RoleEntity
role
=
roleService
.
findByName
(
key
);
if
(
role
!=
null
)
return
role
.
getId
();
return
null
;
}
};
private
RemovalListener
<
String
,
Long
>
removalListener
=
new
RemovalListener
<
String
,
Long
>()
{
public
void
onRemoval
(
RemovalNotification
<
String
,
Long
>
removal
)
{
if
(
logger
.
isTraceEnabled
())
logger
.
trace
(
"Removing entry {} -> {} from roleCache ({})"
,
removal
.
getKey
(),
removal
.
getValue
(),
removal
.
getCause
());
}
};
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment