| | |
| | | package cn.gistack.auth.endpoint; |
| | | |
| | | |
| | | import cn.gistack.auth.props.LoginUser; |
| | | import cn.gistack.auth.props.OAuthProperties; |
| | | 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.SneakyThrows; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import cn.gistack.common.cache.CacheNames; |
| | | import org.springblade.core.cache.utils.CacheUtil; |
| | |
| | | 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.beans.factory.annotation.Autowired; |
| | | 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.HttpServletResponse; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.time.Duration; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.Map; |
| | | |
| | | import static cn.gistack.common.cache.CacheNames.OAUTH_KEY; |
| | | import static org.springblade.core.cache.constant.CacheConstant.*; |
| | | |
| | | /** |
| | |
| | | private final String WX_APP_ID = "wx521ae71bcde281bc"; |
| | | private final String WX_SECRET = "c1c93b0c4545f8cdf42752121ea8458c"; |
| | | |
| | | @Autowired |
| | | private final OAuthProperties oauthProperties; |
| | | |
| | | /** |
| | | * 登录页面 |
| | | */ |
| | |
| | | return model; |
| | | } |
| | | |
| | | @SneakyThrows |
| | | @GetMapping("/oauth/tokenLoginUI") |
| | | public void tokenLoginUI(HttpServletResponse response, String code) { |
| | | response.sendRedirect(oauthProperties.getRedirectWebUri() + "?code=" + code); |
| | | } |
| | | |
| | | @GetMapping("/oauth/tokenLogin") |
| | | public R tokenLogin(String code) { |
| | | // 统一登录获取accessToken |
| | | String accessToken = getAccessToken(code); |
| | | System.out.println("accessToken:" + accessToken); |
| | | LoginUser loginUser = getUsernameByToken(accessToken); |
| | | loginUser.setCode(code); |
| | | bladeRedis.setEx(OAUTH_KEY + code,code,5*60L); |
| | | return R.data(loginUser); |
| | | } |
| | | |
| | | private String getAccessToken(String code) { |
| | | // 统一登录获取accessToken |
| | | String url = oauthProperties.getTokenUrl(); |
| | | Map<String, String> param = new HashMap<>(); |
| | | param.put("grant_type", "authorization_code"); |
| | | param.put("redirect_uri", oauthProperties.getRedirectUri()); |
| | | param.put("client_id", oauthProperties.getClientId()); |
| | | param.put("client_secret", oauthProperties.getClientSecret()); |
| | | param.put("AppKey", oauthProperties.getAppKey()); |
| | | param.put("AppSecret", oauthProperties.getAppSecret()); |
| | | param.put("code", code); |
| | | String accessToken = HttpClientUtils.doGet(url, param); |
| | | Map<String, String> maps = (Map<String, String>) JSON.parse(accessToken); |
| | | if (maps.containsKey("access_token")) { |
| | | return maps.get("access_token"); |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | private LoginUser getUsernameByToken(String accessToken) { |
| | | String url = oauthProperties.getProfileUrl(); |
| | | Map<String, String> param = new HashMap<>(); |
| | | param.put("AppKey", oauthProperties.getAppKey()); |
| | | param.put("AppSecret", oauthProperties.getAppSecret()); |
| | | param.put("accessToken", accessToken); |
| | | String resp = HttpClientUtils.doGet(url, param); |
| | | System.out.println("接收数据:" + resp); |
| | | LoginUser loginUser = JSON.parseObject(resp, LoginUser.class); |
| | | |
| | | return loginUser; |
| | | } |
| | | |
| | | @GetMapping("/oauth/wxLogin") |
| | | public R wxLogin(String code){ |
| | | String wxAccessToken = getWxAccessToken(); |