| | |
| | | */ |
| | | package cn.gistack.auth.endpoint; |
| | | |
| | | |
| | | import cn.gistack.common.utils.HttpClientUtils; |
| | | import cn.gistack.system.user.entity.User; |
| | | import cn.gistack.system.user.feign.IUserClient; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONAware; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.wf.captcha.SpecCaptcha; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | import org.springblade.core.tool.support.Kv; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | import org.springblade.core.tool.utils.WebUtil; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import org.springframework.security.core.userdetails.UserDetails; |
| | | import org.springframework.security.core.userdetails.UserDetailsService; |
| | | import org.springframework.security.oauth2.common.OAuth2AccessToken; |
| | | import org.springframework.security.oauth2.common.OAuth2RefreshToken; |
| | | import org.springframework.security.oauth2.provider.AuthorizationRequest; |
| | | import org.springframework.security.oauth2.provider.ClientDetailsService; |
| | | import org.springframework.security.oauth2.provider.token.TokenStore; |
| | | import org.springframework.util.LinkedMultiValueMap; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import org.springframework.web.servlet.ModelAndView; |
| | | |
| | | import javax.servlet.http.HttpSession; |
| | | import java.time.Duration; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.Map; |
| | | |
| | | import static org.springblade.core.cache.constant.CacheConstant.*; |
| | | |
| | |
| | | private final JwtProperties jwtProperties; |
| | | private final ClientDetailsService clientDetailsService; |
| | | private final TokenStore tokenStore; |
| | | private final IUserClient userClient; |
| | | private final UserDetailsService userDetailsService; |
| | | //获取wxAccessToken |
| | | private final String WX_GET_ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token"; |
| | | private final String WX_GET_PHONE_NUMBER = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"; |
| | | private final String WX_APP_ID = "wx521ae71bcde281bc"; |
| | | private final String WX_SECRET = "c1c93b0c4545f8cdf42752121ea8458c"; |
| | | |
| | | /** |
| | | * 登录页面 |
| | |
| | | return model; |
| | | } |
| | | |
| | | @GetMapping("/oauth/wxLogin") |
| | | public R wxLogin(String code){ |
| | | String wxAccessToken = getWxAccessToken(); |
| | | //参数 |
| | | String url = WX_GET_PHONE_NUMBER+"?access_token="+wxAccessToken; |
| | | //向微信接口发起请求获取手机号 |
| | | |
| | | RestTemplate template = new RestTemplate(); |
| | | Map<String, Object> params = new LinkedHashMap<>(); |
| | | params.put("code", code); |
| | | String result = template.postForObject(url, params, String.class); |
| | | JSONObject resultObj = (JSONObject)JSONObject.parse(result); |
| | | JSONObject phoneInfo = (JSONObject)resultObj.get("phone_info"); |
| | | String phoneNumber = phoneInfo.get("phoneNumber").toString(); |
| | | R<User> userR = userClient.userByAccount("000000", phoneNumber); |
| | | if (userR.getData() != null){ |
| | | UserDetails userDetails = userDetailsService.loadUserByUsername(userR.getData().getAccount()); |
| | | return R.data(userDetails); |
| | | }else { |
| | | return R.fail("不存在该用户"); |
| | | } |
| | | } |
| | | |
| | | public String getWxAccessToken(){ |
| | | //参数 |
| | | Map<String, String> params = new HashMap<>(); |
| | | params.put("grant_type","client_credential"); |
| | | params.put("appid",WX_APP_ID); |
| | | params.put("secret",WX_SECRET); |
| | | String result = HttpClientUtils.doGet(WX_GET_ACCESS_TOKEN, params); |
| | | JSONObject jsonObject = JSON.parseObject(result); |
| | | String accessToken = jsonObject.getString("access_token"); |
| | | return accessToken; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 授权页面 |
| | | */ |