Jelajahi Sumber

Merge branch 'dev' of yeshijie/wlyy2.0 into dev

叶仕杰 5 tahun lalu
induk
melakukan
2e0fac78fe

+ 170 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/oauth2/provider/endpoint/WlyyYkyyEndpoint.java

@ -0,0 +1,170 @@
package com.yihu.jw.security.oauth2.provider.endpoint;
import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.entity.base.login.BaseLoginLogDO;
import com.yihu.jw.entity.base.patient.BasePatientDO;
import com.yihu.jw.restmodel.web.ObjEnvelop;
import com.yihu.jw.security.core.userdetails.jdbc.WlyyUserDetailsService;
import com.yihu.jw.security.dao.doctor.BaseDoctorDao;
import com.yihu.jw.security.login.service.BaseLoginLogService;
import com.yihu.jw.security.model.WlyyUserSimple;
import com.yihu.jw.security.oauth2.provider.WlyyTokenGranter;
import com.yihu.jw.security.service.YkyyService;
import io.swagger.annotations.Api;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidRequestException;
import org.springframework.security.oauth2.common.exceptions.UnsupportedGrantTypeException;
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.endpoint.AbstractEndpoint;
import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;
import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestValidator;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.Map;
/**
 * Created by yeshijie on 2020/4/24.
 *
 * @author yeshijie.
 */
@Api(description = "眼科医院")
@RestController
public class WlyyYkyyEndpoint extends AbstractEndpoint {
    private final Logger LOG = LoggerFactory.getLogger(getClass());
    private OAuth2RequestFactory oAuth2RequestFactory;
    private OAuth2RequestValidator oAuth2RequestValidator = new DefaultOAuth2RequestValidator();
    @Autowired
    private YkyyService ykyyService;
    @Autowired
    private WlyyUserDetailsService userDetailsService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private BaseDoctorDao doctorDao;
    @Autowired
    private ClientDetailsService clientDetailsService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private BaseLoginLogService baseLoginLogService;
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private WlyyTokenGranter tokenGranter;
    @PostConstruct
    private void init() {
        super.setTokenGranter(tokenGranter);
    }
    /**
     * 眼科医生登录
     * @param verifyCode
     * @param client_id
     * @param login_type 用户类型 1或默认为user,2:医生登录
     * @return
     */
    @RequestMapping(value = "/oauth/ykyyDoctorLogin", method = RequestMethod.POST)
    public ObjEnvelop ykyyDoctorLogin(String verifyCode, String client_id,String login_type) {
        if (StringUtils.isEmpty(client_id)) {
            throw new InvalidRequestException("client_id is null");
        }
        if (StringUtils.isEmpty(login_type)) {
            throw new InvalidRequestException("login_type is null");
        }
        try {
            logger.info("verifyCode :"+verifyCode);
            String data = ykyyService.getDoctorInfoByVerifycode(verifyCode);
            JSONObject jsonObject = JSONObject.parseObject(data);
            if(!jsonObject.getString("code").equalsIgnoreCase("10000")){
                logger.info(jsonObject.getString("message"));
                return ObjEnvelop.getError("授权登录失败!");
            }
            JSONObject value = jsonObject.getJSONObject("value");
            String doctorId = value.getString("doctorId");
            String tel = value.getString("tel");
            String idCard = value.getString("idCard");
            String loginId = value.getString("loginId");
//            String idCard = "352103197207090030";
            BaseDoctorDO doctorDO = doctorDao.findByIdcard(idCard);
            if(doctorDO == null){
                return ObjEnvelop.getError("授权登录失败!");
            }
            ClientDetails authenticatedClient = clientDetailsService.loadClientByClientId(client_id);
            Map<String, String> parameters = new HashedMap();
            parameters.put("username", idCard);
            parameters.put("grant_type", "ihealthDcotor");
            TokenRequest tokenRequest = oAuth2RequestFactory.createTokenRequest(parameters, authenticatedClient);
            if (authenticatedClient != null) {
                oAuth2RequestValidator.validateScope(tokenRequest, authenticatedClient);
            }
            OAuth2AccessToken token = getTokenGranter().grant(tokenRequest.getGrantType(), tokenRequest);
            if (token == null) {
                throw new UnsupportedGrantTypeException("Unsupported grant type: " + tokenRequest.getGrantType());
            }
            WlyyUserSimple wlyyUserSimple = userDetailsService.authSuccess(parameters.get("username"));
            wlyyUserSimple.setAccessToken(token.getValue());
            wlyyUserSimple.setTokenType(token.getTokenType());
            wlyyUserSimple.setExpiresIn(token.getExpiresIn());
            wlyyUserSimple.setRefreshToken(token.getRefreshToken().getValue());
            wlyyUserSimple.setUser(parameters.get("username"));
            String loginType = parameters.get("login_type");
            BaseLoginLogDO baseLoginLogDO = new BaseLoginLogDO();
            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 ObjEnvelop.getSuccess("success",wlyyUserSimple);
        }catch (Exception e){
            logger.error(e);
        }
        return ObjEnvelop.getError("登录失败!");
    }
    @Override
    protected TokenGranter getTokenGranter() {
        return this.tokenGranter;
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.state(clientDetailsService != null, "ClientDetailsService must be provided");
        Assert.state(authenticationManager != null, "AuthenticationManager must be provided");
        oAuth2RequestFactory = new DefaultOAuth2RequestFactory(clientDetailsService);
    }
}

+ 33 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/service/YkyyService.java

@ -0,0 +1,33 @@
package com.yihu.jw.security.service;
import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.security.dao.doctor.BaseDoctorDao;
import com.yihu.jw.security.utils.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 眼科医院
 * Created by yeshijie on 2020/4/24.
 *
 * @author yeshijie.
 */
@Service
public class YkyyService {
    @Autowired
    private HttpClientUtil httpClientUtil;
    private static String yktUrl = "http://www.yanketong.com:133/api/";
    /**
     * 眼科医院单点登录接口
     * @return
     */
    public String getDoctorInfoByVerifycode(String verifyCode){
        String url = "Doctor/GetDoctorInfoByverifycode";
        return httpClientUtil.get(url,"GBK");
    }
}