Commit 6228da47 authored by hezhen's avatar hezhen

修改登录流程

parent 4bd29ffb
......@@ -7,6 +7,7 @@ import com.github.wxiaoqi.security.common.constant.RequestTypeConstants;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.process.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
......@@ -89,7 +90,7 @@ public class AuthController {
@RequestParam(value="nickname",defaultValue="")String nickname,
@RequestParam(value="headimgurl",defaultValue="")String headimgurl,
@RequestParam(value="openid",defaultValue="")String openid,
@RequestParam(value="password",defaultValue="")String unionid,
@RequestParam(value="unionid",defaultValue="")String unionid,
@RequestParam(value="type",defaultValue="0")Integer type,
@RequestParam(value="isQQ",defaultValue="0")Integer isQQ
)throws Exception{
......@@ -97,11 +98,13 @@ public class AuthController {
JSONObject data=appAuthService.wxregister( username, mobilecode, password, nickname,
headimgurl, openid, unionid,type,isQQ);
if(data!=null&&data.getInteger("status")== ResultCode.SUCCESS_CODE){
JwtAuthenticationRequest authenticationRequest=new JwtAuthenticationRequest();
authenticationRequest.setUsername(username);
authenticationRequest.setPassword(password);
String token=appAuthService.login(authenticationRequest);
data.put("token",token);
JSONObject result=data.getJSONObject("data");
if(result==null){
data.put("status",1001);
}else {
String token=appAuthService.getToken(username,result.getInteger("userid"));
data.put("token",token);
}
}
return data;
}
......@@ -116,4 +119,39 @@ public class AuthController {
return appAuthService.wxlogin(openid,isQQ);
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public JSONObject login(@RequestParam(value="username",defaultValue="")String username,
@RequestParam(value="mobilecode",defaultValue="")String mobilecode,
@RequestParam(value="password",defaultValue="")String password,
@RequestParam(value="type",defaultValue="1")Integer type)throws Exception{
log.info(username+"----require login...");
JSONObject data=appAuthService.tlogin(username,password,mobilecode,type);
if(data!=null&&data.getInteger("status")== ResultCode.SUCCESS_CODE){
JSONObject result=data.getJSONObject("data");
if(result==null){
data.put("status",1001);
}else {
String token=appAuthService.getToken(username,result.getInteger("userid"));
data.put("token",token);
}
}
return data;
}
@RequestMapping(value = "/reset", method = RequestMethod.POST)
public JSONObject reset(@RequestParam(value="username",defaultValue="")String username,
@RequestParam(value="mobilecode",defaultValue="")String mobilecode,
@RequestParam(value="password",defaultValue="")String password)throws Exception{
log.info(username+"----require reset...");
JSONObject data=appAuthService.reset(username,mobilecode,password);
if(data!=null&&data.getInteger("status")== ResultCode.SUCCESS_CODE){
JwtAuthenticationRequest authenticationRequest=new JwtAuthenticationRequest();
authenticationRequest.setUsername(username);
authenticationRequest.setPassword(password);
String token=appAuthService.login(authenticationRequest);
data.put("token",token);
}
return data;
}
}
......@@ -38,8 +38,17 @@ public interface IUserService {
@RequestParam(value="password")String password,@RequestParam(value="nickname")String nickname,
@RequestParam(value="headimgurl")String headimgurl,@RequestParam(value="openid")String openid,
@RequestParam(value="unionid")String unionid,@RequestParam(value="type")Integer type,@RequestParam(value="isQQ")Integer isQQ);
@RequestMapping(value = "/api/app/checkBindWechat",method = RequestMethod.POST)
@RequestMapping(value = "/api/app/user/checkBindWechat",method = RequestMethod.POST)
public JSONObject checkBindWechat( @RequestParam(value="username")String username);
@RequestMapping(value = "/api/app/user/wxlogin",method = RequestMethod.POST)
public JSONObject wxlogin(@RequestParam(value="openid")String openid,@RequestParam(value="isQQ")Integer isQQ);
@RequestMapping(value = "/api/app/user/login", method = RequestMethod.POST)
public JSONObject login( @RequestParam(value="username")String username,
@RequestParam(value="password")String password,
@RequestParam(value="mobilecode")String mobilecode,
@RequestParam(value="type")Integer type);
@RequestMapping(value = "/api/app/user/reset", method = RequestMethod.POST)
public JSONObject reset( @RequestParam(value="username")String username,
@RequestParam(value="mobilecode")String mobilecode,
@RequestParam(value="password")String password);
}
......@@ -13,4 +13,7 @@ public interface AuthService {
JSONObject wxregister( String username, String mobilecode, String password, String nickname, String headimgurl, String openid, String unionid, Integer type,Integer isQQ) throws Exception;
JSONObject checkBindWechat(String username) throws Exception;
JSONObject wxlogin(String openid,Integer isQQ) throws Exception;
JSONObject tlogin(String username, String password,String mobilecode,Integer type) throws Exception;
String getToken(String username,Integer id) throws Exception;
JSONObject reset(String username, String mobilecode, String password) throws Exception;
}
......@@ -70,4 +70,20 @@ public class AppAuthServiceImpl implements AuthService {
return userService.wxlogin(openid,isQQ);
}
@Override
public JSONObject tlogin(String username, String password, String mobilecode, Integer type) throws Exception {
return userService.login(username,password,mobilecode,type);
}
@Override
public String getToken(String username,Integer id) throws Exception {
return jwtTokenUtil.generateToken(new JWTInfo(username, id + "",null,
RequestTypeConstants.APP));
}
@Override
public JSONObject reset(String username, String mobilecode, String password) throws Exception {
return userService.reset(username,mobilecode,password);
}
}
......@@ -7,6 +7,7 @@ import com.github.wxiaoqi.security.auth.feign.IUserService;
import com.github.wxiaoqi.security.auth.service.AuthService;
import com.github.wxiaoqi.security.auth.util.user.JwtAuthenticationRequest;
import com.github.wxiaoqi.security.auth.util.user.JwtTokenUtil;
import com.github.wxiaoqi.security.common.constant.RequestTypeConstants;
import com.github.wxiaoqi.security.common.exception.auth.UserInvalidException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -67,4 +68,19 @@ public class AuthServiceImpl implements AuthService {
public JSONObject wxlogin(String openid,Integer isQQ) throws Exception {
return userService.wxlogin(openid,isQQ);
}
@Override
public JSONObject tlogin(String username, String password, String mobilecode, Integer type) throws Exception {
return userService.login(username,password,mobilecode,type);
}
@Override
public String getToken(String username,Integer id) throws Exception {
return jwtTokenUtil.generateToken(new JWTInfo(username, id + "",null,
RequestTypeConstants.APP));
}
@Override
public JSONObject reset(String username, String mobilecode, String password) throws Exception {
return userService.reset(username,mobilecode,password);
}
}
......@@ -47,6 +47,13 @@ public class AppUserLoginBiz extends BaseBiz<AppUserLoginMapper, AppUserLogin> {
super.updateSelectiveById(entity);
}
@CacheClear(pre="user{1.username}")
public void updatePasswordById(AppUserLogin entity) {
String password = new BCryptPasswordEncoder(UserConstant.PW_ENCORDER_SALT).encode(entity.getPassword());
entity.setPassword(password);
super.updateSelectiveById(entity);
}
/**
* 根据用户名获取用户信息
......
......@@ -130,5 +130,34 @@ public class AppUserRest {
return appPermissionService.weCahtLogin(openid,isQQ);
}
/**
* 登录
* @param username
* @param mobilecode
* @param password
* @param type 1-账号密码;2-验证码
* @return
*/
@RequestMapping(value = "/user/login", method = RequestMethod.POST)
public @ResponseBody
JSONObject login(
@RequestParam(value="username",defaultValue="")String username,
@RequestParam(value="mobilecode",defaultValue="")String mobilecode,
@RequestParam(value="password",defaultValue="")String password,
@RequestParam(value="type",defaultValue="1")Integer type
){
return appPermissionService.login(username,password,mobilecode,type);
}
@RequestMapping(value = "/user/reset", method = RequestMethod.POST)
public @ResponseBody
JSONObject login(
@RequestParam(value="username",defaultValue="")String username,
@RequestParam(value="mobilecode",defaultValue="")String mobilecode,
@RequestParam(value="password",defaultValue="")String password
){
return appPermissionService.reset(username,mobilecode,password);
}
}
......@@ -121,8 +121,8 @@ public class AppPermissionService {
return JsonResultUtil.createFailedResult(ResultCode.FAILED_CODE, "手机号未注册,请确认手机号无误");
}*/ else if (type == 4) {
AppUserLogin rsUserLogin = appUserLoginBiz.checkeUserLogin(phone);
if (rsUserLogin != null)
return JsonResultUtil.createFailedResult(ResultCode.EXIST_CODE, "手机号注册");
if (rsUserLogin == null)
return JsonResultUtil.createFailedResult(ResultCode.EXIST_CODE, "手机号注册");
}
// String sms = PassportUtil.SendSMS(phone, SystemConfig.SENDSMS_TITLE);
String mobilecode="123456";
......@@ -172,11 +172,11 @@ public class AppPermissionService {
return JsonResultUtil.createFailedResult(ResultCode.NULL_CODE, "参数为空");
}
String redisLockKey = RedisKey.CONSTANT_CODE_PREFIX+username+mobilecode;
String mobilecodeRedis=String.valueOf(userRedisTemplate.opsForValue().get(redisLockKey));
String mobilecodeRedis=userRedisTemplate.opsForValue().get(redisLockKey)==null?"":userRedisTemplate.opsForValue().get(redisLockKey).toString();
log.error("注册接口,获取redis中的验证码:" + mobilecodeRedis);
// 获取到缓存的验证码后要先清空缓存对应键的值
userRedisTemplate.delete(redisLockKey);
if (mobilecodeRedis == null) {
if (StringUtils.isBlank(mobilecodeRedis)) {
return JsonResultUtil.createFailedResult(ResultCode.NOTEXIST_CODE, "验证码错误");
}
// 是否已存在
......@@ -241,6 +241,17 @@ public class AppPermissionService {
JSONObject data = new JSONObject();
AppUserLogin userLoign = appUserLoginBiz.selectById(userid);
if (userLoign!=null) {
data.put("nickname", nickname);
data.put("headerurl",headimgurl);
AppUserDetail appUserDetail=appUserDetailBiz.getUserByUserid(userid);
if(appUserDetail!=null){
if(StringUtils.isNotBlank(appUserDetail.getNickname())){
data.put("nickname", appUserDetail.getNickname());
}
if(StringUtils.isNotBlank(appUserDetail.getHeadimgurl())){
data.put("headerurl", appUserDetail.getHeadimgurl());
}
}
// 缓存操作
String token="";
String imtoken_="";
......@@ -250,8 +261,7 @@ public class AppPermissionService {
// data.put("token", token);
data.put("username", username);
data.put("userid", userid);
data.put("nickname", nickname);
data.put("headerurl",headimgurl);
}
return data;
}
......@@ -331,7 +341,7 @@ public class AppPermissionService {
return JsonResultUtil.createSuccessResultWithObj(data);
}
} else if (type == 2) { // 新增
JSONObject register = register(username, password, nickname, headimgurl, mobilecode,
JSONObject register = register(username, password, headimgurl, nickname, mobilecode,
openId, unionid,isQQ);
if (register.getInteger("status") != ResultCode.SUCCESS_CODE) {
if (register.getInteger("status") == ResultCode.EXIST_CODE) {
......@@ -391,7 +401,7 @@ public class AppPermissionService {
}
AppUserLogin userLogin = appUserLoginBiz.getUserByOpenid(openId,isQQ);
if(userLogin==null){
return JsonResultUtil.createFailedResult(ResultCode.WXNOTEXIST_CODE, "该微信号尚未绑定手机号", openId);
return JsonResultUtil.createFailedResult(ResultCode.WXNOTEXIST_CODE, "该微信号尚未绑定手机号");
}
if(userLogin.getStatus()==1){
return JsonResultUtil.createFailedResult(ResultCode.EXIST_CODE, "用户已被禁用");
......@@ -411,4 +421,74 @@ public class AppPermissionService {
return JsonResultUtil.createDefaultFail();
}
public JSONObject login(String username, String password,String mobilecode,int type) {
if (StringUtils.isBlank(username) || ((StringUtils.isBlank(password)&&type==1)|| (StringUtils.isBlank(mobilecode)&&type==2))) {
return JsonResultUtil.createFailedResult(ResultCode.NULL_CODE, "请求参数为空");
}
try {
AppUserLogin user = appUserLoginBiz.checkeUserLogin(username);
if(type==1){
if (user==null||!encoder.matches(password,user.getPassword()))
return JsonResultUtil.createFailedResult(ResultCode.EXIST_CODE, "用户名或密码错误");
}else {
String redisLockKey = RedisKey.CONSTANT_CODE_PREFIX+username+mobilecode;
String mobilecodeRedis=userRedisTemplate.opsForValue().get(redisLockKey)==null?"":userRedisTemplate.opsForValue().get(redisLockKey).toString();
log.error("验证码登录接口,获取redis中的验证码:" + mobilecodeRedis);
// 获取到缓存的验证码后要先清空缓存对应键的值
userRedisTemplate.delete(redisLockKey);
if (StringUtils.isBlank(mobilecodeRedis))
return JsonResultUtil.createFailedResult(ResultCode.NOTEXIST_CODE, "验证码错误");
if (user==null)
return JsonResultUtil.createFailedResult(ResultCode.NOTEXIST_CODE, "用户不存在");
}
// 判断是否禁用
if (user.getStatus() == 1) {
return JsonResultUtil.createFailedResult(ResultCode.EXIST_CODE, "用户已被禁用");
}
Integer userid = user.getId();
String nickname=SystemConfig.USER_NIKENAME_DEFAULT+(int)((Math.random()*9+1)*100000);
JSONObject data = autoLogin(userid,user.getUsername(),SystemConfig.USER_HEADER_URL_DEFAULT,nickname);
if (data != null) {
return JsonResultUtil.createSuccessResultWithObj(data);
}
return JsonResultUtil.createDefaultFail();
}catch (Exception e) {
log.error("userlogin->error:{}", e.getMessage(), e);
return JsonResultUtil.createFailedResult(ResultCode.EXCEPTION_CODE, "出现异常");
}
}
@Transactional
public JSONObject reset(String username, String mobilecode, String password) {
if (StringUtils.isBlank(username) || StringUtils.isBlank(mobilecode)|| StringUtils.isBlank(password)) {
return JsonResultUtil.createFailedResult(ResultCode.NULL_CODE, "请求参数为空");
}
try {
String redisLockKey = RedisKey.CONSTANT_CODE_PREFIX+username+mobilecode;
String mobilecodeRedis=userRedisTemplate.opsForValue().get(redisLockKey)==null?"":userRedisTemplate.opsForValue().get(redisLockKey).toString();
log.error("验证码登录接口,获取redis中的验证码:" + mobilecodeRedis);
// 获取到缓存的验证码后要先清空缓存对应键的值
userRedisTemplate.delete(redisLockKey);
if (StringUtils.isBlank(mobilecodeRedis)){
return JsonResultUtil.createFailedResult(ResultCode.NOTEXIST_CODE, "验证码错误");
}
AppUserLogin user = appUserLoginBiz.checkeUserLogin(username);
if(user==null){
return JsonResultUtil.createFailedResult(ResultCode.NOTEXIST_CODE, "用户不存在");
}
user.setPassword(password);
appUserLoginBiz.updatePasswordById(user);
Integer userid=user.getId();
String nickname=SystemConfig.USER_NIKENAME_DEFAULT+(int)((Math.random()*9+1)*100000);
JSONObject data = autoLogin(userid,user.getUsername(),SystemConfig.USER_HEADER_URL_DEFAULT,nickname);
if (data != null) {
return JsonResultUtil.createSuccessResultWithObj(data);
}
return JsonResultUtil.createDefaultFail();
}catch (Exception e) {
log.error("userlogin->error:{}", e.getMessage(), e);
return JsonResultUtil.createFailedResult(ResultCode.EXCEPTION_CODE, "出现异常");
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment