@@ -61,5 +61,11 @@ public interface AccountService { | |||
* @return | |||
*/ | |||
public DataResult<AccountVO> wxRegister(AccountVO account,String verificationCode); | |||
/** | |||
* 通过session获取用户信息 | |||
* @return | |||
*/ | |||
public DataResult<AccountVO> queryUserBySession(); | |||
} |
@@ -56,6 +56,7 @@ | |||
</exclusion> | |||
</exclusions> | |||
</dependency> | |||
<dependency> | |||
<groupId>com.hp</groupId> | |||
<artifactId>user-client</artifactId> | |||
@@ -0,0 +1,25 @@ | |||
package com.hp.user.service.config; | |||
import org.springframework.context.annotation.Configuration; | |||
import org.springframework.web.servlet.config.annotation.*; | |||
import com.hp.user.service.interceptor.SystemInterceptor; | |||
//放在主类上不包括其他东西 | |||
//WebMvcConfigurerAdapter这个类,重写这个类中的方法可以让我们增加额外的配置 | |||
@Configuration | |||
public class WebAppConfigurer implements WebMvcConfigurer { | |||
//addResourceLocations指的是文件放置的目录 | |||
@Override | |||
public void addInterceptors(InterceptorRegistry registry) { | |||
// 多个拦截器组成一个拦截器链 | |||
registry.addInterceptor(new SystemInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**"); | |||
} | |||
//addResoureHandler指的是对外暴露的访问路径 此处一般默认即可 | |||
@Override | |||
public void addResourceHandlers(ResourceHandlerRegistry registry) { | |||
// 静态资源拦截器 | |||
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); | |||
} | |||
} |
@@ -32,5 +32,9 @@ public class UserConstants { | |||
public final static String WX_SECRET = "949ca88e8f786d9496d695f956cae594"; | |||
public final static String WX_GRANT_TYPE = "authorization_code"; | |||
public final static String SESSION_PRIFX = "hp@session@"; | |||
public final static int SESSION_OUT_TIME = 1800; | |||
} |
@@ -17,7 +17,7 @@ import io.swagger.annotations.ApiOperation; | |||
*/ | |||
@Api(tags="短信") | |||
@RestController | |||
@RequestMapping("/area") | |||
@RequestMapping("/message") | |||
public class MessageController { | |||
@Autowired | |||
@@ -106,4 +106,13 @@ public class UserController { | |||
public Result batchChangeStatusOfUser(@RequestBody String[] userIds, @RequestParam Short status) { | |||
return accountService.batchChangeStatusOfUser(userIds, status); | |||
} | |||
/** | |||
* 通过session查询用户 | |||
*/ | |||
@RequestMapping("/queryUserBySession") | |||
@ApiOperation(value = "通过session查询用户", notes = "通过session查询用户", httpMethod = "POST") | |||
public DataResult<AccountVO> queryUserBySession() { | |||
return accountService.queryUserBySession(); | |||
} | |||
} |
@@ -10,15 +10,23 @@ import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; | |||
import com.hp.user.client.entity.*; | |||
import com.hp.user.client.service.AccountService; | |||
import com.hp.user.client.service.MessageService; | |||
import com.hp.user.service.constants.UserConstants; | |||
import com.hp.user.service.dao.AccountMapper; | |||
import com.hp.user.service.entity.Account; | |||
import com.hp.user.service.entity.CustomerCompany; | |||
import com.hp.user.service.entity.ElectricianGroup; | |||
import com.hp.user.service.redis.RedisOperation; | |||
import com.hp.user.service.utils.WeixinSignUtil; | |||
import redis.clients.jedis.Jedis; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import javax.servlet.http.HttpServletRequest; | |||
import javax.servlet.http.HttpServletResponse; | |||
import javax.servlet.http.HttpSession; | |||
import org.apache.commons.lang3.StringUtils; | |||
import org.springframework.beans.BeanUtils; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
@@ -34,9 +42,12 @@ import org.springframework.stereotype.Service; | |||
*/ | |||
@Service | |||
public class AccountServiceImpl implements AccountService { | |||
@Autowired | |||
private MessageService messageService; | |||
@Autowired | |||
private HttpServletRequest request; | |||
@Autowired | |||
private AccountMapper accountMapper; | |||
@@ -46,12 +57,19 @@ public class AccountServiceImpl implements AccountService { | |||
Result result = new Result(); | |||
try { | |||
//需要判断phone是否存在 | |||
Result checkPhoneResult = checkPhone(phone); | |||
if(null != checkPhoneResult.getMessage()) { | |||
DataResult<Account> checkPhoneResult = checkPhone(phone); | |||
Account account = checkPhoneResult.getData(); | |||
HttpSession session = request.getSession(); | |||
if(null != account) { | |||
String code = messageService.getVerificationCode(phone); | |||
if (StringUtils.equals(verificationCode, code)) { | |||
//登陆成功 | |||
messageService.deleteVerificationCode(phone); | |||
String sessionId = session.getId(); | |||
account.setWxOpenId(null); | |||
Jedis jedis = RedisOperation.getRedis(); | |||
String key = UserConstants.SESSION_PRIFX+sessionId; | |||
jedis.setex(key, UserConstants.EXPIRE_SECONDS, JSONObject.toJSONString(account)); | |||
} else { | |||
//登陆失败 | |||
//返回错误信息 | |||
@@ -63,6 +81,7 @@ public class AccountServiceImpl implements AccountService { | |||
result.setMessage("手机号码未注册!"); | |||
} | |||
} catch (Exception e) { | |||
e.printStackTrace(); | |||
result.setCode("-1"); | |||
result.setMessage("系统出现问题"); | |||
} | |||
@@ -70,22 +89,21 @@ public class AccountServiceImpl implements AccountService { | |||
} | |||
@Override | |||
public Result checkPhone(String phone) { | |||
Result result = new Result(); | |||
public DataResult<Account> checkPhone(String phone) { | |||
DataResult<Account> result = new DataResult<Account>(); | |||
//不存在为true | |||
Boolean flage = true; | |||
try { | |||
QueryWrapper<Account> queryWrapper = new QueryWrapper<>(); | |||
queryWrapper.eq("phone", phone); | |||
queryWrapper.eq("sys_deleted", 0); | |||
Integer count = accountMapper.selectCount(queryWrapper); | |||
if (count > 0) { | |||
Account account = accountMapper.selectOne(queryWrapper); | |||
if (null != account) { | |||
//表示手机以及存在,需要返回错误 | |||
result.setCode("-1"); | |||
result.setMessage("手机号已经存在"); | |||
result.setData(account); | |||
} | |||
} catch (Exception e) { | |||
result.setCode("-1"); | |||
result.setMessage("系统出现问题"); | |||
@@ -197,6 +215,7 @@ public class AccountServiceImpl implements AccountService { | |||
} | |||
String code = messageService.getVerificationCode(accountVO.getPhone()); | |||
if(StringUtils.equals(verificationCode, code)) { | |||
messageService.deleteVerificationCode(accountVO.getPhone()); | |||
Account account = new Account(); | |||
BeanUtils.copyProperties(accountVO, account); | |||
Long id = IdWorker.getId(); | |||
@@ -377,6 +396,7 @@ public class AccountServiceImpl implements AccountService { | |||
if (SqlHelper.retBool(flag)) { | |||
result.setCode("0"); | |||
} else { | |||
result.setCode("-1"); | |||
result.setMessage("更新失败"); | |||
} | |||
@@ -400,8 +420,37 @@ public class AccountServiceImpl implements AccountService { | |||
result.setData(temp); | |||
} | |||
} catch (Exception e) { | |||
e.printStackTrace(); | |||
result.setCode("-1"); | |||
result.setMessage("更新失败"); | |||
} | |||
return result; | |||
} | |||
@Override | |||
public DataResult<AccountVO> queryUserBySession() { | |||
DataResult<AccountVO> result = new DataResult<AccountVO>(); | |||
try { | |||
HttpSession session = request.getSession(); | |||
String sessionId = session.getId(); | |||
String value = null; | |||
AccountVO accountVO = null; | |||
if(!StringUtils.isBlank(sessionId)) { | |||
Jedis jedis = RedisOperation.getRedis(); | |||
String key = UserConstants.SESSION_PRIFX+sessionId; | |||
value = jedis.get(key); | |||
accountVO = JSONObject.parseObject(value, AccountVO.class); | |||
} | |||
result.setData(accountVO); | |||
// String userId = ; | |||
// session. | |||
}catch(Exception e) { | |||
e.printStackTrace(); | |||
result.setCode("-1"); | |||
result.setMessage("系统出现问题"); | |||
} | |||
return result; | |||
} | |||
} |
@@ -0,0 +1,131 @@ | |||
package com.hp.user.service.interceptor; | |||
import javax.servlet.http.HttpServletRequest; | |||
import javax.servlet.http.HttpServletResponse; | |||
import javax.servlet.http.HttpSession; | |||
import org.apache.commons.lang3.StringUtils; | |||
import org.springframework.stereotype.Repository; | |||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | |||
import com.hp.user.service.constants.UserConstants; | |||
import com.hp.user.service.redis.RedisOperation; | |||
import redis.clients.jedis.Jedis; | |||
/** | |||
* | |||
* @author geloin | |||
*/ | |||
@Repository | |||
public class SystemInterceptor extends HandlerInterceptorAdapter { | |||
// @Resource(name = "systemLoggerService") | |||
// private SystemLoggerService systemLoggerService; | |||
/* | |||
* (non-Javadoc) | |||
* | |||
* @see | |||
* org.springframework.web.servlet.handler.HandlerInterceptorAdapter#preHandle | |||
* (javax.servlet.http.HttpServletRequest, | |||
* javax.servlet.http.HttpServletResponse, java.lang.Object) | |||
*/ | |||
@SuppressWarnings({ "rawtypes", "unchecked" }) | |||
@Override | |||
public boolean preHandle(HttpServletRequest request, | |||
HttpServletResponse response, Object handler) throws Exception { | |||
String uri = request.getRequestURI(); | |||
HttpSession session = request.getSession(); | |||
String sessionId = session.getId(); | |||
// "message | |||
if(-1 != StringUtils.indexOfIgnoreCase(uri,"message") || -1 != StringUtils.indexOfIgnoreCase(uri,"swagger") || -1 != StringUtils.indexOfIgnoreCase(uri,"authentication/login") | |||
|| -1 != StringUtils.indexOfIgnoreCase(uri,"wxAuthorization")||-1 != StringUtils.indexOfIgnoreCase(uri,"wxRegister")) { | |||
// response.setStatus(401); | |||
return true; | |||
} | |||
if(StringUtils.isBlank(sessionId)) { | |||
response.setStatus(401); | |||
return false; | |||
}else { | |||
Jedis jedis = RedisOperation.getRedis(); | |||
String key = UserConstants.SESSION_PRIFX+sessionId; | |||
String value = jedis.get(key); | |||
if(StringUtils.isBlank(value)) { | |||
response.setStatus(401); | |||
return false; | |||
}else { | |||
return true; | |||
} | |||
} | |||
// System.out.println("拦截测试"); | |||
// return false; | |||
// request.setCharacterEncoding("UTF-8"); | |||
// response.setCharacterEncoding("UTF-8"); | |||
// response.setContentType("text/html;charset=UTF-8"); | |||
// | |||
// // 后台session控制 | |||
// String[] noFilters = new String[] { "login.html", "veriCode.html", | |||
// "index.html", "logout.html" }; | |||
// String uri = request.getRequestURI(); | |||
// | |||
// if (uri.indexOf("log") != -1) { | |||
// boolean beFilter = true; | |||
// for (String s : noFilters) { | |||
// if (uri.indexOf(s) != -1) { | |||
// beFilter = false; | |||
// break; | |||
// } | |||
// } | |||
// if (beFilter) { | |||
// Object obj = request.getSession().getAttribute( | |||
// Constants.LOGINED); | |||
// if (null == obj) { | |||
// | |||
// // 未登录 | |||
// PrintWriter out = response.getWriter(); | |||
// StringBuilder builder = new StringBuilder(); | |||
// builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">"); | |||
// builder.append("alert(\"页面过期,请重新登录\");"); | |||
// builder.append("window.top.location.href=\""); | |||
// builder.append(Constants.basePath); | |||
// builder.append("/background/index.html\";</script>"); | |||
// out.print(builder.toString()); | |||
// out.close(); | |||
// return false; | |||
// } else { | |||
// // 添加日志 | |||
//// String operateContent = Constants.operateContent(uri); | |||
//// if (null != operateContent) { | |||
//// String url = uri.substring(uri.indexOf("background")); | |||
//// String ip = request.getRemoteAddr(); | |||
//// Integer userId = ((SystemUserForm) obj).getId(); | |||
//// SystemLoggerForm form = new SystemLoggerForm(); | |||
//// form.setUserId(userId); | |||
//// form.setIp(ip); | |||
//// form.setOperateContent(operateContent); | |||
//// form.setUrl(url); | |||
//// this.systemLoggerService.edit(form); | |||
// } | |||
// } | |||
// } | |||
// } | |||
// | |||
// Map paramsMap = request.getParameterMap(); | |||
// | |||
// for (Iterator<Map.Entry> it = paramsMap.entrySet().iterator(); it | |||
// .hasNext();) { | |||
// Map.Entry entry = it.next(); | |||
// Object[] values = (Object[]) entry.getValue(); | |||
// for (Object obj : values) { | |||
// if (!DataUtil.isValueSuccessed(obj)) { | |||
// throw new RuntimeException("有非法字符:" + obj); | |||
// } | |||
// } | |||
// } | |||
// | |||
// return super.preHandle(request, response, handler); | |||
} | |||
} |