11
zhouwei
2019-07-12 143f7be25ff19896e70ffc486999a64a3bc3b76f
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
package com.safeluck.aaej.app.service
 
import com.safeluck.aaej.app.config.DBAlias
import com.safeluck.aaej.app.po.DeviceBackList
import com.safeluck.aaej.app.po.MobileSession
import com.safeluck.aaej.base.dbhelper.impl.DbKey
import org.springframework.cache.annotation.CacheConfig
import org.springframework.cache.annotation.CacheEvict
import org.springframework.cache.annotation.CachePut
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service
import org.springframework.util.StringUtils
import javax.servlet.http.HttpServletRequest
 
@Service
class TokenService: BaseService()
{
    fun getToken(request: HttpServletRequest): String? {
        var token: String? = request.getHeader("Authorization")
 
        if (token == null) {
            request.cookies?.run {
                for (cookie in this) {
                    if (cookie.name == "token") {
                        token = cookie.value
                    }
                }
            }
 
        }
        return token
    }
 
    @DbKey(DBAlias.center)
    @Cacheable(key="'exists'+#token")
    fun sessionExists(token:String):Boolean
    {
        if(!redisService.keyExists(token))
        {
            return dbHelper.queryForInteger("select count(1) from app_session where token = ? ",token)>0
        }
        return true
    }
 
    @DbKey(DBAlias.center)
    @Cacheable(key="#token")
    fun getSession(token:String):MobileSession?
    {
        return dbHelper.findById(MobileSession::class.java,token)
    }
 
    @DbKey(DBAlias.center)
    @Cacheable(key="'black_'+#session.ip")
    fun isBlackList(session:MobileSession):Boolean
    {
        return dbHelper.findById(DeviceBackList::class.java,session.ip)!=null
    }
 
 
    @DbKey(DBAlias.center)
    @CachePut(key="#session.token")
    fun updateSession(session:MobileSession):MobileSession
    {
        dbHelper.update(session)
        return session
    }
 
    @DbKey(DBAlias.center)
    fun deleteSession(session:MobileSession)
    {
        dbHelper.delete(session)
    }
 
    @DbKey(DBAlias.center)
    fun insertSession(session:MobileSession):MobileSession
    {
        dbHelper.insert(session)
        return session
    }
}