|
@ -1,7 +1,10 @@
|
|
|
package com.yihu.jw.security.oauth2.provider.endpoint;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.jw.entity.base.login.BaseLoginLogDO;
|
|
|
import com.yihu.jw.restmodel.web.ObjEnvelop;
|
|
|
import com.yihu.jw.security.core.userdetails.jdbc.WlyyUserDetailsService;
|
|
|
import com.yihu.jw.security.login.service.BaseLoginLogService;
|
|
|
import com.yihu.jw.security.model.*;
|
|
|
import com.yihu.jw.security.oauth2.core.redis.WlyyRedisVerifyCodeService;
|
|
|
import com.yihu.jw.security.oauth2.provider.WlyyTokenGranter;
|
|
@ -84,6 +87,8 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
|
|
|
private WlyyRedisVerifyCodeService wlyyRedisVerifyCodeService;
|
|
|
@Autowired
|
|
|
private StringRedisTemplate redisTemplate;
|
|
|
@Autowired
|
|
|
private BaseLoginLogService baseLoginLogService;
|
|
|
|
|
|
@PostConstruct
|
|
|
private void init() {
|
|
@ -142,11 +147,11 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
|
|
|
在网关处通过HTTP状态码告知前端是过期(402)还是账号在别处登陆(403),
|
|
|
实现同一账号只能在一处登陆*/
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
if (request.getHeader("login-device") != null && request.getHeader("login-device").equals("mobile")) {
|
|
|
tokenStore.removeAccessToken(token.getValue());
|
|
|
tokenStore.removeRefreshToken(token.getRefreshToken().getValue());
|
|
|
token = getTokenGranter().grant(tokenRequest.getGrantType(), tokenRequest);
|
|
|
}
|
|
|
// if (request.getHeader("login-device") != null && request.getHeader("login-device").equals("mobile")) {
|
|
|
tokenStore.removeAccessToken(token.getValue());
|
|
|
tokenStore.removeRefreshToken(token.getRefreshToken().getValue());
|
|
|
token = getTokenGranter().grant(tokenRequest.getGrantType(), tokenRequest);
|
|
|
// }
|
|
|
if (token == null) {
|
|
|
throw new UnsupportedGrantTypeException("Unsupported grant type: " + tokenRequest.getGrantType());
|
|
|
}
|
|
@ -157,14 +162,27 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
|
|
|
wlyyUserSimple.setRefreshToken(token.getRefreshToken().getValue());
|
|
|
wlyyUserSimple.setUser(parameters.get("username"));
|
|
|
wlyyUserSimple.setState(parameters.get("state"));
|
|
|
|
|
|
String loginType = parameters.get("login_type");
|
|
|
String openid = parameters.get("openid");
|
|
|
//更新患者openId
|
|
|
if(!StringUtils.isEmpty(openid) && !"undefined".equalsIgnoreCase(openid) && "3".equalsIgnoreCase(loginType)){
|
|
|
BaseLoginLogDO baseLoginLogDO = new BaseLoginLogDO();
|
|
|
if(!StringUtils.isEmpty(openid) && !"undefined".equalsIgnoreCase(openid) && "3".equals(loginType)){
|
|
|
baseLoginLogDO.setOpenid(openid);
|
|
|
userDetailsService.updateOpenId(openid,wlyyUserSimple.getId());
|
|
|
}
|
|
|
if (parameters.get("password") != null) {
|
|
|
//使用密码登录成功后, 更新失败次数为 0
|
|
|
userDetailsService.addFailureCount(username,0);
|
|
|
}
|
|
|
userDetailsService.setRolePhth(loginType,token,wlyyUserSimple.getId(),redisTemplate);
|
|
|
|
|
|
baseLoginLogDO.setUserId(wlyyUserSimple.getId());
|
|
|
baseLoginLogDO.setCreateTime(new Date());
|
|
|
String userAgent = JSONObject.toJSONString(wlyyUserSimple);
|
|
|
baseLoginLogDO.setUserAgent(userAgent);
|
|
|
baseLoginLogDO.setLoginType(loginType);
|
|
|
baseLoginLogService.save(baseLoginLogDO);
|
|
|
|
|
|
return getResponse(wlyyUserSimple);
|
|
|
}
|
|
|
|
|
@ -209,6 +227,27 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
|
|
|
return getResponse(wlyyUserSimple);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param openid
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping(value = "/oauth/getByOpenId", method = RequestMethod.POST)
|
|
|
public ResponseEntity<Oauth2Envelop<WlyyUserSimple>> getByOpenId( @RequestParam(value = "openid", required = true) String openid) {
|
|
|
BaseLoginLogDO loginLog = baseLoginLogService.findByOpenId(openid);
|
|
|
if(loginLog== null){
|
|
|
throw new UsernameNotFoundException("can't find login log by openod: "+openid);
|
|
|
}
|
|
|
String userAgent = loginLog.getUserAgent();
|
|
|
WlyyUserSimple wlyyUserSimple = JSONObject.parseObject(userAgent, WlyyUserSimple.class);
|
|
|
String accessToken = wlyyUserSimple.getAccessToken();
|
|
|
OAuth2Authentication authentication = tokenStore.readAuthentication(accessToken);
|
|
|
if (null == authentication) {
|
|
|
throw new InvalidTokenException("Cant not load authentication");
|
|
|
}
|
|
|
return getResponse(wlyyUserSimple);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 登出
|
|
|
* @param parameters
|
|
@ -323,7 +362,17 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
|
|
|
headers.set("Pragma", "no-cache");
|
|
|
return new ResponseEntity<>(oauth2Envelop, headers, HttpStatus.OK);
|
|
|
}
|
|
|
throw new IllegalStateException((String) result.get("message"));
|
|
|
String message = (String) result.get("message");
|
|
|
|
|
|
try {
|
|
|
JSONObject jsonStr = JSONObject.parseObject(message);
|
|
|
if(jsonStr.containsKey("Message")){
|
|
|
message = jsonStr.getString("Message");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
}
|
|
|
throw new IllegalStateException(message);
|
|
|
/*Captcha _captcha = new Captcha();
|
|
|
_captcha.setCode("12345");
|
|
|
_captcha.setExpiresIn(10000);
|
|
@ -451,7 +500,17 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
|
|
|
String loginType = parameters.get("login_type");
|
|
|
|
|
|
userDetailsService.setRolePhth(loginType,token,wlyyUserSimple.getId(),redisTemplate);
|
|
|
|
|
|
BaseLoginLogDO baseLoginLogDO = new BaseLoginLogDO();
|
|
|
baseLoginLogDO.setUserId(wlyyUserSimple.getId());
|
|
|
baseLoginLogDO.setCreateTime(new Date());
|
|
|
String userAgent = JSONObject.toJSONString(wlyyUserSimple);
|
|
|
baseLoginLogDO.setUserAgent(userAgent);
|
|
|
baseLoginLogDO.setLoginType(loginType);
|
|
|
String openid = parameters.get("openid");
|
|
|
if(!StringUtils.isEmpty(openid) && "undefined".equalsIgnoreCase(openid)){
|
|
|
baseLoginLogDO.setOpenid(openid);
|
|
|
}
|
|
|
baseLoginLogService.save(baseLoginLogDO);
|
|
|
return getResponse(wlyyUserSimple);
|
|
|
}
|
|
|
return null;
|
|
@ -501,13 +560,13 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
|
|
|
} else if (e instanceof InvalidGrantException) {
|
|
|
return handleOAuth2Exception(new Oauth2Envelop(invalidGrantMessage((InvalidGrantException)e), HttpStatus.UNAUTHORIZED.value()), e);
|
|
|
} else if (e instanceof InvalidTokenException) {
|
|
|
return handleOAuth2Exception(new Oauth2Envelop("Token有误!", HttpStatus.UNAUTHORIZED.value()), e);
|
|
|
return handleOAuth2Exception(new Oauth2Envelop("Token有误/过期!", HttpStatus.FORBIDDEN.value()), e);
|
|
|
} else if (e instanceof InvalidRequestException) {
|
|
|
return handleOAuth2Exception(new Oauth2Envelop("参数" + e.getMessage() + "缺失!", HttpStatus.UNAUTHORIZED.value()), e);
|
|
|
} else if (e instanceof IllegalAccessException) {
|
|
|
return handleOAuth2Exception(new Oauth2Envelop("短信请求频率过快,请稍后再试!", -1), e);
|
|
|
} else if (e instanceof IllegalStateException) {
|
|
|
return handleOAuth2Exception(new Oauth2Envelop("短信网关请求失败!", -1), e);
|
|
|
return handleOAuth2Exception(new Oauth2Envelop(e.getMessage(), -1), e);
|
|
|
}
|
|
|
return handleOAuth2Exception(new Oauth2Envelop(e.getMessage(), -1), e);
|
|
|
}
|