package com.lxzn.auth.security; import com.alibaba.fastjson.JSON; import com.lxzn.auth.RedisUtil; import com.lxzn.auth.config.ServerConfig; import com.lxzn.framework.domain.auth.ext.AuthToken; import com.lxzn.framework.domain.auth.response.AuthCode; import com.lxzn.framework.domain.auth.response.LoginResult; import com.lxzn.framework.exception.ExceptionCast; import com.lxzn.framework.model.response.CommonCode; import com.lxzn.framework.utils.Oauth2Util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.http.client.ClientHttpResponse; import org.springframework.stereotype.Service; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.DefaultResponseErrorHandler; import org.springframework.web.client.RestTemplate; import java.io.IOException; import java.util.Map; /** * @author Administrator * @version 1.0 **/ @Service public class AuthService { @Value("${auth.tokenValiditySeconds}") private int tokenValiditySeconds; @Autowired private RedisUtil redisUtil; @Autowired private ServerConfig serverConfig; @Autowired private RestTemplate restTemplate; //用户认证申请令牌,将令牌存储到redis public LoginResult login(String username, String password, String clientId, String clientSecret) { //请求spring security申请令牌 AuthToken authToken = this.applyToken(username, password, clientId, clientSecret); if(authToken == null){ ExceptionCast.cast(AuthCode.AUTH_LOGIN_APPLY_TOKEN_FAIL); } //用户身份令牌 String access_token = authToken.getAccess_token(); //存储到redis中的内容 String jsonString = JSON.toJSONString(authToken); //将令牌存储到redis boolean result = redisUtil.saveToken(access_token, jsonString, tokenValiditySeconds); if (!result) { ExceptionCast.cast(AuthCode.AUTH_LOGIN_TOKEN_SAVE_FAIL); } return new LoginResult(CommonCode.SUCCESS, authToken.getAccess_token()); } //申请令牌 private AuthToken applyToken(String username, String password, String clientId, String clientSecret){ //从eureka中获取认证服务的地址(因为spring security在认证服务中) //从eureka中获取认证服务的一个实例的地址 //定义header LinkedMultiValueMap header = new LinkedMultiValueMap<>(); String httpBasic = Oauth2Util.getHttpBasic(clientId, clientSecret); header.add("Authorization",httpBasic); //定义body LinkedMultiValueMap body = new LinkedMultiValueMap<>(); body.add("grant_type","password"); body.add("username",username); body.add("password",password); HttpEntity> httpEntity = new HttpEntity<>(body, header); //String url, HttpMethod method, @Nullable HttpEntity requestEntity, Class responseType, Object... uriVariables ResponseEntity exchange = restTemplate.exchange(serverConfig.getUrl() + UrlEnum.LOGIN_URL.getUrl(), HttpMethod.POST, httpEntity, Map.class); //申请令牌信息 Map bodyMap = exchange.getBody(); if(bodyMap == null || bodyMap.get("access_token") == null || bodyMap.get("refresh_token") == null || bodyMap.get("jti") == null){ return null; } AuthToken authToken = new AuthToken(); authToken.setAccess_token((String) bodyMap.get("jti"));//用户身份令牌 authToken.setRefresh_token((String) bodyMap.get("refresh_token"));//刷新令牌 authToken.setJwt_token((String) bodyMap.get("access_token"));//jwt令牌 return authToken; } }