Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
guns-vip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chenjunxiong
guns-vip
Commits
3e6d7e6d
Commit
3e6d7e6d
authored
May 23, 2017
by
fsn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
shiro集成ehcache
parent
2f5298bc
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
33 deletions
+35
-33
src/main/java/com/stylefeng/guns/config/web/ShiroConfig.java
+25
-33
src/main/resources/ehcache.xml
+10
-0
No files found.
src/main/java/com/stylefeng/guns/config/web/ShiroConfig.java
View file @
3e6d7e6d
package
com
.
stylefeng
.
guns
.
config
.
web
;
import
com.stylefeng.guns.core.shiro.ShiroDbRealm
;
import
org.apache.shiro.cache.CacheManager
;
import
org.apache.shiro.cache.ehcache.EhCacheManager
;
import
org.apache.shiro.codec.Base64
;
import
org.apache.shiro.spring.LifecycleBeanPostProcessor
;
import
org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor
;
...
...
@@ -10,6 +12,7 @@ import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import
org.apache.shiro.web.servlet.SimpleCookie
;
import
org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator
;
import
org.springframework.beans.factory.config.MethodInvokingFactoryBean
;
import
org.springframework.cache.ehcache.EhCacheManagerFactoryBean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.DependsOn
;
...
...
@@ -28,25 +31,28 @@ public class ShiroConfig {
/**
* 安全管理器
*
* @author fengshuonan
*/
@Bean
public
DefaultWebSecurityManager
securityManager
(
CookieRememberMeManager
rememberMeManager
)
{
public
DefaultWebSecurityManager
securityManager
(
CookieRememberMeManager
rememberMeManager
,
CacheManager
cacheShiroManager
)
{
DefaultWebSecurityManager
securityManager
=
new
DefaultWebSecurityManager
();
//设置自定义Realm
securityManager
.
setRealm
(
this
.
shiroDbRealm
());
//记住密码管理
securityManager
.
setCacheManager
(
cacheShiroManager
);
securityManager
.
setRememberMeManager
(
rememberMeManager
);
return
securityManager
;
}
/**
* 缓存管理器 使用Ehcache实现
*/
@Bean
public
CacheManager
getCacheShiroManager
(
EhCacheManagerFactoryBean
ehcache
)
{
EhCacheManager
ehCacheManager
=
new
EhCacheManager
();
ehCacheManager
.
setCacheManager
(
ehcache
.
getObject
());
return
ehCacheManager
;
}
/**
* 项目自定义的Realm
*
* @author fengshuonan
*/
@Bean
public
ShiroDbRealm
shiroDbRealm
()
{
...
...
@@ -55,8 +61,6 @@ public class ShiroConfig {
/**
* rememberMe管理器, cipherKey生成见{@code Base64Test.java}
*
* @author fengshuonan
*/
@Bean
public
CookieRememberMeManager
rememberMeManager
(
SimpleCookie
rememberMeCookie
)
{
...
...
@@ -68,8 +72,6 @@ public class ShiroConfig {
/**
* 记住密码Cookie
*
* @author fengshuonan
*/
@Bean
public
SimpleCookie
rememberMeCookie
()
{
...
...
@@ -81,25 +83,23 @@ public class ShiroConfig {
/**
* Shiro的过滤器链
*
* @author fengshuonan
*/
@Bean
public
ShiroFilterFactoryBean
shiroFilter
(
DefaultWebSecurityManager
securityManager
)
{
ShiroFilterFactoryBean
shiroFilter
=
new
ShiroFilterFactoryBean
();
//安全管理器
shiroFilter
.
setSecurityManager
(
securityManager
);
//默认的登陆访问url
/**
* 默认的登陆访问url
*/
shiroFilter
.
setLoginUrl
(
"/login"
);
//登陆成功后跳转的url
/**
* 登陆成功后跳转的url
*/
shiroFilter
.
setSuccessUrl
(
"/"
);
//没有权限跳转的url
/**
* 没有权限跳转的url
*/
shiroFilter
.
setUnauthorizedUrl
(
"/global/error"
);
/**
* 配置shiro拦截器链
*
...
...
@@ -108,20 +108,17 @@ public class ShiroConfig {
* user 验证通过或RememberMe登录的都可以
*
*/
Map
<
String
,
String
>
hashMap
=
new
HashMap
<
String
,
String
>();
Map
<
String
,
String
>
hashMap
=
new
HashMap
<>();
hashMap
.
put
(
"/static/**"
,
"anon"
);
hashMap
.
put
(
"/login"
,
"anon"
);
hashMap
.
put
(
"/kaptcha"
,
"anon"
);
hashMap
.
put
(
"/**"
,
"user"
);
shiroFilter
.
setFilterChainDefinitionMap
(
hashMap
);
return
shiroFilter
;
}
/**
* 在方法中 注入 securityManager,进行代理控制
*
* @author fengshuonan
*/
@Bean
public
MethodInvokingFactoryBean
methodInvokingFactoryBean
(
DefaultWebSecurityManager
securityManager
)
{
...
...
@@ -133,19 +130,14 @@ public class ShiroConfig {
/**
* 保证实现了Shiro内部lifecycle函数的bean执行
*
* @author fengshuonan
*/
@Bean
public
LifecycleBeanPostProcessor
lifecycleBeanPostProcessor
()
{
return
new
LifecycleBeanPostProcessor
();
}
/**
* 启用shrio授权注解拦截方式,AOP式方法级权限检查
*
* @author fengshuonan
*/
@Bean
@DependsOn
(
value
=
"lifecycleBeanPostProcessor"
)
//依赖其他bean的初始化
...
...
src/main/resources/ehcache.xml
View file @
3e6d7e6d
...
...
@@ -6,6 +6,16 @@
<diskStore
path=
"java.io.tmpdir/ehcache"
/>
<defaultCache
maxElementsInMemory=
"50000"
eternal=
"false"
timeToIdleSeconds=
"3600"
timeToLiveSeconds=
"3600"
overflowToDisk=
"true"
diskPersistent=
"false"
diskExpiryThreadIntervalSeconds=
"120"
/>
<!-- 全局变量:永不过期-->
<cache
name=
"CONSTANT"
maxElementsInMemory=
"50000"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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