Commit 10afb83c70c4caf235fa80b4cedb1b09823b774a
1 parent
e46902e72e
Exists in
master
and in
1 other branch
增加
Showing 4 changed files with 307 additions and 0 deletions
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/session/SessionProvider.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/session/strategy/ISessionProvider.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/session/strategy/LocalCacheSessionStrategy.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/session/strategy/UserCenterStrategy.java
platform-operate-api/src/main/java/com/lyms/platform/operate/web/session/SessionProvider.java
View file @
10afb83
| 1 | +package com.lyms.platform.operate.web.session; | |
| 2 | + | |
| 3 | +import com.lyms.platform.common.base.LoginContext; | |
| 4 | +import com.lyms.platform.operate.web.session.strategy.ISessionProvider; | |
| 5 | + | |
| 6 | +import javax.servlet.http.HttpServletRequest; | |
| 7 | +import javax.servlet.http.HttpServletResponse; | |
| 8 | +import java.util.Map; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * session 能力提供 | |
| 12 | + * <p> | |
| 13 | + * <p> | |
| 14 | + * Created by Administrator on 2016/6/2 0002. | |
| 15 | + */ | |
| 16 | +public class SessionProvider implements ISessionProvider { | |
| 17 | + /** | |
| 18 | + * 配置 | |
| 19 | + */ | |
| 20 | + private Map<String, ISessionProvider> iSessionProviderMap; | |
| 21 | + /** | |
| 22 | + * 默认的session提供 | |
| 23 | + */ | |
| 24 | + private ISessionProvider defaultSessionProvider; | |
| 25 | + /** | |
| 26 | + * 当前策略 | |
| 27 | + */ | |
| 28 | + private String currentStrateger; | |
| 29 | + | |
| 30 | + public String getCurrentStrateger() { | |
| 31 | + return currentStrateger; | |
| 32 | + } | |
| 33 | + | |
| 34 | + public void setCurrentStrateger(String currentStrateger) { | |
| 35 | + this.currentStrateger = currentStrateger; | |
| 36 | + } | |
| 37 | + | |
| 38 | + public ISessionProvider getDefaultSessionProvider() { | |
| 39 | + return defaultSessionProvider; | |
| 40 | + } | |
| 41 | + | |
| 42 | + public void setDefaultSessionProvider(ISessionProvider defaultSessionProvider) { | |
| 43 | + this.defaultSessionProvider = defaultSessionProvider; | |
| 44 | + } | |
| 45 | + | |
| 46 | + public Map<String, ISessionProvider> getiSessionProviderMap() { | |
| 47 | + return iSessionProviderMap; | |
| 48 | + } | |
| 49 | + | |
| 50 | + public void setiSessionProviderMap(Map<String, ISessionProvider> iSessionProviderMap) { | |
| 51 | + this.iSessionProviderMap = iSessionProviderMap; | |
| 52 | + } | |
| 53 | + | |
| 54 | + @Override | |
| 55 | + public LoginContext checkSession(HttpServletRequest request, HttpServletResponse response, String token) { | |
| 56 | + ISessionProvider iSessionProvider = iSessionProviderMap.get(currentStrateger); | |
| 57 | + if (null != iSessionProvider) { | |
| 58 | + return iSessionProvider.checkSession(request, response, token); | |
| 59 | + } | |
| 60 | + if (null != defaultSessionProvider) { | |
| 61 | + return defaultSessionProvider.checkSession(request, response, token); | |
| 62 | + } | |
| 63 | + return null; | |
| 64 | + } | |
| 65 | + | |
| 66 | + @Override | |
| 67 | + public void removeSession(String token) { | |
| 68 | + ISessionProvider iSessionProvider = iSessionProviderMap.get(currentStrateger); | |
| 69 | + if (null != iSessionProvider) { | |
| 70 | + iSessionProvider.removeSession(token); | |
| 71 | + } | |
| 72 | + if (null != defaultSessionProvider) { | |
| 73 | + defaultSessionProvider.removeSession(token); | |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + @Override | |
| 78 | + public LoginContext login(String account, String password, String code) { | |
| 79 | + ISessionProvider iSessionProvider = iSessionProviderMap.get(currentStrateger); | |
| 80 | + if (null != iSessionProvider) { | |
| 81 | + return iSessionProvider.login(account, password, code); | |
| 82 | + } | |
| 83 | + if (null != defaultSessionProvider) { | |
| 84 | + return defaultSessionProvider.login(account, password, code); | |
| 85 | + } | |
| 86 | + return null; | |
| 87 | + } | |
| 88 | + | |
| 89 | + @Override | |
| 90 | + public LoginContext register(String userId, String phone, String account, String pwd) { | |
| 91 | + ISessionProvider iSessionProvider = iSessionProviderMap.get(currentStrateger); | |
| 92 | + if (null != iSessionProvider) { | |
| 93 | + return iSessionProvider.register(userId, phone, account,pwd); | |
| 94 | + } | |
| 95 | + if (null != defaultSessionProvider) { | |
| 96 | + return defaultSessionProvider.register(userId, phone, account, pwd); | |
| 97 | + } | |
| 98 | + return null; | |
| 99 | + } | |
| 100 | +} |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/session/strategy/ISessionProvider.java
View file @
10afb83
| 1 | +package com.lyms.platform.operate.web.session.strategy; | |
| 2 | +import com.lyms.platform.common.base.LoginContext; | |
| 3 | + | |
| 4 | +import javax.servlet.http.HttpServletRequest; | |
| 5 | +import javax.servlet.http.HttpServletResponse; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * | |
| 9 | + * 提供验证session的方法 | |
| 10 | + * | |
| 11 | + * Created by Administrator on 2016/6/2 0002. | |
| 12 | + */ | |
| 13 | +public interface ISessionProvider { | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * 检查session | |
| 17 | + * | |
| 18 | + * @param request | |
| 19 | + * @param response | |
| 20 | + * @param token | |
| 21 | + * @return | |
| 22 | + */ | |
| 23 | + LoginContext checkSession(HttpServletRequest request,HttpServletResponse response,String token); | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * 删除session | |
| 27 | + * @param token | |
| 28 | + */ | |
| 29 | + void removeSession(String token); | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * 登录 | |
| 33 | + * | |
| 34 | + * @return | |
| 35 | + */ | |
| 36 | + LoginContext login(String account,String password,String code); | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * 注册 | |
| 40 | + * | |
| 41 | + * @param userId | |
| 42 | + * @param phone | |
| 43 | + * @param account | |
| 44 | + * @param pwd | |
| 45 | + * @return | |
| 46 | + */ | |
| 47 | + LoginContext register(String userId, String phone,String account, String pwd); | |
| 48 | +} |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/session/strategy/LocalCacheSessionStrategy.java
View file @
10afb83
| 1 | +package com.lyms.platform.operate.web.session.strategy; | |
| 2 | + | |
| 3 | +import com.google.common.cache.Cache; | |
| 4 | +import com.google.common.cache.CacheBuilder; | |
| 5 | +import com.lyms.platform.common.base.LoginContext; | |
| 6 | +import com.lyms.platform.common.enums.YnEnums; | |
| 7 | +import com.lyms.platform.common.exception.TokenException; | |
| 8 | +import com.lyms.platform.common.utils.MD5Utils; | |
| 9 | +import com.lyms.platform.common.utils.TokenUtils; | |
| 10 | +import com.lyms.platform.permission.model.Users; | |
| 11 | +import com.lyms.platform.permission.model.UsersQuery; | |
| 12 | +import com.lyms.platform.permission.service.UsersService; | |
| 13 | +import org.apache.commons.collections.CollectionUtils; | |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 15 | + | |
| 16 | +import javax.servlet.http.HttpServletRequest; | |
| 17 | +import javax.servlet.http.HttpServletResponse; | |
| 18 | +import java.util.List; | |
| 19 | +import java.util.Map; | |
| 20 | +import java.util.UUID; | |
| 21 | +import java.util.concurrent.ConcurrentHashMap; | |
| 22 | +import java.util.concurrent.TimeUnit; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * 本地session缓存策略 | |
| 26 | + * <p> | |
| 27 | + * Created by Administrator on 2016/6/2 0002. | |
| 28 | + */ | |
| 29 | +public class LocalCacheSessionStrategy implements ISessionProvider { | |
| 30 | + @Autowired | |
| 31 | + private UsersService usersService; | |
| 32 | + | |
| 33 | + private String preFix = "luc"; | |
| 34 | + | |
| 35 | + private Map<String, LoginContext> localSessionCache = new ConcurrentHashMap<>(); | |
| 36 | + | |
| 37 | + public static Cache<String,LoginContext> cache = CacheBuilder.newBuilder() | |
| 38 | + .expireAfterAccess(30, TimeUnit.MINUTES) | |
| 39 | + .build(); | |
| 40 | + | |
| 41 | + @Override | |
| 42 | + public LoginContext checkSession(HttpServletRequest request, HttpServletResponse response, String token) { | |
| 43 | + LoginContext loginContext = cache.getIfPresent(token); | |
| 44 | + if(null==loginContext||!loginContext.isLogin()) { | |
| 45 | + throw new TokenException(); | |
| 46 | + } | |
| 47 | + Users users = usersService.getUsers(loginContext.getId()); | |
| 48 | + if(null == users) { | |
| 49 | + throw new TokenException(); | |
| 50 | + } | |
| 51 | + request.setAttribute("loginContext", loginContext); | |
| 52 | + return loginContext; | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Override | |
| 56 | + public void removeSession(String token) { | |
| 57 | + /*cache.remove(token);*/ | |
| 58 | + } | |
| 59 | + | |
| 60 | + | |
| 61 | + @Override | |
| 62 | + public LoginContext login(String account, String password, String code) { | |
| 63 | + | |
| 64 | + UsersQuery membersQuery = new UsersQuery(); | |
| 65 | + membersQuery.setAccount(account); | |
| 66 | + membersQuery.setPwd(MD5Utils.md5(password).toUpperCase()); | |
| 67 | + membersQuery.setYn(YnEnums.YES.getId()); | |
| 68 | + //本地版本忽略code没有短信验证码 | |
| 69 | + List<Users> membersList = usersService.queryUsers(membersQuery); | |
| 70 | + LoginContext loginContext = new LoginContext(); | |
| 71 | + loginContext.setErrormsg("用户不存在."); | |
| 72 | + loginContext.setErrorcode(4003); | |
| 73 | + if (CollectionUtils.isNotEmpty(membersList)) { | |
| 74 | + Integer userId = membersList.get(0).getId(); | |
| 75 | + loginContext.setId(userId); | |
| 76 | + String token = preFix + TokenUtils.getToken(UUID.randomUUID().toString()); | |
| 77 | + loginContext.setToken(token); | |
| 78 | + loginContext.setErrorcode(0); | |
| 79 | + loginContext.setErrormsg("登录成功"); | |
| 80 | + cache.put(token, loginContext); | |
| 81 | + } | |
| 82 | + return loginContext; | |
| 83 | + } | |
| 84 | + | |
| 85 | + @Override | |
| 86 | + public LoginContext register(String userId, String phone, String account, String pwd) { | |
| 87 | + LoginContext loginContext = new LoginContext(); | |
| 88 | + return loginContext; | |
| 89 | + } | |
| 90 | +} |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/session/strategy/UserCenterStrategy.java
View file @
10afb83
| 1 | +package com.lyms.platform.operate.web.session.strategy; | |
| 2 | + | |
| 3 | +import com.lyms.platform.common.base.LoginContext; | |
| 4 | +import com.lyms.platform.common.exception.TokenException; | |
| 5 | +import com.lyms.platform.common.utils.LoginUtil; | |
| 6 | +import com.lyms.platform.permission.model.Users; | |
| 7 | +import com.lyms.platform.permission.service.UsersService; | |
| 8 | +import org.apache.commons.lang.StringUtils; | |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | +import org.springframework.beans.factory.annotation.Value; | |
| 11 | + | |
| 12 | +import javax.servlet.http.HttpServletRequest; | |
| 13 | +import javax.servlet.http.HttpServletResponse; | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * 基于用户中心的登录策略 | |
| 17 | + * | |
| 18 | + * Created by Administrator on 2016/6/2 0002. | |
| 19 | + */ | |
| 20 | + | |
| 21 | +public class UserCenterStrategy implements ISessionProvider { | |
| 22 | + @Autowired | |
| 23 | + private UsersService usersService; | |
| 24 | + @Value("#{configProperties['login.token']}") | |
| 25 | + private String token; | |
| 26 | + @Value("#{configProperties['login.typeId']}") | |
| 27 | + private String typeId; | |
| 28 | + @Override | |
| 29 | + public LoginContext checkSession(HttpServletRequest request, HttpServletResponse response,String token) { | |
| 30 | + LoginContext loginContext = LoginUtil.checkLoginState(token); | |
| 31 | + if(!loginContext.isLogin()) { | |
| 32 | + throw new TokenException(); | |
| 33 | + } | |
| 34 | + Users users = usersService.getUsersByLoginCenterId(loginContext.getId()); | |
| 35 | + if(null == users) { | |
| 36 | + throw new TokenException(); | |
| 37 | + } | |
| 38 | + loginContext.setId(users.getId()); | |
| 39 | + loginContext.setToken(token); | |
| 40 | + request.setAttribute("loginContext", loginContext); | |
| 41 | + return loginContext; | |
| 42 | + } | |
| 43 | + | |
| 44 | + @Override | |
| 45 | + public void removeSession(String token) { | |
| 46 | + if(StringUtils.isNotEmpty(token)){ | |
| 47 | + LoginUtil.deleteToken(token); | |
| 48 | + } | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Override | |
| 52 | + public LoginContext login(String account, String password,String code) { | |
| 53 | + LoginContext loginContext = new LoginContext(); | |
| 54 | + loginContext.setErrormsg("服务器内部错误!"); | |
| 55 | + loginContext.setErrorcode(-1); | |
| 56 | + if(null != code) { | |
| 57 | + loginContext =LoginUtil.loginByPhone(account, code, typeId, token); | |
| 58 | + } | |
| 59 | + if(null != password) { | |
| 60 | + loginContext = LoginUtil.loginByAccount(account, password, typeId, token); | |
| 61 | + } | |
| 62 | + return loginContext; | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + public LoginContext register(String userId, String phone, String account, String pwd) { | |
| 67 | + return LoginUtil.register(null, phone,account, pwd, token, typeId); | |
| 68 | + } | |
| 69 | +} |