| | |
| | | */ |
| | | package org.springblade.auth.endpoint; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.wf.captcha.SpecCaptcha; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.SneakyThrows; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springblade.common.cache.CacheNames; |
| | | import org.springblade.common.utils.HttpClientUtils; |
| | | import org.springblade.core.cache.utils.CacheUtil; |
| | | import org.springblade.core.jwt.JwtUtil; |
| | | import org.springblade.core.jwt.props.JwtProperties; |
| | |
| | | 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.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import org.springframework.security.oauth2.common.OAuth2AccessToken; |
| | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.time.Duration; |
| | | import java.util.Base64; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | import static org.springblade.core.cache.constant.CacheConstant.*; |
| | | |
| | |
| | | return model; |
| | | } |
| | | |
| | | // /** |
| | | // * 登录页面 |
| | | // */ |
| | | // @SneakyThrows |
| | | // @GetMapping("/oauth/login") |
| | | // public void require(HttpServletResponse response) { |
| | | // response.sendRedirect("http://localhost:1888/"); |
| | | // } |
| | | /** |
| | | * 通过 code 获取 token |
| | | */ |
| | | @SneakyThrows |
| | | @GetMapping("/oauth/getTokenByCode") |
| | | public R require(HttpServletResponse response,String code) { |
| | | // 设置请求头 |
| | | Map<String, String> headerMap = new HashMap<>(); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_JSON); |
| | | String clientStr = "sword:sword_secret"; |
| | | String encodedString = Base64.getEncoder().encodeToString(clientStr.getBytes()); |
| | | headerMap.put("Authorization", "Basic " + encodedString); |
| | | // url |
| | | String url = "http://localhost:8100/oauth/token"; |
| | | // 参数 |
| | | Map<String, String> param = new HashMap<>(); |
| | | param.put("code", code); |
| | | param.put("grant_type", "authorization_code"); |
| | | param.put("redirect_uri", "http://192.168.1.50:1888"); |
| | | String accessToken = HttpClientUtils.doPost(url,headerMap ,param); |
| | | System.out.println("accessToken = " + accessToken); |
| | | Map<String, String> maps = (Map<String, String>) JSON.parse(accessToken); |
| | | return R.data(maps); |
| | | } |
| | | |
| | | /** |
| | | * 授权页面 |