1、最近在做一個貸款項目是城市分站的 分爲貸款前臺和貸款機構後臺,這個兩個平臺的二級域名不一樣,一個前臺是cityname.xx.com,cityname是會地區的不同而變化的,如在 北京就是bj.xx.com,機構後臺是loan.xx.com,在機構登錄的時候 ,如果把登錄信息放在session,會有一個問題,就是當切換到前臺的時候,由於域名改變了,此時session就會改變,之前session保存的信 息就不存在了,也就是session跨域問題,最後想到了使用緩存才存儲在線用戶信息,這樣就不存在session跨域的問題。
2、ehCache介紹
EhCache 是一個純Java的進程內緩存框架,具有快速、精幹等特點,是Hibernate中默認的CacheProvider。
下圖是 Ehcache 在應用程序中的位置:
主要的特性有:
1. 快速.
2. 簡單.
3. 多種緩存策略
4. 緩存數據有兩級:內存和磁盤,因此無需擔心容量問題
5. 緩存數據會在虛擬機重啓的過程中寫入磁盤
6. 可以通過RMI、可插入API等方式進行分佈式緩存
7. 具有緩存和緩存管理器的偵聽接口
8. 支持多緩存管理器實例,以及一個實例的多個緩存區域
9. 提供Hibernate的緩存實現
下面說ehcache的使用
①下載ehcache.jar,自己去google下載地址
②隨後,開始配置ehCache的屬性,ehCache需要一個xml文件來設置ehCache相關的一些屬性,如最大緩存數量、cache刷新的時間等等
ehcache.xml放在你的classpath下.
05 |
maxElementsInMemory = "1000" |
07 |
timeToIdleSeconds = "120" |
08 |
timeToLiveSeconds = "120" |
09 |
overflowToDisk = "false" |
11 |
< cache
name = "DEFAULT_CACHE" |
12 |
maxElementsInMemory = "10000" |
14 |
timeToIdleSeconds = "300000" |
15 |
timeToLiveSeconds = "600000" |
16 |
overflowToDisk = "false" |
③用spring3攔截器檢查緩存中是否有用戶信息
01 |
package
com.woaika.loan.front.common.filter; |
03 |
import
javax.annotation.Resource; |
04 |
import
javax.servlet.http.HttpServletRequest; |
05 |
import
javax.servlet.http.HttpServletResponse; |
06 |
import
javax.servlet.http.HttpSession; |
08 |
import
net.sf.ehcache.Cache; |
09 |
import
net.sf.ehcache.Element; |
11 |
import
org.apache.log4j.Logger; |
12 |
import
org.springframework.web.servlet.HandlerInterceptor; |
13 |
import
org.springframework.web.servlet.ModelAndView; |
15 |
import
com.woaika.loan.front.loanuser.vo.LoginOrganVo; |
16 |
import
com.woaika.loan.ip.IPUtil; |
17 |
import
com.woaika.loan.po.LoanOrgan; |
19 |
public
class OrgMgtInterceptor implements
HandlerInterceptor { |
21 |
private
Logger log = Logger.getLogger(OrgMgtInterceptor. class );
|
23 |
private
Cache ehCache; |
25 |
@Resource (name= "ehCache" )
|
26 |
public
void setEhCache(Cache ehCache) {
|
27 |
this .ehCache = ehCache;
|
30 |
public
void afterCompletion(HttpServletRequest arg0,
|
31 |
HttpServletResponse arg1, Object arg2, Exception arg3)
|
38 |
public
void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
|
39 |
Object arg2, ModelAndView arg3)
throws Exception {
|
45 |
public
boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
46 |
Object handler)
throws Exception {
|
49 |
String ip = IPUtil.getRemortIP(request);
|
50 |
Element elementIp = ehCache.get(ip);
|
52 |
response.sendRedirect(com.woaika.loan.commons.constants.SiteConstant.JIGOU_URL+ "/tologin" );
|
55 |
LoginOrganVo vo =(LoginOrganVo)elementIp.getObjectValue();
|
57 |
Element elementId = ehCache.get(vo.getId().toString());
|
59 |
String tempIp = (String)elementId.getObjectValue();
|
60 |
if (tempIp!= null
&& ! "" .equals(tempIp) && ip.equals(tempIp)){
|
61 |
request.setAttribute( "currentOrgan" , vo);
|
66 |
response.sendRedirect(com.woaika.loan.commons.constants.SiteConstant.JIGOU_URL+ "/tologin" );
|
④將ehcache進行注入的配置applicationContext-ehCache.xml
01 |
< beans
xmlns = "http://www.springframework.org/schema/beans" |
02 |
xmlns:context = "http://www.springframework.org/schema/context" |
03 |
xmlns:p = "http://www.springframework.org/schema/p" |
04 |
xmlns:mvc = "http://www.springframework.org/schema/mvc" |
05 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
06 |
xmlns:aop = "http://www.springframework.org/schema/aop" |
07 |
xmlns:tx = "http://www.springframework.org/schema/tx" |
08 |
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
09 |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
10 |
http://www.springframework.org/schema/context
|
11 |
http://www.springframework.org/schema/context/spring-context.xsd
|
12 |
http://www.springframework.org/schema/tx
|
13 |
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
|
14 |
http://www.springframework.org/schema/aop
|
15 |
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
|
16 |
http://www.springframework.org/schema/mvc
|
17 |
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
|
19 |
< bean
id = "defaultCacheManager"
class = "org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
|
20 |
< property
name = "configLocation" >
|
21 |
< value >classpath:ehcache.xml</ value >
|
26 |
< bean
id = "ehCache"
class = "org.springframework.cache.ehcache.EhCacheFactoryBean" >
|
27 |
< property
name = "cacheManager" >
|
28 |
< ref
local = "defaultCacheManager" />
|
30 |
< property
name = "cacheName" >
|
31 |
< value >DEFAULT_CACHE</ value >
|
⑤登錄的時候,驗證用戶名密碼,正確放入緩存,這裏只是提供一個簡單的方法(用的spring的mvc)
01 |
@RequestMapping (value= "/login" )
|
02 |
public
String login(String organname, String pwd, HttpServletRequest request, HttpServletResponse response){
|
03 |
judgmentCity.getCityInfo(request);
|
04 |
LoanOrgan organ = loanOrganService.login(organname, pwd);
|
06 |
request.setAttribute( "msg" ,
"用戶名或密碼錯誤" );
|
07 |
return
"forward:/jigou/tologin" ;
|
10 |
String ip = IPUtil.getRemortIP(request);
|
11 |
LoginOrganVo vo =
new LoginOrganVo();
|
12 |
vo.setId(organ.getId());
|
13 |
vo.setOrganname(organ.getOrganname());
|
14 |
Element elementip =
new Element(ip, (Serializable) vo);
|
15 |
Element elementid =
new Element(organ.getId().toString(), ip);
|
17 |
ehCache.put(elementip); |
18 |
ehCache.put(elementid);
|
21 |
return
"redirect:/organmgt/index" ;
|
註銷操作(用的spring的mvc):
02 |
@RequestMapping (value= "/logout" )
|
03 |
public
String logout(HttpServletRequest request){ |
04 |
String ip = IPUtil.getRemortIP(request);
|
05 |
Element elementIp = ehCache.get(ip);
|
07 |
LoginOrganVo vo =(LoginOrganVo)elementIp.getObjectValue();
|
09 |
ehCache.remove(vo.getId());
|
14 |
return
"redirect:/jigou/tologin" ;
|
⑥spring3攔截器的配置文件
3 |
< mvc:mapping
path = "/organmgt/**"
/> |
4 |
< bean
class = "com.woaika.loan.front.common.filter.OrgMgtInterceptor" >
|
這樣所有的/organmgt/開頭的請求都會被攔截,在這個攔截器進行檢查是否登錄就可以,這裏我採用的是用戶客戶端ip和用戶id兩個key存儲了用戶信息保證用戶的唯一信。
事實上到了這裏,一個簡單的Spring + ehCache Framework基本完成了。